Spring-Boot integrates mybatis(1), using the default database connection pool

Posted by vietnamese on Wed, 17 Jul 2019 21:39:22 +0200

Recently, I've been busy, I haven't written a blog for a long time. Sorry, the springboot project used JPA standard. It was implemented by hibernate. The latest trend is to try to integrate mybatis with springboot. So I found the official documents to configure it. Here's the text.

 

First of all, introduce the development environment:

  1. jdk version is 1.8
  2. springboot version is 1.4.1
  3. The development tool is intellij idea

 

First, we introduce mybatis dependency and add the following to the project's pom file:

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.1.1</version>
</dependency>

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After introducing dependencies, execute the following SQL in the database

create table city (id int primary key auto_increment, name varchar, state varchar, country varchar);
insert into city (name, state, country) values ('San Francisco', 'CA', 'US');  

The above is to create a table and insert a piece of data for us to test whether it is executed or not.

 

Then we create a mapper

 

 

//mapper annotation
@Mapper
public interface CityMapper {
 
    //SQL annotations
    @Select("SELECT * FROM CITY WHERE state = #{state}")
    City findByState(@Param("state") String state);

}

 

PS Note Description:

    @Mapper This means that this is a mapper, similar to the mapper previously configured in xml, except that the annotations used here do not use xml. If they are not used in the default path, they also need to be used with the @MapperScan annotation, otherwise the path of @Mapper annotation will not be scanned.

    @Select SQL annotations, which contain SQL to be executed, such as @Update, etc.

@ Param Parameter annotation for SQL parameter injection

 

 

After creating mapper, we need to create a mapping entity class for mapping between objects and tables. We just need to create a normal java object.

public class City {

	private Long id;

	private String name;

	private String state;

	private String country;

    .... getterAndsetter

}

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

mapper with model We've all written it.,Let's configure the database connection and so on.,This time we use properties Configuration,The following is the configuration(These contents,You must all know that.,I'm not writing notes anymore.),The default database connection pool is:

org.apache.tomcat.jdbc.pool.DataSource

 

spring.datasource.url = jdbc:mysql://localhost:3306/springboot
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
//If you want to replace other database connection pools, add the following configuration
#spring.datasource.type = package path of database connection pool

 

 

 

Because my mapper is not under the default package, but under other packages, I also need to add the @MapperScan annotation on the Startup Class of Applicaction, the code is as follows:

 

@SpringBootApplication
@MapperScan(basePackages = "com.demo.mybatisDemo")
public class DemoApplication{

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

 

Basically all the above configurations, let's write a unit test to test it.

 

The test code is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MybatisTest {

	@Autowired
	private CityMapper cityMapper;

	private Gson gson = new Gson();

	@Test
	public void mybatisMethod(){
		City cn = cityMapper.findByState("CA");

		System.out.println(gson.toJson(cn));
	}

}

 

 

Then you will find that the implementation is successful, indicating that the configuration is successful. This is a simple example of integrating mybatis. You can refer to this for your own configuration.

 

Here is an official example. If you are interested in it, you can check it by connecting yourself.

PS:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

 

At this point, the article is over!

Above all, the results of my test may be different or wrong. You are welcome to make corrections.

Welcome to reprint, please indicate the source and author, thank you!

Topics: Spring Mybatis SQL Database