SpringBoot集成Mybatis

您所在的位置:网站首页 威廉格罗辛 SpringBoot集成Mybatis

SpringBoot集成Mybatis

#SpringBoot集成Mybatis| 来源: 网络整理| 查看: 265

接上一章,SpringBoot项目实现增删改查_原你是阳光(#`O′)的博客-CSDN博客,集成Mybatis-Plus Mybatis-Plus特点

无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 – Sequence),可自由配置,完美解决主键问题

支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询分页插件

支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库

内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

引入依赖 com.baomidou mybatis-plus-boot-starter 3.5.1 org.projectlombok lombok 1.18.8 实体类: @Data @NoArgsConstructor @AllArgsConstructor public class People { /** * id */ private Long id; /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; /** * 地址 */ private String address; } controller @RestController public class PeopleController { @Autowired private IPeopleService peopleService; /** * 查询全部用户 * * @param people * @return */ @GetMapping("/selectAll") public List selectAll(People people) { return peopleService.selectAll(people); } /** * 增加用户或者修改用户 * * @param people * @return */ @PostMapping("/people") public int people(@RequestBody People people) throws Exception { return peopleService.people(people); } /** * 根据id查询一条数据 * * @param id * @return */ @GetMapping("/selectOne/{id}") public People selectOne(@PathVariable Long id) { return peopleService.selectOne(id); } /** * 删除数据 * @param id * @return */ @DeleteMapping("/delPeople/{id}") public int delPeople(@PathVariable Long id) throws Exception{ return peopleService.delPeople(id); } } service public interface IPeopleService extends IService { /** * 查询全部用户 * * @param people * @return */ List selectAll(People people); /** * 增加用户或者修改用户 * * @param people * @return */ int people(People people) throws Exception; /** * 根据id查询一条数据 * * @param id * @return */ People selectOne(Long id); /** * 删除数据 * @param id * @return */ int delPeople(Long id) throws Exception; } service实现类 @Service public class PeopleImpl extends ServiceImpl implements IPeopleService { @Autowired private PeopleMapper peopleMapper; /** * 查询全部用户 * * @param people * @return */ public List selectAll(People people) { return baseMapper.selectList(null); } /** * 增加用户或者修改用户 * * @param people * @return */ public int people(People people) throws Exception { //判断id是否为空,如果为空就是添加 if (StringUtils.isEmpty(people.getId())) { SnowFlake snowFlake = new SnowFlake(2, 3); long l = snowFlake.nextId(); //设置id people.setId(l); System.out.println("添加的id是:" + l); //添加数据 return baseMapper.insert(people); } else { //如果id不为空,就是修改 //到数据库查询是否存在这条数据,如果不存在抛出异常 People people1 = peopleMapper.selectById(people.getId()); if (people1 == null) { throw new Exception("修改的数据不存在!!!"); } //修改数据 return baseMapper.updateById(people); } } /** * 根据id查询一条数据 * * @param id * @return */ public People selectOne(Long id) { return peopleMapper.selectById(id); } /** * 删除数据 * * @param id * @return */ public int delPeople(Long id) throws Exception { if (id != null) { People people = peopleMapper.selectById(id); if (people != null) { //删除数据 return peopleMapper.deleteById(id); } else { throw new Exception("修改的数据不存在!!!"); } } else { throw new Exception("数据异常!!!"); } } } mapper接口 @Mapper public interface PeopleMapper extends BaseMapper { } mapper.xml 添加数据测试:

SpringBoot集成Mybatis-Plus  运行日志:

SpringBoot集成Mybatis-Plus 修改数据:

SpringBoot集成Mybatis-Plus 原数据

SpringBoot集成Mybatis-Plus 修改后的数据

SpringBoot集成Mybatis-Plus  运行日志

SpringBoot集成Mybatis-Plus 查询一条数据 运行结果

SpringBoot集成Mybatis-Plus 运行日志

SpringBoot集成Mybatis-Plus 删除数据: 运行结果

SpringBoot集成Mybatis-Plus 运行日志

SpringBoot集成Mybatis-Plus


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3