Spring_Boot 简单例子
第一步创建项目:
创建项目地址:https://start.spring.io/

接下来就下载到本地了 跟着加压

接着用idea打开:等待资源下载完成



我写了个简单的:增删改查
项目结构:

dao层:
package com.nf147.demo.dao; import com.nf147.demo.entity.News;
import org.springframework.data.jpa.repository.JpaRepository; public interface NewsMapper extends JpaRepository<News,Integer> { //第一个参数是实体类,第二个是id的类型
}
entity层:
package com.nf147.demo.entity; import javax.persistence.*; @Entity
@Table(name = "news") //表名
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //标明该字段是自动增长
private int id;
private String title;
private String body; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getBody() {
return body;
} public void setBody(String body) {
this.body = body;
}
}
service层:
package com.nf147.demo.service;
import com.nf147.demo.entity.News;
import java.util.List;
public interface NewsService {
List<News> listAll();
void add (News news);
void del (int id);
void update(News news);
}
实现服务接口:
package com.nf147.demo.service; import com.nf147.demo.dao.NewsMapper;
import com.nf147.demo.entity.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class NewsServiceImp implements NewsService { @Autowired
private NewsMapper newsMapper; @Override
@Cacheable("listNews")
public List<News> listAll() {
return newsMapper.findAll();
} @Override
public void add(News news) {
newsMapper.save(news); } @Override
public void del(int id) {
newsMapper.deleteById(id);
} @Override
public void update(News news) {
newsMapper.save(news);
}
}
controller层:
package com.nf147.demo.controller; import com.nf147.demo.entity.News;
import com.nf147.demo.service.NewsServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class NewsController { @Autowired
private NewsServiceImp newsServiceImp; //查询
@RequestMapping(value = "/listNews", method = RequestMethod.GET)
public List<News> getNews() {
return newsServiceImp.listAll();
} //添加 http://localhost:8082/listNewsAdd?title=标题&body=随便给的内容
@RequestMapping(value = "/listNewsAdd", method = RequestMethod.GET)
public void add(News news) {
newsServiceImp.add(news);
} //删除
//地址栏写法 http://localhost:8082/listNewsdel?id=7
@RequestMapping(value = "/listNewsdel", method = RequestMethod.GET)
public void del(int id) {
newsServiceImp.del(id);
} //修改
//地址栏写法 http://localhost:8082/listNewsupdate?id=6&title=好好&body=学习
@RequestMapping(value = "/listNewsupdate", method = RequestMethod.GET)
public void del(News news) {
newsServiceImp.update(news);
} }
测试:

项目下载地址:https://github.com/nongzihong/Spring_Boot
Spring_Boot 简单例子的更多相关文章
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select
以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...
- jsonp的简单例子
jsonp的简单例子 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
随机推荐
- mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement
linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ...
- numpy数组的运算
numpy数组的运算 数组的乘法 >>> import numpy as np >>> arr=np.array([[1,2,3],[4,5,6]]) >&g ...
- Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式
Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...
- python常量 (最全常量解析)
常量 一.常量 变量是变化的量,常量则是不变的量.python中没有使用语法强制定义常量,也就是说,python中定义常量本质上就是变量.如果非要定义常量,变量名必须全大写. AGE_OF_NICK ...
- HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
pog loves szh III Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- Enlarge GCD(素数筛)
题意 删去最少的数,使gcd变大 题解 只要保留相同素数因子最多的数即可. 素数筛. C++代码 #include<bits/stdc++.h> using namespace std; ...
- express热更新nodemon,自启动项目
一.说一下 每次修改文件,我们都需要重启服务器npm start,很麻烦,所以使用引入nodemon插件,解决这个问题,实现保存文件,即自启动刷新项目 二.直接开码 npm install nodem ...
- linux系统下如何在vscode中调试C++代码
本篇博客以一个简单的hello world程序,介绍在vscode中调试C++代码的配置过程. 1. 安装编译器 vscode是一个轻量的代码编辑器,并不具备代码编译功能,代码编译需要交给编译器完成. ...
- 简单Spring Cloud 微服务框架搭建
微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ...
- Python学习笔记-列表的增删改查