SpringBoot2.0應用(五):SpringBoot2.0整合MyBatis

貳級天災發表於2018-11-25

如何整合MyBatis

1、pom依賴

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- 分頁外掛 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!--mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.3</version>
        </dependency>
        <!-- alibaba的druid資料庫連線池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
複製程式碼

2、新增配置

spring.datasource.name=mysql_test
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#druid相關配置
spring.datasource.druid.filters=stat
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
#基本屬性
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
spring.datasource.druid.username=root
spring.datasource.druid.password=1234
#配置初始化大小/最小/最大
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
#獲取連線等待超時時間
spring.datasource.druid.max-wait=60000
#間隔多久進行一次檢測,檢測需要關閉的空閒連線
spring.datasource.druid.time-between-eviction-runs-millis=60000
#一個連線在池中最小生存的時間
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
#開啟PSCache,並指定每個連線上PSCache的大小。oracle設為true,mysql設為false。分庫分表較多推薦設定為false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#Mapper路徑
mybatis.mapper-locations=classpath=mapper/*.xml
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
複製程式碼

3、自動生成Mapper

新增Mybatis的自動生成外掛

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.0</version>
                    </dependency>
                </dependencies>
            </plugin>
複製程式碼

配置Mybatis的generatorConfig

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <properties resource="application.properties"/>
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="sample.mybatis.mapper.MyMapper"/>
        </plugin>
        <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true"
                        driverClass="com.mysql.jdbc.Driver"
                        userId="root"
                        password="1234">
        </jdbcConnection>
        <javaModelGenerator targetPackage="sample.mybatis.entity" targetProject="src/main/java">
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <javaClientGenerator targetPackage="sample.mybatis.dao" targetProject="src/main/java"
                             type="XMLMAPPER"/>
        <table tableName="city">
            <!--mysql 配置-->
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>
複製程式碼

執行外掛會自動生成實體Bean,Mapper介面和對應的xml檔案。

5、寫個簡單的Controller觸發呼叫

@RestController
public class CityController {

    @Autowired
    private CityMapper cityMapper;

    @GetMapping("/")
    public List<City> index() {
        PageHelper.startPage(0, 3);
        List<City> cities = this.cityMapper.selectAll();
        return cities;
    }

}
複製程式碼

啟動專案後通過PostMan訪問:http://localhost:8080/

SpringBoot2.0應用(五):SpringBoot2.0整合MyBatis

原始碼地址:GitHub


本篇到此結束,如果讀完覺得有收穫的話,歡迎點贊、關注、加公眾號【貳級天災】,查閱更多精彩歷史!!!

SpringBoot2.0應用(五):SpringBoot2.0整合MyBatis

相關文章