通用mapper就是指的是  tk.mybatis  包下的。这个是通用mapper就是说自动生成的dao层需要继承这个框架提供的mapper类。而我们之前用的org.mybatis这个最开始是普通的。

就是说最开始我们用这个会给我们生成dao接口(不继承其他类),mapper文件(有许多方法增删改查)和实体类。我们感觉很麻烦,所以就出来了通用mapper,只需要继承mapper类就行,自动有增删改查的方法,类似于jpa。代码很简洁。我们这里只讲一下如何自动生成。

坑很多,比如说springboot2.0以上就会出问题,换成1.5就可以。要按照教程来。肯定会成功的。

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>tk-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>tk-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- mybatis 逆向生成工具  -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
        <!--mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--generator配置文件所在位置-->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.36</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

generatorConfig.properties

# Generator
generator.targetProject=src/main/java
generator.mappers=com.example.demo.baseMapper.IBaseMapper
generator.javaModel-targetPackage=com.example.demo.domain
generator.sqlMap-targetPackage=com.example.demo.dao
generator.javaClient-targetPackage=com.example.demo.dao

#mysql
driverClass=com.mysql.jdbc.Driver
connectionURL=jdbc:mysql://localhost:3306/video?serverTimezone=UTC
userId=root
password=root

generatorConfig.xml

<?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="generatorConfig.properties"/>
    <!-- 必须 指定生成一组对象的环境 -->
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <!--分隔符 -->
        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 通用mapper插件 -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <!-- 此处的 MyMapper注意一定不能被扫描到-->
            <property name="mappers" value="${generator.mappers}"/>
        </plugin>
        <!-- 注释 规则-->
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}" targetProject="${generator.targetProject}"/>
        <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}" targetProject="${generator.targetProject}"/>
        <javaClientGenerator targetPackage="${generator.javaClient-targetPackage}" targetProject="${generator.targetProject}"
                             type="XMLMAPPER"/>
        <table tableName="bgm">
            <!--mysql 配置-->
            <generatedKey column="id" sqlStatement="Mysql" />
            <!--oracle 配置-->
            <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
        </table>
    </context>
</generatorConfiguration>

IBaseMapper(让生成的mapper接口都继承这个类)

package com.example.demo.baseMapper;

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

/**
 * Created by 敲代码的卡卡罗特
 * on 2018/11/1 22:04.
 */
public interface IBaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

就ok了。

04-20 15:43