mybatis-generator-maven-plugin使用

前提说明

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

操作说明

引入插件

<plugins>
            <!-- MyBatis 逆向工程 插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql-connector-java.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 配置文件 -->
                    <configurationFile>src/test/resources/config/generator-config.xml</configurationFile>
                </configuration>
            </plugin>
        </plugins>

创建 generator 的配置文件

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

db:
  driverLocation: D:_toolsmaven_resprositymysqlmysql-connector-java8.0.15mysql-connector-java-8.0.15.jar
generator:
  maper:
    sqlMap-targetPackage: src/main/resources
  mapper: cn.com.bsfit.mapper
  pojo: cn.com.bsfit.pojo
  targetProject: src/main/java
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    druid:
      StatViewServlet:
        loginPassword: druid
        loginUsername: druid
      WebStatFilter:
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
      initialSize: 5
      maxActive: 30
      minIdle: 5
    maxWait: 60000
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://192.168.2.11:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
    username: root

在这里插入图片描述

逆向配置模板信息

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

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

    <!-- 指定数据库驱动的jdbc驱动jar包的位置 -->
    <classPathEntry location="${db.driverLocation}" />

    <!-- context 是逆向工程的主要配置信息 -->
    <!-- id:起个名字 -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">
        <!--覆盖生成XML文件-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!--optional,旨在创建class时,对注释进行控制-->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />

        </commentGenerator>

        <!--jdbc的数据库连接-->
        <jdbcConnection
                driverClass="${spring.datasource.driver-class-name}"
                connectionURL="${spring.datasource.url}"
                userId="${spring.datasource.username}"
                password="${spring.datasource.password}">
        </jdbcConnection>


        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
         NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaTypeResolver>

        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="${generator.pojo}"
                            targetProject="${generator.targetProject}">
            <!-- 是否允许子包,默认false -->
            <!--官网文档介绍:For example, suppose a table MYTABLE in schema MYSCHMA.
              Also suppose that the targetPackage attribute is set to "com.mycompany".
              If this property is true, the generated objects
              for the table will be placed in the package "com.mycompany.myschema".
              If the property is false, the generated objects will
              be placed in the "com.mycompany" schema.
              The default value is false.-->
            <property name="enableSubPackages" value="false" />
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true" />
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true" />
            <!-- 建立model对象是否不可改变 即生成的model对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false" />
        </javaModelGenerator>

        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="${generator.maper.sqlMap-targetPackage}">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${generator.mapper}"
                             targetProject="${generator.targetProject}">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类 -->
        <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>

逆向生产

在这里插入图片描述
自此生成了我们需要的表的 pojomapper.xmlmapper.java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值