# spring-boot-demo-orm-mybatis-plus > 此 demo 演示了 Spring Boot 如何集成 mybatis-plus,简化Mybatis开发,带给你难以置信的开发体验。 > > - 2019-09-14 新增:ActiveRecord 模式操作 ## pom.xml ```xml 4.0.0 spring-boot-demo-orm-mybatis-plus 1.0.0-SNAPSHOT jar spring-boot-demo-orm-mybatis-plus Demo project for Spring Boot com.xkcoding spring-boot-demo 1.0.0-SNAPSHOT UTF-8 UTF-8 1.8 3.0.5 org.springframework.boot spring-boot-starter com.baomidou mybatis-plus-boot-starter ${mybatis.plus.version} org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java cn.hutool hutool-all com.google.guava guava org.projectlombok lombok true spring-boot-demo-orm-mybatis-plus org.springframework.boot spring-boot-maven-plugin ``` ## MybatisPlusConfig.java ```java /** *

* mybatis-plus 配置 *

* * @author yangkai.shen * @date Created in 2018-11-08 17:29 */ @Configuration @MapperScan(basePackages = {"com.xkcoding.orm.mybatis.plus.mapper"}) @EnableTransactionManagement public class MybatisPlusConfig { /** * 性能分析拦截器,不建议生产使用 */ @Bean public PerformanceInterceptor performanceInterceptor(){ return new PerformanceInterceptor(); } /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` ## CommonFieldHandler.java ```java package com.xkcoding.orm.mybatis.plus.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.util.Date; /** *

* 通用字段填充 *

* * @author yangkai.shen * @date Created in 2018-11-08 17:40 */ @Slf4j @Component public class CommonFieldHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { log.info("start insert fill ...."); this.setFieldValByName("createTime", new Date(), metaObject); this.setFieldValByName("lastUpdateTime", new Date(), metaObject); } @Override public void updateFill(MetaObject metaObject) { log.info("start update fill ...."); this.setFieldValByName("lastUpdateTime", new Date(), metaObject); } } ``` ## application.yml ```yaml spring: datasource: url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource initialization-mode: always continue-on-error: true schema: - "classpath:db/schema.sql" data: - "classpath:db/data.sql" hikari: minimum-idle: 5 connection-test-query: SELECT 1 FROM DUAL maximum-pool-size: 20 auto-commit: true idle-timeout: 30000 pool-name: SpringBootDemoHikariCP max-lifetime: 60000 connection-timeout: 30000 logging: level: com.xkcoding: debug com.xkcoding.orm.mybatis.plus.mapper: trace mybatis-plus: mapper-locations: classpath:mappers/*.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.xkcoding.orm.mybatis.plus.entity global-config: # 数据库相关配置 db-config: #主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID"; id-type: auto #字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断" field-strategy: not_empty #驼峰下划线转换 table-underline: true #是否开启大写命名,默认不开启 #capital-mode: true #逻辑删除配置 #logic-delete-value: 1 #logic-not-delete-value: 0 db-type: mysql #刷新mapper 调试神器 refresh: true # 原生配置 configuration: map-underscore-to-camel-case: true cache-enabled: true ``` ## UserMapper.java ```java /** *

* UserMapper *

* * @author yangkai.shen * @date Created in 2018-11-08 16:57 */ @Component public interface UserMapper extends BaseMapper { } ``` ## UserService.java ```java /** *

* User Service *

* * @author yangkai.shen * @date Created in 2018-11-08 18:10 */ public interface UserService extends IService { } ``` ## UserServiceImpl.java ```java /** *

* User Service *

* * @author yangkai.shen * @date Created in 2018-11-08 18:10 */ @Service public class UserServiceImpl extends ServiceImpl implements UserService { } ``` ## UserServiceTest.java ```java /** *

* User Service 测试 *

* * @author yangkai.shen * @date Created in 2018-11-08 18:13 */ @Slf4j public class UserServiceTest extends SpringBootDemoOrmMybatisPlusApplicationTests { @Autowired private UserService userService; /** * 测试Mybatis-Plus 新增 */ @Test public void testSave() { String salt = IdUtil.fastSimpleUUID(); User testSave3 = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave3@xkcoding.com").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).build(); boolean save = userService.save(testSave3); Assert.assertTrue(save); log.debug("【测试id回显#testSave3.getId()】= {}", testSave3.getId()); } /** * 测试Mybatis-Plus 批量新增 */ @Test public void testSaveList() { List userList = Lists.newArrayList(); for (int i = 4; i < 14; i++) { String salt = IdUtil.fastSimpleUUID(); User user = User.builder().name("testSave" + i).password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave" + i + "@xkcoding.com").phoneNumber("1730000000" + i).status(1).lastLoginTime(new DateTime()).build(); userList.add(user); } boolean batch = userService.saveBatch(userList); Assert.assertTrue(batch); List ids = userList.stream().map(User::getId).collect(Collectors.toList()); log.debug("【userList#ids】= {}", ids); } /** * 测试Mybatis-Plus 删除 */ @Test public void testDelete() { boolean remove = userService.removeById(1L); Assert.assertTrue(remove); User byId = userService.getById(1L); Assert.assertNull(byId); } /** * 测试Mybatis-Plus 修改 */ @Test public void testUpdate() { User user = userService.getById(1L); Assert.assertNotNull(user); user.setName("MybatisPlus修改名字"); boolean b = userService.updateById(user); Assert.assertTrue(b); User update = userService.getById(1L); Assert.assertEquals("MybatisPlus修改名字", update.getName()); log.debug("【update】= {}", update); } /** * 测试Mybatis-Plus 查询单个 */ @Test public void testQueryOne() { User user = userService.getById(1L); Assert.assertNotNull(user); log.debug("【user】= {}", user); } /** * 测试Mybatis-Plus 查询全部 */ @Test public void testQueryAll() { List list = userService.list(new QueryWrapper<>()); Assert.assertTrue(CollUtil.isNotEmpty(list)); log.debug("【list】= {}", list); } /** * 测试Mybatis-Plus 分页排序查询 */ @Test public void testQueryByPageAndSort() { initData(); int count = userService.count(new QueryWrapper<>()); Page userPage = new Page<>(1, 5); userPage.setDesc("id"); IPage page = userService.page(userPage, new QueryWrapper<>()); Assert.assertEquals(5, page.getSize()); Assert.assertEquals(count, page.getTotal()); log.debug("【page.getRecords()】= {}", page.getRecords()); } /** * 测试Mybatis-Plus 自定义查询 */ @Test public void testQueryByCondition() { initData(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.like("name", "Save1").or().eq("phone_number", "17300000001").orderByDesc("id"); int count = userService.count(wrapper); Page userPage = new Page<>(1, 3); IPage page = userService.page(userPage, wrapper); Assert.assertEquals(3, page.getSize()); Assert.assertEquals(count, page.getTotal()); log.debug("【page.getRecords()】= {}", page.getRecords()); } /** * 初始化数据 */ private void initData() { testSaveList(); } } ``` ## 2019-09-14新增 ### ActiveRecord 模式 - Role.java ```java /** *

* 角色实体类 *

* * @author yangkai.shen * @date Created in 2019-09-16 14:04 */ @Data @TableName("orm_role") @Accessors(chain = true) @EqualsAndHashCode(callSuper = true) public class Role extends Model { /** * 主键 */ private Long id; /** * 角色名 */ private String name; /** * 主键值,ActiveRecord 模式这个必须有,否则 xxById 的方法都将失效! * 即使使用 ActiveRecord 不会用到 RoleMapper,RoleMapper 这个接口也必须创建 */ @Override protected Serializable pkVal() { return this.id; } } ``` - RoleMapper.java ```java /** *

* RoleMapper *

* * @author yangkai.shen * @date Created in 2019-09-16 14:06 */ public interface RoleMapper extends BaseMapper { } ``` - ActiveRecordTest.java ```java /** *

* Role *

* * @author yangkai.shen * @date Created in 2019-09-16 14:19 */ @Slf4j public class ActiveRecordTest extends SpringBootDemoOrmMybatisPlusApplicationTests { /** * 测试 ActiveRecord 插入数据 */ @Test public void testActiveRecordInsert() { Role role = new Role(); role.setName("VIP"); Assert.assertTrue(role.insert()); // 成功直接拿会写的 ID log.debug("【role】= {}", role); } /** * 测试 ActiveRecord 更新数据 */ @Test public void testActiveRecordUpdate() { Assert.assertTrue(new Role().setId(1L).setName("管理员-1").updateById()); Assert.assertTrue(new Role().update(new UpdateWrapper().lambda().set(Role::getName, "普通用户-1").eq(Role::getId, 2))); } /** * 测试 ActiveRecord 查询数据 */ @Test public void testActiveRecordSelect() { Assert.assertEquals("管理员", new Role().setId(1L).selectById().getName()); Role role = new Role().selectOne(new QueryWrapper().lambda().eq(Role::getId, 2)); Assert.assertEquals("普通用户", role.getName()); List roles = new Role().selectAll(); Assert.assertTrue(roles.size() > 0); log.debug("【roles】= {}", roles); } /** * 测试 ActiveRecord 删除数据 */ @Test public void testActiveRecordDelete() { Assert.assertTrue(new Role().setId(1L).deleteById()); Assert.assertTrue(new Role().delete(new QueryWrapper().lambda().eq(Role::getName, "普通用户"))); } } ``` ## 参考 - mybatis-plus官方文档:http://mp.baomidou.com/