springboot2.1 integration mybatis-generator and general mapper

1: Add dependence

<?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>

        <! - General 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>
                    <the fork> to true</fork>
                </configuration></verbose>
            </ plugin> 

            <-! MyBatis-Generator widget -> 
            <plugin> 
                <-! Mybatis-Generator plug, and for automatically generating Mapper POJOs -> 
                <the groupId> org.mybatis.generator </ the groupId> 
                < the artifactId> MyBatis-Generator-Maven-plugin </ the artifactId> 
                <Version> 1.3.7 </ Version> 
                <configuration> 
                    <-! location profile must be changed to the location of the profile -> 
                    <configurationFile> the src / main / Resources / MyBatis-Generator / Generator-the config.xml </ configurationFile> 
                    <-! cover -> 
                    <verbose> to true 
                    <!- rewriting -> 
                    <Overwrite> to 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: Use the mybatis-generator + generic mapper can generate a model, mapper, mapper.xml file, but if not add mybatis dependent resulting file will complain ...

notice2: Use the mybatis will start with the data source or an error ...

notice3: pom dependence may be configured to rely on a version number, mapper, model like storage position ...

 

2: Adding a file db.properties mybatis-generator need to use (may not be added)

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

# plug-in location 
mapper .plugin = tk.mybatis.mapper.generator.MapperPlugin 
# generated mapper class inherits the parent class location 
mapper.Mapper = com.dev.mapper.BaseMapper

Figure:

 

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 Configuration ->
        <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}"
                        the userId="${jdbc.username}"
                        password = "$ {} jdbc.password"> 

            <-! mysql zone setting value needs to be set driver version 8.x GMT, UTC can -> 
            <Property name = "serverTimezone" value = "GMT" /> 

            < ! - solve generate tables and other database model mapper etc. -> 
            <-! model can also be solved generated inside the lack of @Id notes issue (mysql driver version 5.1 this problem can be solved fallback) -> 
            <Property name = "nullCatalogMeansCurrent" value = "to true" /> 
        </ JdbcConnection> 

        <javaTypeResolver> 
            <Property name = "forceBigDecimals" value = "to 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 Penalty for com.dev.mapper; 

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

/ ** 
 * the Created by zgq7 ON 2019 / 6/24. 
 * so generated are integrated interface mapper file 
 * / 
public  interface BaseMapper <T> the extends Mapper <T>, MySqlMapper <T>, IdsMapper <T> { 

}

 

notice1: class path to the db.properties defined as, or will be error ...

 

5: application.xml configuration

 

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=to true 
# print sql (Both are, but I prefer the latter) 
# mybatis.configuration.log -impl = org.apache.ibatis.logging.stdout.StdOutImpl
 logging.level.com.dev.mapper.repositry = Debug 

 

Here I thought a moment druid configuration, I am here just to do a simple configuration only, and not only this kind of configuration, there are other configuration methods, but this obviously is more convenient and faster.

I'll update later in the configuration druid other writing, a lot of study.

 

6: Running mybatis-generator plug-ins to generate the relevant documents.

Generating a file structure as follows:

 

7: unit testing to see whether the normal operation of Hey ~ ~

notice1: sql log also can be seen played out

notice2: unit tests are successful, great service complete la la la. . . .

 

 

Summary: learning new knowledge and understanding of new uses, in order to gain more.

Attached mybatis official website address details about xml configuration Introduction: http://www.mybatis.org/generator/configreference/table.html

 

Guess you like

Origin www.cnblogs.com/zgq7/p/11084161.html