一,先采用IDEA构建项目步骤如下
二,项目构建好之后,在pom.xml添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
第三步,把application.properties修改为application.yaml
在文件中添加如下内容
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
# 这是链接数据库的账号
username: root
# 这是链接数据库的密码
password: root
第四步 构建目录结构,如下图
第五步,在entity包编写user实体类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data // 自动注入set和get方法
@AllArgsConstructor //自动生成有参构造方法
@NoArgsConstructor //自动生成无参构造方法
@TableName("user") //对应的数据表
public class User {
// 因为数据表的ID是自增的,所以需配置一下
@TableId(value ="userid" ,type = IdType.AUTO)
private Integer userid;
private String userName; //对应数据库的字段是 user_name
private String userPwd; //对应数据库的字段是 user_pwd
private String sex;
private int age;
}
第六步 在entity包编写mybatisplus分页插件
/**
- 使用这个配置类体添加mybatisplus的分页插件
*/
@Configuration
public class PageConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
第七 在dao包新建UserMapper接口
第八 在service包新建UserService接口
第九 在impl包中新建UserServiceImpl类
还有一个工具类,返回统一格式
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
/**
* 响应体,主要用来做接口统一返回格式
*/
@Data
public class Result {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
public Result() {
}
public Result(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public Result(Object data) {
this.status = 0;
this.msg = "OK";
this.data = data;
}
public static Result ok() {
return new Result(null);
}
public static Result ok(Object data) {
return new Result(data);
}
public static Result build(Integer status, String msg) {
return new Result(status, msg, null);
}
public static Result build(Integer status, String msg, Object data) {
return new Result(status, msg, data);
}
}
第十步 新建controller
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/getAllUser")
public Page<User> getAllUser() {
List<User> list = userService.list();
for (User user : list) {
System.out.println(user.toString());
}
//构建分页对象
Page<User> userPage = new Page<>(2,2);
//第一个参数为分页对象,第二个为查询条件(可以不写)
Page<User> page = userService.page(userPage, null);
System.out.println("总页码:" + page.getPages());
System.out.println("总记录数:" + page.getTotal());
return page;
}
/**
* 根据id获取数据
* @param
* @return
*/
@RequestMapping(value = "/getUserById",method = RequestMethod.POST)
public Result getUserById(@RequestBody User user){
User userSelect = userService.getById(user.getUserid());
return Result.ok(userSelect);
}
/**
* 根据id更新数据
* @param user
* @return
*/
@PostMapping("/update")
public Result updateById(@RequestBody User user){
boolean ret = userService.updateById(user);
if (!ret){
return Result.build(-1,"更新失败");
}
return Result.ok();
}
/**
* 新增用户
* @param user
* @return
*/
@PostMapping("/save")
public Result saveUser(@RequestBody User user){
try {
boolean ret = userService.save(user);
if (!ret){
return Result.build(-1,"新增失败");
}
return Result.ok();
} catch (Exception e) {
return Result.build(-1,e.getMessage());
}
}
/**
* 删除用户
* @param user
* @return
*/
@PostMapping("/delete")
public Result deleteUser(@RequestBody User user){
try {
boolean ret = userService.removeById(user.getUserid());
if (!ret){
return Result.build(-1,"删除失败");
}
return Result.ok();
} catch (Exception e) {
return Result.build(-1,e.getMessage());
}
}
}
第十一 采用postman进行测试接口
1,查询所有
2,根据ID查询
3,更新
4,根据ID删除
5,添加