其他分享
首页 > 其他分享> > springboot2.1整合mybatis-generator以及通用mapper

springboot2.1整合mybatis-generator以及通用mapper

作者:互联网

1:添加依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dev</groupId>
    <artifactId>devloper-mine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>devloper-mine</name>
    <description>dev project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>

        <!-- MyBatis Generator -->
        <!-- Java接口和实体类 -->
        <targetJavaProject>${basedir}/src/main/java</targetJavaProject>
        <targetMapperPackage>com.dev.mapper.repositry</targetMapperPackage>
        <targetModelPackage>com.dev.model</targetModelPackage>
        <!-- XML生成路径 -->
        <targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
        <targetXMLPackage>mapper</targetXMLPackage>

        <!-- 通用mapper -->
        <tk.mybatis.version>4.1.5</tk.mybatis.version>
        <persistence.version>1.0.2</persistence.version>
        <mapper.plugin>tk.mybatis.mapper.generator.MapperPlugin</mapper.plugin>
        <mapper.Mapper>tk.mybatis.mapper.common.Mapper</mapper.Mapper>
    </properties>

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

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

        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
        <!-- 通用 mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>${tk.mybatis.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
        <!-- @entity 注解 -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>${persistence.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>


    </dependencies>

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

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>

            <!--mybatis-generator插件-->
            <plugin>
                <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!--配置文件的位置  一定要改成配置文件的位置-->
                    <configurationFile>src/main/resources/mybatis-generator/generator-config.xml</configurationFile>
                    <!-- 覆盖 -->
                    <verbose>true</verbose>
                    <!-- 重写 -->
                    <overwrite>true</overwrite>
                </configuration>

                <dependencies>
                    <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>${tk.mybatis.version}</version>
                    </dependency>

                </dependencies>

            </plugin>
        </plugins>
    </build>

</project>

notice1:使用了 mybatis-generator+通用mapper 可以生成model、mapper、mapper.xml文件,但是若不加mybatis的依赖生成的文件会报错...

notice2:使用了mybatis就要配数据源否则会启动报错...

notice3:pom依赖中可以配置依赖版本号、mapper、model等的存放位置...

 

2:添加一个mybatis-generator需要用的db.properties文件(也可以不添加)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dsos_zgq?&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

#插件位置
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
#生成的mapper类继承的父类的位置
mapper.Mapper=com.dev.mapper.BaseMapper

如图:

 

3:添加generator-config.xml(要和pom文件中配置的路径一致)

<?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="mybatis-generator/db.properties"/>

    <classPathEntry
            location="D:\IntelljIdea\Maven3.5\localmaven\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar"/>

    <!-- MyBatis3Simple 可以不生成生成model衍生的example文件 -->
    <context id="DB2Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <!-- TKmybatis配置 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="${mapper.plugin}">
            <property name="mappers" value="${mapper.Mapper}"/>
        </plugin>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

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

            <!-- mysql 驱动8.x版本需要设置时区 value设置为GMT、UTC都可以 -->
            <property name="serverTimezone" value="GMT"/>

            <!-- 解决生成其他数据库的表的model和mapper等 -->
            <!-- 也可解决生成的model里面缺少@Id注解问题(此问题mysql驱动版本回退到5.1也可以解决)-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>

        <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
        <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
                             type="XMLMAPPER"/>

        <!--tableName 代表表名 domainObjectName代表实体名 schema指数据库结构-->
     <!-- 要保证表aopi存在哦~,使用%则可生成当前数据下所有表对应的model、mapper等文件 --> <table tableName="aopi"> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>

 

 

 4:编写一个通用mapper的接口BaseMapper

 

package com.dev.mapper;

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

/**
 * Created by zgq7 on 2019/6/24.
 * 让生成的mapper文件都集成该接口
 */
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, IdsMapper<T> {

}

 

notice1:类路径要与db.properties中定义的一样,不然会报错...

 

5:application.xml的配置

 

server.port=8022
server.servlet.context-path=/
#druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.url=jdbc:mysql://localhost:3306/dsos_zgq?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=root
spring.datasource.druid.password=root
#druid连接配置
spring.datasource.druid.db-type=mysql
#druid测试
spring.datasource.druid.test-on-borrow=true
#打印sql(两种都可以,但是我偏向后者了)
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
logging.level.com.dev.mapper.repositry=debug 

 

在此处我思索了一下druid的配置,我这里只是做简单配置而已,而且不止这一种配置方法,还有其他的配置方法,但是这种明显更方便更快捷。

后面我会更新druid的相关配置其他写法,多多研究一下。

 

6:运行mybatis-generator的插件,生成相关文件。

生成的文件结构如下:

 

7:进行单元测试看看是否能正常运行嘿嘿~~

notice1:可以看到sql日志也打出来了

notice2:单元测试也成功了,大功完成啦啦啦。。。。

 

 

总结:学习新的知识,了解新的用法,才能收获更多。

附mybatis官网地址关于xml配置的详情介绍:http://www.mybatis.org/generator/configreference/table.html

 

标签:mapper,springboot2.1,generator,spring,boot,druid,mybatis
来源: https://www.cnblogs.com/zgq7/p/11084161.html