Spring Boot integrates Mybatis (1)

New spring boot project, dependent



Using Druid connection pool to introduce dependency

		<!--Introduce druid-->
		<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->

Writing the Druid configuration class

You can also configure it directly in the configuration file

public class DruidConfig {

    //Associate configuration items in the yml file
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid() {

        return  DruidDataSourceBuilder.create().build();


    //Configure druid monitoring  
//    1. Configure the servlet in the background of management
    public ServletRegistrationBean startViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        Map<String, String> map = new HashMap<>();
        map.put("loginUsername", "admin");
        map.put("loginPassword", "123456");
        map.put("allow", "");//Allow all access by default
//        map.put("deny" , "");
        return bean;

    //2. Configure a filter for web Monitoring
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String, String> map = new HashMap<>();
        map.put("exclusions", "*.js,*.css,/druid/*");

        return bean;


Related properties configured in the configuration file

#   The application.yml file is configured in the format of YML file

#   Basic configuration of data source
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://
#    type: com.alibaba.druid.pool.DruidDataSource
#   Data source other configuration
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   Configure the filters intercepted by monitoring statistics. After the filters are removed, the monitoring interface sql cannot be counted. The 'wall' is used for the firewall
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

Configuration class or configuration file can be used for mybatis related configuration

public class MybtisConfig {

    //Open hump
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            public void customize(Configuration configuration) {

mapper.class file

public interface DepartmentMapper {

    @Select("select * from department")
    public List<Department> getList();


@MapperScan(value = "com.wgm")
public class BootmybatisApplication {

Note that using the @ Mapper annotation to scan the current interface or using the @ MapperScan annotation scan package will be added to the container

In this way, sql statements can be written directly on Methods

Or use the configuration file related mode, i.e. traditional configuration of mybatis-config.xml, / mapper / *. XML

Configuration file needs to be modified

  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

File directory

Keywords: Druid Spring Mybatis MySQL

Added by trexx on Fri, 03 Jan 2020 23:44:44 +0200