SpringBoot使用tk.mybatis整合Mybatis

SpringBoot使用tk.mybatis整合Mybatis

  • pom.xml
 <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.0.2version>
        dependency>
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.2.5version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.boot.HelloSpringBootApplicationmainClass>
                configuration>
            plugin>

            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>${mysql.version}version>
                    dependency>
                    <dependency>
                        <groupId>tk.mybatisgroupId>
                        <artifactId>mapperartifactId>
                        <version>3.4.4version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>
  • application.yml
spring:
  datasource:
    druid:
      url: jdbc:mysql://172.18.29.220:3308/myshop ?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: qqhzn.000
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.jdbc.Driver
  mybatis:
    type-aliases-package: com.qian.springbootthymeleaf.entity #实体类的存放路径
    mapper-locations: classpath:mapper/*.xml    #mapper.xml的存放路径
  • jdbc.properties
#============================#
#==== Database settings ====#
#============================#
# JDBC
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://172.18.29.220:3308/myshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=qqhzn.000
  • java…/tk.mybatis/MyMapper.java
package com.qian.springbootthymeleaf.tk.mybatis;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
     
}
  • resource/generator/generatorConfig.xml



<generatorConfiguration>
    
    <properties resource="jdbc.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="com.qian.springbootthymeleaf.tk.mybatis.MyMapper"/>
        plugin>

        
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.username}"
                password="${jdbc.password}">
        jdbcConnection>

        
        <javaModelGenerator targetPackage="com.qian.springbootthymeleaf.entity" targetProject="src/main/java"/>

        
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

        
        <javaClientGenerator
                targetPackage="com.qian.springbootthymeleaf.Dao"
                targetProject="src/main/java"
                type="XMLMAPPER"/>

        
        <table catalog="myshop" tableName="%">
            
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        table>
    context>
generatorConfiguration>

你可能感兴趣的:(Spring,mybatis,spring,springBoot)