1、准备数据库表
2、创建springboot工程,引入对应的起步依赖(web、mybatis、mysql驱动、lombok)
3、配置文件application.properties引入mybatis的配置信息,准备对应实体类
#?????????
#?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#??????url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis01
#?????????
spring.datasource.username=root
#????????
spring.datasource.password=root
#??mybaits?????
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.map-underscore-to-camel-case= true
4、三层架构(controller、Service、Mapper)
添加注解
在pojo实体类中
package com.yyy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code;//响应码,1 代表成功; 0 代表失败
private String msg; //响应信息 描述字符串
private Object data; //返回的数据
//增删改 成功响应
public static Result success(){
return new Result(1,"success",null);
}
//查询 成功响应
public static Result success(Object data){
return new Result(1,"success",data);
}
//失败响应
public static Result error(String msg){
return new Result(0,msg,null);
}
}
@Slf4j(在类上面申明,)=private static Logger log = LoggerFactory.getLogger(DeptController.class);
@RequestMapping(value = "/depts",method = RequestMethod.GET)= @GetMapping("/depts")
新增部门:
requestbody将json数据转变为实体数据