MybatisGenerator的使用记录

mybatisGenerator的使用

首先需要一个generator.xml的配置文件

根据http://blog.csdn.net/isea533/article/details/42102297#reply来写,配置相关的内容

写好的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE
generatorConfiguration
       
PUBLIC "-//mybatis.org//DTDMyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"
>
<generatorConfiguration>
   
<!--指定外部配置文件-->
   
<properties resource="generator.properties"/>
   
<!--指定路径的文件可被加载,常用于加载驱动的路径-->
    <!--<classPathEntrylocation=""/>-->
   
<context id="mygenarator"defaultModelType="flat" targetRuntime="MyBatis3">
        <property
name="beginningDelimiter" value="'"></property>
        <property
name="endingDelimiter" value="'"></property>
       
<!--为生成的java文件指定字符编码-->
        <property name="javaFileEncoding" value="UTF-8"></property>
       
<!--给生成的类添加toString方法-->

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

       
<!-- 为生成的Java模型类添加序列化接口,并生成serialVersionUID字段 -->
        <!--这是一个自己写的插件,需要继承类或接口然后覆写相应的方法-->
        <!--org.mybatis.generator.api.Plugin-->
       <!--org.mybatis.generator.api.PluginAdapter-->
        <plugin type="com.zheng.common.plugin.SerializablePlugin">
            <property
name="suppressJavaInterface" value="false"/>
        </plugin>
       
<!-- 生成一个新的selectByExample方法,这个方法可以接收offset和limit参数,主要用来实现分页,只支持mysql(已使用pagehelper代替) -->
        <!--<plugintype="com.zheng.common.plugin.PaginationPlugin"></plugin>-->

        <!-- 生成在XML中的<cache>元素-->
        <plugin type="org.mybatis.generator.plugins.CachePlugin">
           
<!-- 使用ehcache-->
           
<propertyname="cache_type" value="org.mybatis.caches.ehcache.LoggingEhcache"/>
           
<!-- 内置cache配置 -->
            <!--
            <propertyname="cache_eviction" value="LRU" />
            <propertyname="cache_flushInterval" value="60000" />
            <propertyname="cache_readOnly" value="true" />
            <propertyname="cache_size" value="1024" />
            -->
        </plugin>
       
<!-- Java模型生成equals和hashcode方法-->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>

       
<!--这里有个type自己实现需要一个类继承org.mybatis.generator.api.CommentGenerator-->
        <!--默认实现org.mybatis.generator.internal.DefaultCommentGenerator-->
        <commentGenerator>
           
<!--禁止生成注释和时间戳-->
           
<propertyname="suppressAllComments" value="false"></property>
            <property
name="suppressDate" value="true"></property>
        </commentGenerator>
       
<!--数据库连接信息-->
       
<jdbcConnection driverClass="${generator.jdbc.driver}"
                       
connectionURL="${generator.jdbc.url}"
                       
userId="${generator.jdbc.username}"
                       
password="zheng" />

       
<!--生成根据表生成model,根据defaultmodeltype一个表生成一个model-->
        <!--关于model的相关设置都在这里面,比如是构造方法入参还是getter和setter入参等设置,使用property标签来设置 -->
        <javaModelGenerator targetPackage="com.zheng.upms.dao.model" targetProject="D:/ieadZheng/zheng/zheng-upms/zheng-upms-dao/src/main/java" />
       
<!--生成mapperXML,该标签和javaClientGenerator有联系-->
        <sqlMapGenerator targetPackage="com.zheng.upms.dao.mapper" targetProject="D:/ieadZheng/zheng/zheng-upms/zheng-upms-rpc-service/src/main/java"/>
       
<!--生成mapper接口,这里的type可分成几种情况,里面的子属性,可以指定生成的接口在某个包下,继承某个公共的父接口等等-->
        <javaClientGenerator targetPackage="com.zheng.upms.dao.mapper" targetProject="D:/ieadZheng/zheng/zheng-upms/zheng-upms-dao/src/main/java" type="XMLMAPPER" />

       
<!--根据表生成mapper,model,modelexample和mapperXML-->
        <table tableName="upms_log" domainObjectName="UpmsLog"></table>
        <table
tableName="upms_organization" domainObjectName="UpmsOrganization"></table>
        <table
tableName="upms_permission" domainObjectName="UpmsPermission"></table>
        <table
tableName="upms_role" domainObjectName="UpmsRole"></table>
        <table
tableName="upms_role_permission" domainObjectName="UpmsRolePermission"></table>
        <table
tableName="upms_system" domainObjectName="UpmsSystem"></table>
        <table
tableName="upms_user" domainObjectName="UpmsUser">
            <generatedKey
column="user_id" sqlStatement="MySql" identity="true"/>
        </table>
        <table
tableName="upms_user_organization" domainObjectName="UpmsUserOrganization"></table>
        <table
tableName="upms_user_permission" domainObjectName="UpmsUserPermission"></table>
        <table
tableName="upms_user_role" domainObjectName="UpmsUserRole"></table>
    </context>
</generatorConfiguration>

以上这些配置已经基本够用了,如果还需要添加其他插件的话,网上找,或者自己继承或实现相关的接口。

最后,java中调用generator生成文件的方式

public static void generator(){
  
try{
      String path = MybatisGeneratorUtil.
class.getResource("/NewGenerator.xml").getPath();
     
List<String> warnings = new ArrayList<String>();
      boolean
overwrite = true;
     
File configFile = new File(path);
     
ConfigurationParser cp = new ConfigurationParser(warnings);
     
Configuration config = cp.parseConfiguration(configFile);
     
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
     
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
     
myBatisGenerator.generate(null);
      for
(String warning : warnings) {
         System.
out.println(warning);
     
}
   }
catch (Exceptione){
      e.printStackTrace()
;
  
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值