查看: 179|回复: 0

[Java学习] spring boot整合mybatis利用Mysql实现主键UUID的方法

发表于 4 天前

前言

本文主要给大家介绍了关于spring boot整合mybatis利用Mysql实现主键UUID的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

实现

基础项目的pom.xml部分代码如下

  1. <properties>
  2. <java.version>1.8</java.version>
  3. </properties>
  4. <!-- Inherit defaults from Spring Boot -->
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.9.RELEASE</version>
  9. </parent>
  10. <dependencies>
  11. <!--Spring Boot依赖-->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-jdbc</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. <!--常用库依赖-->
  26. <dependency>
  27. <groupId>commons-codec</groupId>
  28. <artifactId>commons-codec</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.apache.commons</groupId>
  32. <artifactId>commons-lang3</artifactId>
  33. <version>3.6</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.google.guava</groupId>
  37. <artifactId>guava</artifactId>
  38. <version>23.0</version>
  39. </dependency>
  40. <!--MySQL JDBC驱动-->
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. <scope>runtime</scope>
  45. </dependency>
  46. <!--MyBatis 及 插件依赖-->
  47. <dependency>
  48. <groupId>org.mybatis</groupId>
  49. <artifactId>mybatis-spring</artifactId>
  50. <version>1.3.1</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.mybatis</groupId>
  54. <artifactId>mybatis</artifactId>
  55. <version>3.4.5</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>tk.mybatis</groupId>
  59. <artifactId>mapper</artifactId>
  60. <version>3.4.2</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>com.github.pagehelper</groupId>
  64. <artifactId>pagehelper</artifactId>
  65. <version>4.2.1</version>
  66. </dependency>
  67. <!--阿里 FastJson依赖-->
  68. <dependency>
  69. <groupId>com.alibaba</groupId>
  70. <artifactId>fastjson</artifactId>
  71. <version>1.2.44</version>
  72. </dependency>
  73. <!--阿里 Druid Spring Boot Starter依赖-->
  74. <dependency>
  75. <groupId>com.alibaba</groupId>
  76. <artifactId>druid-spring-boot-starter</artifactId>
  77. <version>1.1.6</version>
  78. </dependency>
  79. </dependencies>
  80. </project>
复制代码

具体的mybatis配置如下

  1. @Bean
  2. public MapperScannerConfigurer mapperScannerConfigurer() {
  3. MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
  4. mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
  5. mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE);
  6. //配置通用Mapper,详情请查阅官方文档
  7. Properties properties = new Properties();
  8. properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE);
  9. properties.setProperty("notEmpty", "true");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
  10. properties.setProperty("IDENTITY", "SELECT UUID()");//使用UUID作為主鍵
  11. properties.setProperty("ORDER","BEFORE");//将查询主键作为前置操作
  12. mapperScannerConfigurer.setProperties(properties);
  13. return mapperScannerConfigurer;
  14. }
复制代码

实体如下

  1. public class User {
  2. @Id
  3. @Column(name = "ID")
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private String id;
  6. @Column(name = "CREATED_BY")
  7. private String createdBy;
  8. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  9. @Column(name = "CREATED_DATE")
  10. private Date createdDate;
  11. @Column(name = "LAST_MODIFIED_BY")
  12. private String lastModifiedBy;
  13. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  14. @Column(name = "LAST_MODIFIED_DATE")
  15. 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. 2018-03-06 17:59:22.612 DEBUG 6208 --- [nio-8686-exec-1] XXXXX: ==> Executing: SELECT UUID()
  2. 2018-03-06 17:59:22.629 DEBUG 6208 --- [nio-8686-exec-1] XXXXX: <== Total: 1
  3. 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( ?,?,?,?,? )
  4. 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)
  5. 2018-03-06 17:59:22.670 DEBUG 6208 --- [nio-8686-exec-1] XXXXX: <== Updates: 1
复制代码

可以看到,首先执行了查询UUID的操作,然后将执行结果作为插入的主键执行插入操作.

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。



回复

使用道具 举报