Mybatis generator自动生成mybatis配置和类信息

自动生成代码方式两种:

1、命令形式生成代码,详细讲解每一个配置参数。

2、Eclipse利用插件形式生成代码。

安装插件方式:

eclipse插件安装地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/

附件有link安装包,link安装方式参考http://maimode.iteye.com/admin/blogs/1164524

MyBatis Generator详细介绍参见:http://code.google.com/p/mybatis/wiki/Generator

安装插件的过程就不说了,安装完后,eclipse中File-》new-》other中会发现多了mybatis选项说明插件安装成功

如何使用插件

在任意项目中利用上图中的向导创建generatorConfig.xml文件(名称可修改)然后修改文件内容,主要是设置连接数据的相关参数:

generator自动生成mybatis的xml配置、model、map等信息:

1、下载mybatis-generator-core-1.3.2.jar包。
       网址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下载mybatis-generator-core-1.3.2-bundle.zip,将mybatis-generator-core-1.3.2.jar放到项目中
2、编写generatorConfig的xml文件,名下:generatorConfig.xml

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="init.properties"/>
    <!-- 指定数据连接驱动jar地址 -->
    <classPathEntry location="E:workspacemysql-connector-java-5.1.24-bin.jar"/>
    
    <!-- 一个数据库一个context -->
    <context id="infoGuardian" targetRuntime="MyBatis3">
        <!-- 注释 -->
        <commentGenerator >  
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>  
          
        <!-- jdbc连接 -->
        <jdbcConnection driverClass="${jdbc_driver}"  
            connectionURL="${jdbc_url}" userId="${jdbc_user}"  
            password="${jdbc_password}" />  
          
        <!-- 类型转换 -->
        <javaTypeResolver>  
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
          
        <!-- 生成实体类地址 -->    
        <javaModelGenerator targetPackage="com.common.user.model"  
            targetProject="basic-user-websrcmainjava" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="true"/>  
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
          
        <!-- 生成mapxml文件 -->  
        <sqlMapGenerator targetPackage="com.common.user.Mapper"  
            targetProject="basic-user-websrcmainjava" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="true" />  
        </sqlMapGenerator>  
          
        <!-- 生成mapxml对应client,也就是接口dao -->      
        <javaClientGenerator targetPackage="com.user.model"  
            targetProject="basic-user-websrcmain
esources" type="XMLMAPPER" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->  
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
          
        <!-- 配置表信息 -->      
        <!--<table schema="cap_common" tableName="mosf_common_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>  --> 
         <table schema="" tableName="mosf_common_user" domainObjectName="UserModel"  
            delimitIdentifiers="true" delimitAllColumns="true"  
            enableCountByExample="false" enableUpdateByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false"  
            selectByExampleQueryId="false" />
    </context>  
</generatorConfiguration>

table其他属性:
enableCountByExample="false" 
enableUpdateByExample="false"
enableDeleteByExample="false" 
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 
就不会生成对应的Example类了.


如果table里边不配置property,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。

点击generatorConfig.xml右键上的选项,如果配置正确,便自动创建相关文件了。

文件主要有三类:

client包,mapper 接口文件

model包,实体bean文件

mapper包,mapper xml文件

 

原文地址:https://www.cnblogs.com/564085446java/p/3831651.html