mybatis-generator-maven-plugin使用

前提说明

  • 数据库:MYSQL57
  • Mybatis : http://mybatis.org/generator/index.html

操作说明

引入插件

<plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>${mysql-connector-java.version}version>
                    dependency>
                dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <phase>packagephase>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    
                    <verbose>trueverbose>
                    
                    <overwrite>trueoverwrite>
                    
                    <configurationFile>src/test/resources/config/generator-config.xmlconfigurationFile>
                configuration>
            plugin>
        plugins>

创建 generator 的配置文件

注意:配置文件名要为:application.properties ,同时要放在 resources/config/application.properties,想要修改请详见下面的配置xml信息。

# jdbc
db.driverLocation=D:\_tools\maven_resprosity\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar
#mysql??
spring.datasource.url=jdbc:mysql://192.168.2.11:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#alibaba DruidDataSource ??
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=30
# ?????????????
spring.datasource.maxWait=60000
#Druid????
spring.datasource.druid.WebStatFilter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.StatViewServlet.loginUsername=druid
spring.datasource.druid.StatViewServlet.loginPassword=druid

# Generator targetProject
generator.targetProject=src/main/java
# pojo
generator.pojo=cn.com.bsfit.pojo
# mapper
generator.mapper=cn.com.bsfit.mapper
# mapper path ,文件夹名,默认mapper,可以去mapper中修改
generator.maper.sqlMap-targetPackage=src/main/resources

逆向配置模板信息

注意:
  其他的信息可以不变,但是我们要修改 table 标签,添加我们要逆向的数据表。


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="./config/application.properties"/>

    
    <classPathEntry location="${db.driverLocation}" />

    
    
    
    <context id="default" targetRuntime="MyBatis3">
        
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        
        <commentGenerator>
            <property name="suppressDate" value="true" />
            
            <property name="suppressAllComments" value="true" />

        commentGenerator>

        
        <jdbcConnection
                driverClass="${spring.datasource.driver-class-name}"
                connectionURL="${spring.datasource.url}"
                userId="${spring.datasource.username}"
                password="${spring.datasource.password}">
        jdbcConnection>


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

        
        
        <javaModelGenerator targetPackage="${generator.pojo}"
                            targetProject="${generator.targetProject}">
            
            
            <property name="enableSubPackages" value="false" />
            
            <property name="constructorBased" value="true" />
            
            <property name="trimStrings" value="true" />
            
            <property name="immutable" value="false" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="${generator.maper.sqlMap-targetPackage}">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${generator.mapper}"
                             targetProject="${generator.targetProject}">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>

        
        
        <table tableName="test_01"
               domainObjectName="Course"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
            
            <generatedKey column="id" sqlStatement="JDBC">generatedKey>
        table>

    context>
generatorConfiguration>

逆向生产

mybatis-generator-maven-plugin使用_第1张图片
自此生成了我们需要的表的 pojomapper.xmlmapper.java

你可能感兴趣的:(mybatis,java,逆向工程,maven,idea)