springboot-2-整合myBatis及druid连接池

整合mybatis

1、引入mybatis的maven包

  
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis-spring-boot}version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql-connector}version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.5version>
        dependency>

2、application.properties配置

server.port=8089
server.servlet.context-path=/first
spring.profiles.active=dev

#mybatis
mybatis.typeAliasesPackage=com.example.springboot1.dao
mybatis.mapperLocations=classpath:mapper/*.xml
spring.datasource.url=jdbc:mysql://10.6.80.147:3306/j_test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=skSB@123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

3、mybatis配置文件

mybatis-config.xml

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>
    <properties>
        <property name="dialect" value="mysql" />
    properties>
    <settings>
        
        <setting name="cacheEnabled" value="true" />
        
        <setting name="lazyLoadingEnabled" value="true" />
        
        <setting name="multipleResultSetsEnabled" value="true" />
        
        <setting name="useColumnLabel" value="true" />
        
        <setting name="useGeneratedKeys" value="false" />
        
        <setting name="defaultExecutorType" value="SIMPLE" />
        
        <setting name="defaultStatementTimeout" value="25000" />
    settings>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <property name="offsetAsPageNum" value="true" />
            <property name="rowBoundsWithCount" value="true" />
            <property name="pageSizeZero" value="true" />
            <property name="reasonable" value="true" />
        plugin>
    plugins>
configuration>

4、配置mapper扫描器

springboot只需,在启动类上加注解

@SpringBootApplication
@MapperScan("com.example.springboot1.dao")
@EnableCaching
public class Springboot1Application {

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

}

到这里配置就全部完成了,可以开始使用了

5、dao层

这里dao层我们就不用了传统的xml映射的方式了。用一下mybatis提供的@Mapper注解,这使得我们可以不用去写烦人的xml了。

@Mapper
@Component
public interface StudentMapper {
    @Select("select * from student where name = #{name}")
    public Student find(String name);
}

 

你可能感兴趣的:(springboot-2-整合myBatis及druid连接池)