天天看点

利用MyBatis Generator自动创建代码利用MyBatis Generator自动创建代码

利用MyBatis Generator自动创建代码

1、eclipse安装generator

https://yunpan.cn/cS2jYXX2S9KQR  访问密码 3bbc,解压并安装到eclipse相应文件中。

2、项目创建generator的配置文件

<?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="jdbc.properties"/>  
      
    <!-- 指定数据连接驱动jar地址 -->  
    <classPathEntry location="D:/mybatis-generator/mybatisGenerator/eclipse/lib/mysql-connector-java-5.1.25-bin.jar" />  
      
    <!-- 一个数据库一个context -->  
    <context id="infoGuardian">  
        <!-- 注释 -->  
        <commentGenerator >  
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->  
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->  
        </commentGenerator>  
          
        <!-- jdbc连接 -->  
        <jdbcConnection driverClass="${master.jdbc.driverClassName}"  
            connectionURL="${master.jdbc.url}" userId="${master.jdbc.username}"  
            password="${master.jdbc.password}" />  
          
        <!-- 类型转换 -->  
        <javaTypeResolver>  
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
          
        <!-- 生成实体类地址 -->    
        <javaModelGenerator targetPackage="com.dragonTravel.entity"  
            targetProject="dragonTravel" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false"/>  
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
          
        <!-- 生成mapxml文件 -->  
        <sqlMapGenerator targetPackage="com.dragonTravel.mapper"  
            targetProject="dragonTravel" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>  
          
        <!-- 生成mapxml对应client,也就是接口dao -->      
        <javaClientGenerator targetPackage="com.dragonTravel.dao"  
            targetProject="dragonTravel" type="XMLMAPPER" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </javaClientGenerator>  
          
        <!-- 配置表信息 -->      
        <table schema="drtr" tableName="dt_user"  
            domainObjectName="User" 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>
           

3、右击mybatis-generator.xml配置文件,自动生成代码。

利用MyBatis Generator自动创建代码利用MyBatis Generator自动创建代码