现在druid已经与springboot整合了,只需要引入druid-spring-boot-starter即可轻松使用druid数据库连接池。本文介绍使用druid-spring-boot-starter以及tk.mybatis配合使用的相关配置。引入依赖:
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- tk.mybatis mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.starter.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- druid starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
application.properties文件的配置如下:
spring.application.name=lovecto
#参考https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://172.17.0.2:3306/test
spring.datasource.druid.username=root
spring.datasource.druid.password=aoliday
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=60000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.filters=stat
# mybatis 配置
mybatis.type-aliases-package=cn.lovecto.mybatis.model
mybatis.mapper-locations=classpath:mapper/*.xml
# 通用 Mapper 配置
mapper.mappers=cn.lovecto.mybatis.mapper.LoveCtoMapper
mapper.not-empty=false
mapper.identity=MYSQL
# 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
使用的是mysql,创建一个LoveCtoMapper接口继承自tk.mybatis.mapper.common.Mapper
package cn.lovecto.mybatis.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface LoveCtoMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
创建一个mysql数据库表:
CREATE TABLE `country` (
`Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`countryname` varchar(255) DEFAULT NULL COMMENT '名称',
`countrycode` varchar(255) DEFAULT NULL COMMENT '代码',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=184 DEFAULT CHARSET=utf8 COMMENT='国家信息';
使用maven插件利器mybatis-generator-maven-plugin自动生成tk.mybatis代码。生成的代码如下:
自动生成的Country数据模型类:
package cn.lovecto.mybatis.model;
import javax.persistence.*;
public class Country {
/**
* 主键
*/
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* 名称
*/
private String countryname;
/**
* 代码
*/
private String countrycode;
/**
* 获取主键
*
* @return Id - 主键
*/
public Integer getId() {
return id;
}
/**
* 设置主键
*
* @param id 主键
*/
public void setId(Integer id) {
this.id = id;
}
/**
* 获取名称
*
* @return countryname - 名称
*/
public String getCountryname() {
return countryname;
}
/**
* 设置名称
*
* @param countryname 名称
*/
public void setCountryname(String countryname) {
this.countryname = countryname;
}
/**
* 获取代码
*
* @return countrycode - 代码
*/
public String getCountrycode() {
return countrycode;
}
/**
* 设置代码
*
* @param countrycode 代码
*/
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
自动生成的CountryMapper接口:
package cn.lovecto.mybatis.dao.mapper;
import cn.lovecto.mybatis.mapper.LoveCtoMapper;
import cn.lovecto.mybatis.model.Country;
public interface CountryMapper extends LoveCtoMapper<Country> {
}
自动生成的CountryMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.lovecto.mybatis.dao.mapper.CountryMapper" >
<resultMap id="BaseResultMap" type="cn.lovecto.mybatis.model.Country" >
<!--
WARNING - @mbg.generated
-->
<id column="Id" property="id" jdbcType="INTEGER" />
<result column="countryname" property="countryname" jdbcType="VARCHAR" />
<result column="countrycode" property="countrycode" jdbcType="VARCHAR" />
</resultMap>
</mapper>
应用启动类:
package cn.lovecto.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
@EnableAutoConfiguration
@ComponentScan(basePackages = "cn.lovecto.mybatis")
@MapperScan(basePackages = "cn.lovecto.mybatis.dao.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类:
package cn.lovecto.mybatis.dao.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import cn.lovecto.mybatis.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class CountryMapperTest {
@Autowired
private CountryMapper countryMapper;
@Test
public void test(){
System.out.println(countryMapper.selectCount(null));
}
}
运行测试类,能够返回数据,OK啦!项目截图: