Spring boot is a simple configuration method for configuring MySQL database connection, Hikari connection pool, and Mybatis.
This method is simple and supports MySQL database multi-database connection, Hikari connection pool, and MyBatis (including Dao and xml file location configuration ).
1. Introduce dependency in pom. xml:
<!-- Begin of DB related --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- End of DB related -->
We usedmybatis-spring-boot-starter
And let it exclude the tomcat-jdbc connection pool, so that spring-boot will find out whether HikariCP is available, the second dependency will be found, and mysql-connector will also have it.
2. Related configurations in application. yml:
Spring: profiles: active: dev datasource: driver-class-name: com. mysql. jdbc. driver username: root password: 123456 hikari: maxLifetime: 1765000 # the lifetime of a connection (in milliseconds). If the connection times out and is not used, it is released (retired). Default Value: 30 minutes, we recommend that you set maximumPoolSize to 30 seconds less than the database timeout time: 15 # maximum number of connections allowed in the connection pool. Default Value: 10; recommended formula: (core_count * 2) + inclutive_spindle_count) mybatis: mapperLocations: classpath: mapper /*. xml --- # Development Environment configuration spring: profiles: dev datasource: url: jdbc: mysql: // localhost: 3306/--- # test environment configuration spring: profiles: test datasource: url: jdbc: mysql: // 192.168.0.12: 3306/--- # configure spring: profiles: prod datasource: url: jdbc: mysql: // 192.168.0.13: 3306/in the production environment/
Datasource. the end of the url is not followed by dbName, so that multiple databases can be supported. You only need to specify the database name before the table name of the SQL statement.
3. Dao interface code:
package com.xjj.dao; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.xjj.entity.Person; @Mapper public interface PersonDAO { @Select("SELECT id, first_name AS firstName, last_name AS lastName, birth_date AS birthDate, sex, phone_no AS phoneNo" + " FROM test.t_person WHERE id=#{0};") public Person getPersonById(int id); public int insertPerson(Person person); public int updatePersonById(Person person); public int updatePersonByPhoneNo(Person person); }
You only need to use the @ Mapper annotation to locate it by Mybatis and write SQL statements on the method.
4. XML file:
Create the mapper directory under the resources directory, and then create the xml file as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. xjj. dao. PersonDAO "> <! -- Insert Database User table --> <INSERT id = "insertPerson"> insert INTO test. t_person (first_name, last_name, birth_date, sex, phone_no, update_dt) VALUES (# {firstName}, # {lastName}, # {birthDate}, # {sex }, # {phoneNo}, NOW () </insert> <update id = "updatePersonById"> UPDATE test. t_person SET first_name =#{ firstName}, last_name =#{ lastName}, birth_date =#{ birthDate}, sex =#{ sex }, phone_no =#{ phoneNo} WHERE id =#{ id} </update> <update id = "updatePersonByPhoneNo"> UPDATE test. t_person SET first_name =#{ firstName}, last_name =#{ lastName}, birth_date =#{ birthDate }, sex =#{ sex} WHERE phone_no =#{ phoneNo} </update> </mapper>
5. test:
@ Test public void dbTest () throws JsonProcessingException {Person person2 = personDAO. getPersonById (2); logger.info ("person no 2 is :{}", objectMapper. writeValueAsString (person2); person2.setFirstName ("Eight"); personDAO. updatePersonById (person2); person2 = personDAO. getPersonById (2); logger.info ("person no 2 after update is :{}", objectMapper. writeValueAsString (person2); assertThat (person2.getFirstName (), failed to ("8 "));}
Summary
The above is a simple configuration method for spring boot to configure MySQL database connection, Hikari connection pool and Mybatis. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!