前言
本文主要给大家介绍了关于spring boot整合mybatis利用Mysql实现主键UUID的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
实现
基础项目的pom.xml部分代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<properties> <java.version> 1.8 </java.version> </properties> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 9 .RELEASE</version> </parent> <dependencies> <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--常用库依赖--> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.6 </version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version> 23.0 </version> </dependency> <!--MySQL JDBC驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--MyBatis 及 插件依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version> 1.3 . 1 </version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version> 3.4 . 5 </version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version> 3.4 . 2 </version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version> 4.2 . 1 </version> </dependency> <!--阿里 FastJson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 44 </version> </dependency> <!--阿里 Druid Spring Boot Starter依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version> 1.1 . 6 </version> </dependency> </dependencies> </project> |
具体的mybatis配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName( "sqlSessionFactoryBean" ); mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); //配置通用Mapper,详情请查阅官方文档 Properties properties = new Properties(); properties.setProperty( "mappers" , MAPPER_INTERFACE_REFERENCE); properties.setProperty( "notEmpty" , "true" ); //insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != '' properties.setProperty( "IDENTITY" , "SELECT UUID()" ); //使用UUID作為主鍵 properties.setProperty( "ORDER" , "BEFORE" ); //将查询主键作为前置操作 mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; } |
实体如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class User { @Id @Column (name = "ID" ) @GeneratedValue (strategy = GenerationType.IDENTITY) private String id; @Column (name = "CREATED_BY" ) private String createdBy; @DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss" ) @Column (name = "CREATED_DATE" ) private Date createdDate; @Column (name = "LAST_MODIFIED_BY" ) private String lastModifiedBy; @DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss" ) @Column (name = "LAST_MODIFIED_DATE" ) private Date lastModifiedDate; |
下面这一行代码是为了解决spring MVC在入参是Date类型时或出现异常的问题
1
|
@DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss" ) |
测试
配置文件配置输出mybatis的sql日志
1
|
logging.level.com.你的dao或mapper包=debug |
执行对应插入操作观察控制台
1
2
3
4
5
|
2018 - 03 - 06 17 : 59 : 22.612 DEBUG 6208 --- [nio- 8686 -exec- 1 ] XXXXX: ==> Executing: SELECT UUID() 2018 - 03 - 06 17 : 59 : 22.629 DEBUG 6208 --- [nio- 8686 -exec- 1 ] XXXXX: <== Total: 1 2018 - 03 - 06 17 : 59 : 22.651 DEBUG 6208 --- [nio- 8686 -exec- 1 ] XXXXX: ==> Preparing: INSERT INTO user ( ID,USER_NAME,NICK_NAME,PWD_SALT,PWD_LOGIN,CREATED_BY,CREATED_DATE,LAST_MODIFIED_BY,LAST_MODIFIED_DATE,ACCOUNT_TYPE,ACCOUNT_LOCKED,ONLINE_STATUS,GROUP_ID,GROUP_NAME ) VALUES( ?,?,?,?,? ) 2018 - 03 - 06 17 : 59 : 22.665 DEBUG 6208 --- [nio- 8686 -exec- 1 ] XXXXX: ==> Parameters: 0b97ad1b- 2125 -11e8-9b42-704d7b7036d3(String), 2 (String), 2018 - 01 - 01 01 : 07 : 05.0 (Timestamp), 2 (String), 2018 - 01 - 01 01 : 07 : 05.0 (Timestamp) 2018 - 03 - 06 17 : 59 : 22.670 DEBUG 6208 --- [nio- 8686 -exec- 1 ] XXXXX: <== Updates: 1 |
可以看到,首先执行了查询UUID的操作,然后将执行结果作为插入的主键执行插入操作.
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://segmentfault.com/a/1190000013558552