软件测试学习之路

使用Maven插件mybatis-generator生成代码配置

2018-05-09  本文已影响224人  乘风破浪的姐姐

SSM框架的项目中,用到插件自动生成实体类、dao、mapper.xml文件。详细配如下。
1、在pom.xml中需要导入包和插件:

  <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.6</version>
    </dependency>

    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.6</version>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>

2、在项目的/src/main/resources(默认目录)的文件目录下加入generateConfig.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="prop/jdbc-mysql.properties" />
    <!-- 指定数据连接驱动jar地址 -->
    <classPathEntry location="${driverPath}" />
    <context id="context" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
        <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
        </plugin>
        <!-- 去掉生成出来的代码的注解 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="${jdbc.driverClass}"
           connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="${target_package}.model"
                            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="${target_package}.mapping"
                         targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${target_package}.dao" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
        <table tableName="t_result_detail" domainObjectName="ResultDetail"></table>

    </context>
</generatorConfiguration>

注:
javaModelGenerator :指定生成pojo的包和此包在项目中的地址;
sqlMapGenerator :指定生成pojo的映射xml文件的所在包和此包在项目中的地址;
javaClientGenerator :指定生成访问映射xml文件的接口所在包和此包在项目中的地址;

table属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
tableName为对应的数据库表
domainObjectName是要生成的实体类

如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true或者不要enableCountByExample等字段。 这样就会生成一个对应domainObjectName的Example类。
<table tableName="t_result_detail" domainObjectName="ResultDetail"></table>
这样,一个表自动生成的pojo类有会有两个。一个是domainObjectName命名的类名,一个是domainObjectName+Example的类名。想要修改以Example结尾的类名。可以在上述配置文件中加入以下配置,其中replaceString对应的value可以根据需要设置。

  <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
 </plugin>

Spring初始化这个example后可以创建一个criteria,里面有一系列的条件方法,用来pin sql ,当然这些pin的sql在mapper.xml里面已经写好了,在需要的时候直接调用就可以了。

如果不需要生成对应的Example类,则将enableCountByExample等设为false即可。如下:

<table tableName="t_student" domainObjectName="Address" 
            enableCountByExample="false" 
            enableUpdateByExample="false" 
            enableDeleteByExample="false"
            enableSelectByExample="false" 
           selectByExampleQueryId="false" >
            <property name="useActualColumnNames" value="true"/>
 </table>

3、在intelli IDEA 的菜单栏,点击RUN-Edit Configurations,点击弹出窗口左上角的+号,新增Maven.为当前配置配置一个名称,这里命名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”。这里加了“-e ”选项是为了让该插件输出详细信息,帮助我们定位问题。
点击Apply。


image.png
image.png

点击运行geratorator,即可自动生成dao,pojo,mapping对应文件。


image.png
上一篇 下一篇

猜你喜欢

热点阅读