diff --git a/pom.xml b/pom.xml index 3269d27..9d6d895 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ spring-boot-demo-orm-jdbctemplate spring-boot-demo-orm-jpa spring-boot-demo-orm-mybatis + spring-boot-demo-orm-mybatis-mapper-page spring-boot-demo-email spring-boot-demo-upload spring-boot-demo-war diff --git a/spring-boot-demo-orm-mybatis-mapper-page/.gitignore b/spring-boot-demo-orm-mybatis-mapper-page/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-demo-orm-mybatis-mapper-page/README.md b/spring-boot-demo-orm-mybatis-mapper-page/README.md new file mode 100644 index 0000000..07a1f55 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/README.md @@ -0,0 +1,336 @@ +# spring-boot-demo-orm-mybatis-mapper-page + +> 此 demo 演示了 Spring Boot 如何集成通用Mapper插件和分页助手插件,简化Mybatis开发,带给你难以置信的开发体验。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-orm-mybatis-mapper-page + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-orm-mybatis-mapper-page + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 2.0.4 + 1.2.9 + + + + + org.springframework.boot + spring-boot-starter + + + + + tk.mybatis + mapper-spring-boot-starter + ${mybatis.mapper.version} + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + ${mybatis.pagehelper.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + com.google.guava + guava + + + + mysql + mysql-connector-java + + + + + spring-boot-demo-orm-mybatis-mapper-page + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## SpringBootDemoOrmMybatisApplication.java + +```java +/** + *

+ * 启动器 + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage + * @description: 启动器 + * @author: yangkai.shen + * @date: Created in 2018/11/8 13:43 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.MapperAndPage.mapper"}) // 注意:这里的 MapperScan 是 tk.mybatis.spring.annotation.MapperScan 这个包下的 +public class SpringBootDemoOrmMybatisMapperPageApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoOrmMybatisMapperPageApplication.class, args); + } +} +``` + +## 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.MapperAndPage.mapper: trace +mybatis: + configuration: + # 下划线转驼峰 + map-underscore-to-camel-case: true + mapper-locations: classpath:mappers/*.xml + type-aliases-package: com.xkcoding.orm.mybatis.MapperAndPage.entity +mapper: + mappers: + - tk.mybatis.mapper.common.Mapper + not-empty: true + style: camelhump + wrap-keyword: "`{0}`" + safe-delete: true + safe-update: true + identity: MYSQL +pagehelper: + auto-dialect: true + helper-dialect: mysql + reasonable: true + params: count=countSql +``` + +## UserMapper.java + +```java +/** + *

+ * UserMapper + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage.mapper + * @description: UserMapper + * @author: yangkai.shen + * @date: Created in 2018/11/8 14:15 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +// 注意:这里的Mapper是tk.mybatis.mapper.common.Mapper包下的 +public interface UserMapper extends Mapper, MySqlMapper { +} +``` + +## UserMapperTest.java + +```java +/** + *

