spring boot整合mybatis利用Mysql实现主键UUID的方法
程序员文章站
2022-07-28 15:04:14
前言
本文主要给大家介绍了关于spring boot整合mybatis利用mysql实现主键uuid的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介...
前言
本文主要给大家介绍了关于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的操作,然后将执行结果作为插入的主键执行插入操作.
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: Java中将String类型依照某个字符分割成数组的方法
下一篇: Java判断数字位数的方法总结