You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # spring-boot-demo-orm-mybatis-mapper-page
  2. > 此 demo 演示了 Spring Boot 如何集成通用Mapper插件和分页助手插件,简化Mybatis开发,带给你难以置信的开发体验。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-orm-mybatis-mapper-page</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-orm-mybatis-mapper-page</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. <mybatis.mapper.version>2.0.4</mybatis.mapper.version>
  24. <mybatis.pagehelper.version>1.2.9</mybatis.pagehelper.version>
  25. </properties>
  26. <dependencies>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter</artifactId>
  30. </dependency>
  31. <!-- 通用Mapper -->
  32. <dependency>
  33. <groupId>tk.mybatis</groupId>
  34. <artifactId>mapper-spring-boot-starter</artifactId>
  35. <version>${mybatis.mapper.version}</version>
  36. </dependency>
  37. <!-- 分页助手 -->
  38. <dependency>
  39. <groupId>com.github.pagehelper</groupId>
  40. <artifactId>pagehelper-spring-boot-starter</artifactId>
  41. <version>${mybatis.pagehelper.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.projectlombok</groupId>
  50. <artifactId>lombok</artifactId>
  51. <optional>true</optional>
  52. </dependency>
  53. <dependency>
  54. <groupId>cn.hutool</groupId>
  55. <artifactId>hutool-all</artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.google.guava</groupId>
  59. <artifactId>guava</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>mysql</groupId>
  63. <artifactId>mysql-connector-java</artifactId>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <finalName>spring-boot-demo-orm-mybatis-mapper-page</finalName>
  68. <plugins>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>
  76. ```
  77. ## SpringBootDemoOrmMybatisApplication.java
  78. ```java
  79. /**
  80. * <p>
  81. * 启动器
  82. * </p>
  83. *
  84. * @author yangkai.shen
  85. * @date Created in 2018-11-08 13:43
  86. */
  87. @SpringBootApplication
  88. @MapperScan(basePackages = {"com.xkcoding.orm.mybatis.MapperAndPage.mapper"}) // 注意:这里的 MapperScan 是 tk.mybatis.spring.annotation.MapperScan 这个包下的
  89. public class SpringBootDemoOrmMybatisMapperPageApplication {
  90. public static void main(String[] args) {
  91. SpringApplication.run(SpringBootDemoOrmMybatisMapperPageApplication.class, args);
  92. }
  93. }
  94. ```
  95. ## application.yml
  96. ```yaml
  97. spring:
  98. datasource:
  99. 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
  100. username: root
  101. password: root
  102. driver-class-name: com.mysql.cj.jdbc.Driver
  103. type: com.zaxxer.hikari.HikariDataSource
  104. initialization-mode: always
  105. continue-on-error: true
  106. schema:
  107. - "classpath:db/schema.sql"
  108. data:
  109. - "classpath:db/data.sql"
  110. hikari:
  111. minimum-idle: 5
  112. connection-test-query: SELECT 1 FROM DUAL
  113. maximum-pool-size: 20
  114. auto-commit: true
  115. idle-timeout: 30000
  116. pool-name: SpringBootDemoHikariCP
  117. max-lifetime: 60000
  118. connection-timeout: 30000
  119. logging:
  120. level:
  121. com.xkcoding: debug
  122. com.xkcoding.orm.mybatis.MapperAndPage.mapper: trace
  123. mybatis:
  124. configuration:
  125. # 下划线转驼峰
  126. map-underscore-to-camel-case: true
  127. mapper-locations: classpath:mappers/*.xml
  128. type-aliases-package: com.xkcoding.orm.mybatis.MapperAndPage.entity
  129. mapper:
  130. mappers:
  131. - tk.mybatis.mapper.common.Mapper
  132. not-empty: true
  133. style: camelhump
  134. wrap-keyword: "`{0}`"
  135. safe-delete: true
  136. safe-update: true
  137. identity: MYSQL
  138. pagehelper:
  139. auto-dialect: true
  140. helper-dialect: mysql
  141. reasonable: true
  142. params: count=countSql
  143. ```
  144. ## UserMapper.java
  145. ```java
  146. /**
  147. * <p>
  148. * UserMapper
  149. * </p>
  150. *
  151. * @author yangkai.shen
  152. * @date Created in 2018-11-08 14:15
  153. */
  154. @Component
  155. // 注意:这里的Mapper是tk.mybatis.mapper.common.Mapper包下的
  156. public interface UserMapper extends Mapper<User>, MySqlMapper<User> {
  157. }
  158. ```
  159. ## UserMapperTest.java
  160. ```java
  161. /**
  162. * <p>
  163. * UserMapper 测试
  164. * </p>
  165. *
  166. * @author yangkai.shen
  167. * @date Created in 2018-11-08 14:25
  168. */
  169. @Slf4j
  170. public class UserMapperTest extends SpringBootDemoOrmMybatisMapperPageApplicationTests {
  171. @Autowired
  172. private UserMapper userMapper;
  173. /**
  174. * 测试通用Mapper - 保存
  175. */
  176. @Test
  177. public void testInsert() {
  178. String salt = IdUtil.fastSimpleUUID();
  179. 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();
  180. userMapper.insertUseGeneratedKeys(testSave3);
  181. Assert.assertNotNull(testSave3.getId());
  182. log.debug("【测试主键回写#testSave3.getId()】= {}", testSave3.getId());
  183. }
  184. /**
  185. * 测试通用Mapper - 批量保存
  186. */
  187. @Test
  188. public void testInsertList() {
  189. List<User> userList = Lists.newArrayList();
  190. for (int i = 4; i < 14; i++) {
  191. String salt = IdUtil.fastSimpleUUID();
  192. 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();
  193. userList.add(user);
  194. }
  195. int i = userMapper.insertList(userList);
  196. Assert.assertEquals(userList.size(), i);
  197. List<Long> ids = userList.stream().map(User::getId).collect(Collectors.toList());
  198. log.debug("【测试主键回写#userList.ids】= {}", ids);
  199. }
  200. /**
  201. * 测试通用Mapper - 删除
  202. */
  203. @Test
  204. public void testDelete() {
  205. Long primaryKey = 1L;
  206. int i = userMapper.deleteByPrimaryKey(primaryKey);
  207. Assert.assertEquals(1, i);
  208. User user = userMapper.selectByPrimaryKey(primaryKey);
  209. Assert.assertNull(user);
  210. }
  211. /**
  212. * 测试通用Mapper - 更新
  213. */
  214. @Test
  215. public void testUpdate() {
  216. Long primaryKey = 1L;
  217. User user = userMapper.selectByPrimaryKey(primaryKey);
  218. user.setName("通用Mapper名字更新");
  219. int i = userMapper.updateByPrimaryKeySelective(user);
  220. Assert.assertEquals(1, i);
  221. User update = userMapper.selectByPrimaryKey(primaryKey);
  222. Assert.assertNotNull(update);
  223. Assert.assertEquals("通用Mapper名字更新", update.getName());
  224. log.debug("【update】= {}", update);
  225. }
  226. /**
  227. * 测试通用Mapper - 查询单个
  228. */
  229. @Test
  230. public void testQueryOne(){
  231. User user = userMapper.selectByPrimaryKey(1L);
  232. Assert.assertNotNull(user);
  233. log.debug("【user】= {}", user);
  234. }
  235. /**
  236. * 测试通用Mapper - 查询全部
  237. */
  238. @Test
  239. public void testQueryAll() {
  240. List<User> users = userMapper.selectAll();
  241. Assert.assertTrue(CollUtil.isNotEmpty(users));
  242. log.debug("【users】= {}", users);
  243. }
  244. /**
  245. * 测试分页助手 - 分页排序查询
  246. */
  247. @Test
  248. public void testQueryByPageAndSort() {
  249. initData();
  250. int currentPage = 1;
  251. int pageSize = 5;
  252. String orderBy = "id desc";
  253. int count = userMapper.selectCount(null);
  254. PageHelper.startPage(currentPage, pageSize, orderBy);
  255. List<User> users = userMapper.selectAll();
  256. PageInfo<User> userPageInfo = new PageInfo<>(users);
  257. Assert.assertEquals(5, userPageInfo.getSize());
  258. Assert.assertEquals(count, userPageInfo.getTotal());
  259. log.debug("【userPageInfo】= {}", userPageInfo);
  260. }
  261. /**
  262. * 测试通用Mapper - 条件查询
  263. */
  264. @Test
  265. public void testQueryByCondition() {
  266. initData();
  267. Example example = new Example(User.class);
  268. // 过滤
  269. example.createCriteria().andLike("name", "%Save1%").orEqualTo("phoneNumber", "17300000001");
  270. // 排序
  271. example.setOrderByClause("id desc");
  272. int count = userMapper.selectCountByExample(example);
  273. // 分页
  274. PageHelper.startPage(1, 3);
  275. // 查询
  276. List<User> userList = userMapper.selectByExample(example);
  277. PageInfo<User> userPageInfo = new PageInfo<>(userList);
  278. Assert.assertEquals(3, userPageInfo.getSize());
  279. Assert.assertEquals(count, userPageInfo.getTotal());
  280. log.debug("【userPageInfo】= {}", userPageInfo);
  281. }
  282. /**
  283. * 初始化数据
  284. */
  285. private void initData() {
  286. testInsertList();
  287. }
  288. }
  289. ```
  290. ## 参考
  291. - 通用Mapper官方文档:https://github.com/abel533/Mapper/wiki/1.integration
  292. - pagehelper 官方文档:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md