Mybatis 自动生成代码配置 -Spring + Maven 环境

Spring + Maven 环境下 Mybatis 自动生成代码配置

  • 需要对应数据库的数据库驱动jar包
    本文为 mysql-connector-java-5.1.7-bin.jar
    放置路径为C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar

  • 添加 generatorConfig.xml 文件到项目中,注意路径,本文为 src/main/resources/generator/generatorConfig.xml

    • generatorConfig.xml 内容,涉及数据库驱动,数据库连接,Mapper,实体,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>
        <classPathEntry location="C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar" />
        <context id="context">
            <!-- 注释 -->
            <commentGenerator>  
                <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->  
                <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->  
                <property name="addRemarkComments" value="true"/>
            </commentGenerator>
    
            <!-- JDBC连接 -->
            <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8" driverClass="com.mysql.jdbc.Driver" password="root" userId="root"></jdbcConnection>
            <!-- 类型转换 -->
            <javaTypeResolver>
                <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- 生成实体类地址 -->    
            <javaModelGenerator targetPackage="com.bestcxx.stu.springmybatis.model" targetProject="src/main/java">  
                <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
                <property name="enableSubPackages" value="false" /> 
                <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
                <property name="trimStrings" value="true"/>  
            </javaModelGenerator>  
    
            <!-- 生成mapxml文件 -->  
            <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
                <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>  
    
            <!-- 生成mapxml对应client,也就是接口dao -->      
            <javaClientGenerator targetPackage="com.bestcxx.stu.springmybatis.dao" targetProject="src/main/java" type="XMLMAPPER">  
                <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
                <property name="enableSubPackages" value="false" />
                <!--<property name="rootInterface" value="cn.acsm.farmeasy.order.core.dao.BaseDAO"/> -->
            </javaClientGenerator>
    
            <!-- 配置表信息,这里没生成一张表,这里需要改变一次对应表名 -->  
            <!-- 已经生成实体类的表:-->  
    <!--         <table tableName="表名"  domainObjectName="驼峰格式实体名" module="order" enableCountByExample="false"      -->   
             <table tableName="test2"  domainObjectName="Test2" enableCountByExample="false"     
                enableDeleteByExample="false" enableSelectByExample="false"  enableUpdateByExample="false">  
                <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类   -->  
                <!-- 忽略列,不生成bean 字段   
                <ignoreColumn column="FRED" />-->  
                <!-- 指定列的java数据类型   
                <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->  
            </table>
    
        </context>
    </generatorConfiguration>
  • pom.xml 中添加 Maven 插件 配置

    指定了 generatorConfig.xml 的路径

    <build>
        ...
            <plugins>
            ...
                   <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                        <overwrite>true</overwrite>
                        <verbose>true</verbose>
                    </configuration>
                </plugin>
    
        </plugins>  
      </build>
  • 运行 mybatis-generator:generate,生成代码

    允许一次生成多个表的对应代码,如果使用 Spring Boot 可以直接在项目上运行 Mybatis 自动生成代码的程序
    完整的命令行是 mvn:mybatis-generator:generate ,这是一个 Maven 命令,如果直接在 Eclipse 中运行,使用 mybatis-generator:generate 即可。

猜你喜欢

转载自blog.csdn.net/bestcxx/article/details/79387763