Preface
This article mainly introduces to you the relevant content about spring boot integrating mybatis using Mysql to implement primary key UUID. We will share it for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.
accomplish
The pom.xml part of the basic project is as follows
<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 Dependencies-> <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> <!--Frequently used library dependencies--> <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 Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--MyBatis and plug-in dependencies--> <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> <!--Ali FastJson Dependency--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.44</version> </dependency> <!--Ali Druid Spring Boot Starter Dependency--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.6</version> </dependency> </dependencies></project>
The specific mybatis configuration is as follows
@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mappperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); mappperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); //Configure a general mapper, please refer to the official document for details Properties properties = new Properties(); properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE); properties.setProperty("notEmpty", "true");// whether to determine the string type, !='', that is, whether to append and str != '' properties.setProperty("IDENTITY", "SELECT UUID()");//Use UUID as the main properties.setProperty("ORDER","BEFORE");//Prefer query primary key as a pre-operation mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }The entity is as follows
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;The following line of code is to solve the problem of spring MVC's exception when the parameter entry is of Date type.
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
test
Configuration file configuration output mybatis sql log
logging.level.com.your dao or mapper package=debug
Perform the corresponding insertion operation to observe the console
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: 12018-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
You can see that the operation of querying the UUID is first performed, and then the result of the execution is used as the primary key of the insertion is performed.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.