3

引入starter

修改pom.xml文件,整合mybatis需要如下的包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!--mybatis的starter-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!--druid连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

durid连接池配置

修改yml文件

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sff_test
    driver-class-name: com.mysql.jdbc.Driver
    # 指定自己使用的数据源
    type: com.alibaba.druid.pool.DruidDataSource

    # DruidDataSource 其他属性配置
    druid:
     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
     maxPoolPreparedStatementPerConnectionSize: 20
     useGlobalDataSourceStat: true
     filter:
      stat:
       enabled: true
       log-slow-sql: true
      wall:
       enabled: true

注入duird配置

@Configurable
public class DruidConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSourceTwo() {
        return DruidDataSourceBuilder.create().build();
    }
    /**
     * 配置一个管理后台的Servlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> initParams = new HashMap<>();

        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "admin");
        initParams.put("allow", "");//默认就是允许所有访问

        bean.setInitParameters(initParams);
        return bean;
    }
    /**
     * 配置一个web监控的filter
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String, String> initParams = new HashMap<>();
        //通过 localhost:8080/druid/ 可以访问到监控后台
        initParams.put("exclusions", "*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }
}

配置mybatis组件

使用mybatis注解

定义mapper接口

@Mapper
public interface DeptMapper {

    @Select("select * from dept")
    List<Dept> selectAll();
}

注入mybatis相关配置

public class MybatisConfig {

    //定制mybatis配置
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(Configuration configuration) {
                //开启驼峰命令
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}
  1. 开启驼峰命名,如果不开启会导致数据库中的这种dept_name等字段无法转换为bean中的deptName字段
  2. 在我们的mapper接口很多的时候,比如说有多个类似 DeptMapper时,如果每个mapper都写@Mapper注解很麻烦,此时可以在MybatisConfig配置类上使用@MapperScan(value = "com.sff.spring.boot.mybatis.dao")这个注解,去掉@Mapper注解即可

使用mapper.xml文件

定义mapper接口

public interface EmpMapper {
    List<Emp> selectAll();
}

定义mapper.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="com.sff.spring.boot.mybatis.dao.EmpMapper">
    <select id="selectAll" resultType="Emp">
        select * from emp
    </select>
</mapper>

注意:使用mapper的xml文件时,记得将mapper接口注入的容器中,使用@Mapper`或者@MapperScan注解

定义mybatis的全局配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!--开启驼峰命名-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <!--实体bean路径-->
        <package name="com.sff.spring.boot.mybatis.domain"/>
    </typeAliases>
</configuration>

修改yml文件,指定mybatis配置、mapper文件路径

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

自定义starter

其他参考


一只小小鸟
144 声望25 粉丝

如何做一个深层次的思考者,从简单开始、从记录开始。