+ * UserMapper 测试 + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage.mapper + * @description: UserMapper 测试 + * @author: yangkai.shen + * @date: Created in 2018/11/8 14:25 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserMapperTest extends SpringBootDemoOrmMybatisMapperPageApplicationTests { + + @Autowired + private UserMapper userMapper; + + /** + * 测试通用Mapper - 保存 + */ + @Test + public void testInsert() { + 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()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + userMapper.insertUseGeneratedKeys(testSave3); + Assert.assertNotNull(testSave3.getId()); + log.debug("【测试主键回写#testSave3.getId()】= {}", testSave3.getId()); + } + + /** + * 测试通用Mapper - 批量保存 + */ + @Test + public void testInsertList() { + 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()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + userList.add(user); + } + int i = userMapper.insertList(userList); + Assert.assertEquals(userList.size(), i); + List ids = userList.stream().map(User::getId).collect(Collectors.toList()); + log.debug("【测试主键回写#userList.ids】= {}", ids); + } + + /** + * 测试通用Mapper - 删除 + */ + @Test + public void testDelete() { + Long primaryKey = 1L; + int i = userMapper.deleteByPrimaryKey(primaryKey); + Assert.assertEquals(1, i); + User user = userMapper.selectByPrimaryKey(primaryKey); + Assert.assertNull(user); + } + + /** + * 测试通用Mapper - 更新 + */ + @Test + public void testUpdate() { + Long primaryKey = 1L; + User user = userMapper.selectByPrimaryKey(primaryKey); + user.setName("通用Mapper名字更新"); + int i = userMapper.updateByPrimaryKeySelective(user); + Assert.assertEquals(1, i); + User update = userMapper.selectByPrimaryKey(primaryKey); + Assert.assertNotNull(update); + Assert.assertEquals("通用Mapper名字更新", update.getName()); + log.debug("【update】= {}", update); + } + + /** + * 测试通用Mapper - 查询 + */ + @Test + public void testQueryAll() { + List users = userMapper.selectAll(); + Assert.assertTrue(CollUtil.isNotEmpty(users)); + log.debug("【users】= {}", users); + } + + /** + * 测试分页助手 - 分页排序查询 + */ + @Test + public void testQueryByPageAndSort() { + initData(); + int currentPage = 1; + int pageSize = 5; + String orderBy = "id desc"; + int count = userMapper.selectCount(null); + PageHelper.startPage(currentPage, pageSize, orderBy); + List users = userMapper.selectAll(); + PageInfo userPageInfo = new PageInfo<>(users); + Assert.assertEquals(5, userPageInfo.getSize()); + Assert.assertEquals(count, userPageInfo.getTotal()); + log.debug("【userPageInfo】= {}", userPageInfo); + } + + /** + * 测试通用Mapper - 条件查询 + */ + @Test + public void testQueryByCondition() { + initData(); + Example example = new Example(User.class); + // 过滤 + example.createCriteria().andLike("name", "%Save1%").orEqualTo("phoneNumber", "17300000001"); + // 排序 + example.setOrderByClause("id desc"); + int count = userMapper.selectCountByExample(example); + // 分页 + PageHelper.startPage(1, 3); + // 查询 + List userList = userMapper.selectByExample(example); + PageInfo userPageInfo = new PageInfo<>(userList); + Assert.assertEquals(3, userPageInfo.getSize()); + Assert.assertEquals(count, userPageInfo.getTotal()); + log.debug("【userPageInfo】= {}", userPageInfo); + } + + /** + * 初始化数据 + */ + private void initData() { + testInsertList(); + } + +} +``` + +## 参考 + +- 通用Mapper官方文档:https://github.com/abel533/Mapper/wiki/1.integration +- pagehelper 官方文档:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md \ No newline at end of file diff --git a/spring-boot-demo-orm-mybatis-mapper-page/pom.xml b/spring-boot-demo-orm-mybatis-mapper-page/pom.xml new file mode 100644 index 0000000..07a0822 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + spring-boot-demo-orm-mybatis-mapper-page + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-orm-mybatis-mapper-page + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 2.0.4 + 1.2.9 + + + + + org.springframework.boot + spring-boot-starter + + + + + tk.mybatis + mapper-spring-boot-starter + ${mybatis.mapper.version} + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + ${mybatis.pagehelper.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + com.google.guava + guava + + + + mysql + mysql-connector-java + + + + + spring-boot-demo-orm-mybatis-mapper-page + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplication.java b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplication.java new file mode 100644 index 0000000..41e1887 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplication.java @@ -0,0 +1,27 @@ +package com.xkcoding.orm.mybatis.MapperAndPage; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import tk.mybatis.spring.annotation.MapperScan; + +/** + *

+ * 启动器 + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage + * @description: 启动器 + * @author: yangkai.shen + * @date: Created in 2018/11/8 13:43 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.MapperAndPage.mapper"}) +public class SpringBootDemoOrmMybatisMapperPageApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoOrmMybatisMapperPageApplication.class, args); + } +} diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/entity/User.java b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/entity/User.java new file mode 100644 index 0000000..07ecca1 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/entity/User.java @@ -0,0 +1,86 @@ +package com.xkcoding.orm.mybatis.MapperAndPage.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import tk.mybatis.mapper.annotation.KeySql; + +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.Date; + +/** + *

+ * 用户实体类 + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage.entity + * @description: 用户实体类 + * @author: yangkai.shen + * @date: Created in 2018/11/8 14:14 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Table(name = "orm_user") +public class User implements Serializable { + private static final long serialVersionUID = -1840831686851699943L; + + /** + * 主键 + */ + @Id + @KeySql(useGeneratedKeys = true) + private Long id; + + /** + * 用户名 + */ + private String name; + + /** + * 加密后的密码 + */ + private String password; + + /** + * 加密使用的盐 + */ + private String salt; + + /** + * 邮箱 + */ + private String email; + + /** + * 手机号码 + */ + private String phoneNumber; + + /** + * 状态,-1:逻辑删除,0:禁用,1:启用 + */ + private Integer status; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 上次登录时间 + */ + private Date lastLoginTime; + + /** + * 上次更新时间 + */ + private Date lastUpdateTime; +} diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapper.java b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapper.java new file mode 100644 index 0000000..4cc51ae --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapper.java @@ -0,0 +1,23 @@ +package com.xkcoding.orm.mybatis.MapperAndPage.mapper; + +import com.xkcoding.orm.mybatis.MapperAndPage.entity.User; +import org.springframework.stereotype.Component; +import tk.mybatis.mapper.common.Mapper; +import tk.mybatis.mapper.common.MySqlMapper; + +/** + *

+ * UserMapper + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage.mapper + * @description: UserMapper + * @author: yangkai.shen + * @date: Created in 2018/11/8 14:15 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +public interface UserMapper extends Mapper, MySqlMapper { +} diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/application.yml b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/application.yml new file mode 100644 index 0000000..885c9bf --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/application.yml @@ -0,0 +1,46 @@ +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.MapperAndPage.mapper: trace +mybatis: + configuration: + # 下划线转驼峰 + map-underscore-to-camel-case: true + mapper-locations: classpath:mappers/*.xml + type-aliases-package: com.xkcoding.orm.mybatis.MapperAndPage.entity +mapper: + mappers: + - tk.mybatis.mapper.common.Mapper + not-empty: true + style: camelhump + wrap-keyword: "`{0}`" + safe-delete: true + safe-update: true + identity: MYSQL +pagehelper: + auto-dialect: true + helper-dialect: mysql + reasonable: true + params: count=countSql \ No newline at end of file diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/data.sql b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/data.sql new file mode 100644 index 0000000..3ab2b59 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/data.sql @@ -0,0 +1,2 @@ +INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); +INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); \ No newline at end of file diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/schema.sql b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/schema.sql new file mode 100644 index 0000000..22804e5 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/main/resources/db/schema.sql @@ -0,0 +1,13 @@ +DROP TABLE IF EXISTS `orm_user`; +CREATE TABLE `orm_user` ( + `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', + `password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', + `salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', + `email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', + `phone_number` VARCHAR(15) NOT NULL UNIQUE COMMENT '手机号码', + `status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态,-1:逻辑删除,0:禁用,1:启用', + `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', + `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', + `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplicationTests.java b/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplicationTests.java new file mode 100644 index 0000000..a23d224 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/SpringBootDemoOrmMybatisMapperPageApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.orm.mybatis.MapperAndPage; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootDemoOrmMybatisMapperPageApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapperTest.java b/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapperTest.java new file mode 100644 index 0000000..ae4815d --- /dev/null +++ b/spring-boot-demo-orm-mybatis-mapper-page/src/test/java/com/xkcoding/orm/mybatis/MapperAndPage/mapper/UserMapperTest.java @@ -0,0 +1,154 @@ +package com.xkcoding.orm.mybatis.MapperAndPage.mapper; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.util.IdUtil; +import cn.hutool.crypto.SecureUtil; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.xkcoding.orm.mybatis.MapperAndPage.SpringBootDemoOrmMybatisMapperPageApplicationTests; +import com.xkcoding.orm.mybatis.MapperAndPage.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.assertj.core.util.Lists; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import tk.mybatis.mapper.entity.Example; + +import java.util.List; +import java.util.stream.Collectors; + +/** + *

+ * UserMapper 测试 + *

+ * + * @package: com.xkcoding.orm.mybatis.MapperAndPage.mapper + * @description: UserMapper 测试 + * @author: yangkai.shen + * @date: Created in 2018/11/8 14:25 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserMapperTest extends SpringBootDemoOrmMybatisMapperPageApplicationTests { + + @Autowired + private UserMapper userMapper; + + /** + * 测试通用Mapper - 保存 + */ + @Test + public void testInsert() { + 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()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + userMapper.insertUseGeneratedKeys(testSave3); + Assert.assertNotNull(testSave3.getId()); + log.debug("【测试主键回写#testSave3.getId()】= {}", testSave3.getId()); + } + + /** + * 测试通用Mapper - 批量保存 + */ + @Test + public void testInsertList() { + 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()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + userList.add(user); + } + int i = userMapper.insertList(userList); + Assert.assertEquals(userList.size(), i); + List ids = userList.stream().map(User::getId).collect(Collectors.toList()); + log.debug("【测试主键回写#userList.ids】= {}", ids); + } + + /** + * 测试通用Mapper - 删除 + */ + @Test + public void testDelete() { + Long primaryKey = 1L; + int i = userMapper.deleteByPrimaryKey(primaryKey); + Assert.assertEquals(1, i); + User user = userMapper.selectByPrimaryKey(primaryKey); + Assert.assertNull(user); + } + + /** + * 测试通用Mapper - 更新 + */ + @Test + public void testUpdate() { + Long primaryKey = 1L; + User user = userMapper.selectByPrimaryKey(primaryKey); + user.setName("通用Mapper名字更新"); + int i = userMapper.updateByPrimaryKeySelective(user); + Assert.assertEquals(1, i); + User update = userMapper.selectByPrimaryKey(primaryKey); + Assert.assertNotNull(update); + Assert.assertEquals("通用Mapper名字更新", update.getName()); + log.debug("【update】= {}", update); + } + + /** + * 测试通用Mapper - 查询 + */ + @Test + public void testQueryAll() { + List users = userMapper.selectAll(); + Assert.assertTrue(CollUtil.isNotEmpty(users)); + log.debug("【users】= {}", users); + } + + /** + * 测试分页助手 - 分页排序查询 + */ + @Test + public void testQueryByPageAndSort() { + initData(); + int currentPage = 1; + int pageSize = 5; + String orderBy = "id desc"; + int count = userMapper.selectCount(null); + PageHelper.startPage(currentPage, pageSize, orderBy); + List users = userMapper.selectAll(); + PageInfo userPageInfo = new PageInfo<>(users); + Assert.assertEquals(5, userPageInfo.getSize()); + Assert.assertEquals(count, userPageInfo.getTotal()); + log.debug("【userPageInfo】= {}", userPageInfo); + } + + /** + * 测试通用Mapper - 条件查询 + */ + @Test + public void testQueryByCondition() { + initData(); + Example example = new Example(User.class); + // 过滤 + example.createCriteria().andLike("name", "%Save1%").orEqualTo("phoneNumber", "17300000001"); + // 排序 + example.setOrderByClause("id desc"); + int count = userMapper.selectCountByExample(example); + // 分页 + PageHelper.startPage(1, 3); + // 查询 + List userList = userMapper.selectByExample(example); + PageInfo userPageInfo = new PageInfo<>(userList); + Assert.assertEquals(3, userPageInfo.getSize()); + Assert.assertEquals(count, userPageInfo.getTotal()); + log.debug("【userPageInfo】= {}", userPageInfo); + } + + /** + * 初始化数据 + */ + private void initData() { + testInsertList(); + } + +} \ No newline at end of file