新建一个包,里面放入一些常用的配置类
引入包
前往官网查看
https://baomidou.com/pages/226c21/#%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B7%A5%E7%A8%8B
com.baomidou mybatis-plus-boot-starter3.4.3.4
MybatisPlusConfig----MybatisPlus配置文件
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.example.demo.mapper") public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; } }
配置result包装返回与前台交互数据对象
code 代表成功或者失败;
msg即返回的信息
package com.example.demo.common; //T是范型 public class Result编写用户接口{ private String code; private String msg; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public Result(){ } public Result(T data){ this.data = data; } public static Result success(){ Result result = new Result<>(); result.setCode("0"); result.setMsg("成功"); return result; } //返回一个数据 public static Result success(T data) { Result result = new Result<>(data); result.setCode("0"); result.setMsg("成功"); return result; } //错误返回 public static Result error(String code,String msg){ Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
新建一个包entity实体类
建一个java对象与数据库进行一 一对应
package com.example.demo.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; //与数据库的表名一一对应 @TableName("user") //使用lombok插件简化快速写代码 @Data public class User { //定义自增 @TableId(type = IdType.AUTO) private Integer id; private String username; private Integer age; private String sex; private String address; private String password; }
编写用户接口UserControlller
新建一个包Controller
package com.example.demo.controller; import com.example.demo.common.Result; import com.example.demo.entity.User; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; //定义返回json数据 @RestController //定义路由 @RequestMapping("/user") public class UserController { //定义post接口 @PostMapping //前端网页的新增用户接口,@RequestBody作用是使前台传来的json格式的代码转化为java对象 public Result save(@RequestBody User user) { } }
拿到user数据之后就可以把它插入到数据库当中,编写mapper部分
创建一个UserMapper接口类
前往mybatis-plus官网参考
https://baomidou.com/pages/49cc81/#mapper-crud-%E6%8E%A5%E5%8F%A3
继承baseMapper 就可以实现一些数据库单表的增删改查
package com.example.demo.mapper; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.example.demo.entity.User; public interface UserMapper extends baseMapper最后完善一下UserConler接口{ }
package com.example.demo.controller; import com.example.demo.common.Result; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; //定义返回json数据 @RestController //定义路由 @RequestMapping("/user") public class UserController { //将mapper引入 @Resource UserMapper userMapper; //定义post接口 @PostMapping //前端网页的新增用户接口,@RequestBody作用是使前台传来的json格式的代码转化为java对象 //>泛型表示任何一种类型 public Result> save(@RequestBody User user) { userMapper.insert(user); return Result.success(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)