maven插件自动生产mybatis代码

pom配置mybatis-generator-maven-plugin插件

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.34</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <configurationFile>src/main/resources/plugs/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

在resources下创建plugs文件夹 创建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="plugs/generator.properties"></properties>

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

    <context id="default" targetRuntime="MyBatis3">
        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}">
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.cn.wsj.security.domain.dataObject.generate"
                            targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="true"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->

        <sqlMapGenerator targetPackage="resources.mybatis.mapper.generate"
                         targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->

        <javaClientGenerator targetPackage="com.cn.wsj.security.mapper.generate"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--&lt;!&ndash; 用户信息表 &ndash;&gt;-->
        <table tableName="u_user_login" domainObjectName="UserLogin"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="u_role" domainObjectName="Role"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
        <table tableName="u_permission" domainObjectName="Permission"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

        <!-- 角色资源表 -->
        <table tableName="u_user_role" domainObjectName="UserRole"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

        <table tableName="u_role_permission" domainObjectName="RolePermission"
               enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true"
               selectByExampleQueryId="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

    </context>
</generatorConfiguration>

创建配置文件plugs/generator.properties,以实现增删改查非空判断

//指定启动类路径
jdbc.driverLocation=F:/repostitory/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar
//jdbc.driverLocation=C:/JOB/jar/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/test?zeroDateTimeBehavior=convertToNull
jdbc.userId=root
jdbc.password=abc#123

 可重写生成代码生成格式plugs\generatorConfig.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="generatorConfiguration" type="generatorConfigurationType"/>
  <xs:complexType name="jdbcConnectionType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="driverClass"/>
        <xs:attribute type="xs:string" name="connectionURL"/>
        <xs:attribute type="xs:string" name="userId"/>
        <xs:attribute type="xs:string" name="password"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="javaModelGeneratorType">
    <xs:sequence>
      <xs:element type="propertyType" name="property" maxOccurs="unbounded" minOccurs="0">
        <xs:annotation>
          <xs:documentation>是否允许子包,即targetPackage.schemaName.tableName  是否对model添加 构造函数  是否对类CHAR类型的列的数据进行trim操作  建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
    <xs:attribute type="xs:string" name="targetPackage"/>
    <xs:attribute type="xs:string" name="targetProject"/>
  </xs:complexType>
  <xs:complexType name="generatorConfigurationType">
    <xs:sequence>
      <xs:element type="propertiesType" name="properties">
        <xs:annotation>
          <xs:documentation>导入属性配置</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="classPathEntryType" name="classPathEntry">
        <xs:annotation>
          <xs:documentation>指定特定数据库的jdbc驱动jar包的位置</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="contextType" name="context">
        <xs:annotation>
          <xs:documentation><![CDATA[<table tableName="zq_role" domainObjectName="Role"enableCountByExample="true" enableUpdateByExample="true"enableDeleteByExample="true" enableSelectByExample="true"selectByExampleQueryId="true"><generatedKey column="id" sqlStatement="MySql" identity="true"/></table><table tableName="zq_role_permission" domainObjectName="RolePermissionMapping"enableCountByExample="true" enableUpdateByExample="true"enableDeleteByExample="true" enableSelectByExample="true"selectByExampleQueryId="true"><generatedKey column="id" sqlStatement="MySql" identity="true"/></table><table tableName="zq_user_login" domainObjectName="UserLogin"enableCountByExample="true" enableUpdateByExample="true"enableDeleteByExample="true" enableSelectByExample="true"selectByExampleQueryId="true"><generatedKey column="id" sqlStatement="MySql" identity="true"/></table><table tableName="zq_user_role" domainObjectName="UserRoleMapping"enableCountByExample="true" enableUpdateByExample="true"enableDeleteByExample="true" enableSelectByExample="true"selectByExampleQueryId="true"><generatedKey column="id" sqlStatement="MySql" identity="true"/></table>]]></xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="classPathEntryType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="location"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="javaClientGeneratorType">
    <xs:sequence>
      <xs:element type="propertyType" name="property"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="targetPackage"/>
    <xs:attribute type="xs:string" name="targetProject"/>
    <xs:attribute type="xs:string" name="type"/>
  </xs:complexType>
  <xs:complexType name="tableType">
    <xs:sequence>
      <xs:element type="generatedKeyType" name="generatedKey"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="tableName"/>
    <xs:attribute type="xs:string" name="domainObjectName"/>
    <xs:attribute type="xs:string" name="enableCountByExample"/>
    <xs:attribute type="xs:string" name="enableUpdateByExample"/>
    <xs:attribute type="xs:string" name="enableDeleteByExample"/>
    <xs:attribute type="xs:string" name="enableSelectByExample"/>
    <xs:attribute type="xs:string" name="selectByExampleQueryId"/>
  </xs:complexType>
  <xs:complexType name="commentGeneratorType">
    <xs:sequence>
      <xs:element type="propertyType" name="property" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="javaTypeResolverType">
    <xs:sequence>
      <xs:element type="propertyType" name="property"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="propertyType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name" use="optional"/>
        <xs:attribute type="xs:string" name="value" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="generatedKeyType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="column"/>
        <xs:attribute type="xs:string" name="sqlStatement"/>
        <xs:attribute type="xs:string" name="identity"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="contextType">
    <xs:sequence>
      <xs:element type="propertyType" name="property" maxOccurs="unbounded" minOccurs="0"/>
      <xs:element type="commentGeneratorType" name="commentGenerator">
        <xs:annotation>
          <xs:documentation>optional,旨在创建class时,对注释进行控制</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="jdbcConnectionType" name="jdbcConnection">
        <xs:annotation>
          <xs:documentation>jdbc的数据库连接</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="javaTypeResolverType" name="javaTypeResolver">
        <xs:annotation>
          <xs:documentation>非必需,类型处理器,在数据库类型和java类型之间的转换控制</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="javaModelGeneratorType" name="javaModelGenerator">
        <xs:annotation>
          <xs:documentation>Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="sqlMapGeneratorType" name="sqlMapGenerator">
        <xs:annotation>
          <xs:documentation>Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="javaClientGeneratorType" name="javaClientGenerator">
        <xs:annotation>
          <xs:documentation>客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element type="tableType" name="table"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id"/>
    <xs:attribute type="xs:string" name="targetRuntime"/>
  </xs:complexType>
  <xs:complexType name="propertiesType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="resource"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="sqlMapGeneratorType">
    <xs:sequence>
      <xs:element type="propertyType" name="property"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="targetPackage"/>
    <xs:attribute type="xs:string" name="targetProject"/>
  </xs:complexType>
</xs:schema>

使用IDEA运行 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值