您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關spring boot整合mybatis如何使用Mysql實現主鍵UUID的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
實現
基礎項目的pom.xml部分代碼如下
<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配置如下
@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; }
實體如下
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類型時或出現異常的問題
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
測試
配置文件配置輸出mybatis的sql日志
logging.level.com.你的dao或mapper包=debug
執行對應插入操作觀察控制臺
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的操作,然后將執行結果作為插入的主鍵執行插入操作.
感謝各位的閱讀!關于“spring boot整合mybatis如何使用Mysql實現主鍵UUID”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。