MybatisGenerator的使用记录

来源:互联网 发布:windows官网下载 编辑:程序博客网 时间:2024/05/13 02:35

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文件指定字符编码-->
        <propertyname="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 voidgenerator(){
  
try{
      String path = MybatisGeneratorUtil.
class.getResource("/NewGenerator.xml").getPath();
     
List<String> warnings = newArrayList<String>();
      boolean
overwrite = true;
     
File configFile = newFile(path);
     
ConfigurationParser cp = newConfigurationParser(warnings);
     
Configuration config = cp.parseConfiguration(configFile);
     
DefaultShellCallback callback = newDefaultShellCallback(overwrite);
     
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
     
myBatisGenerator.generate(null);
      for
(String warning : warnings) {
         System.
out.println(warning);
     
}
   }
catch (Exceptione){
      e.printStackTrace()
;
  
}
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 娘娘她不想宫斗 快穿之疯批宿主持美行凶 偏执病娇他说我有精神病 陆总的氪金小甜妻 将军,郡主又叒叕爬墙了! 残疾暴君的庶女妻 我在宗门种向日葵后拯救了修仙界 快穿:病娇男神又凶又粘人 戏精娘子总扮乖 我在系统圈中艰难生存 天道的疯批美人她靠算卦爆红 离婚后,偏执指挥官夜夜求关注 我靠无限物资全球基建 本道祖文成武德 全福夫人要和离 她从火光中来 裴先生每天都在撒娇求和 空间逃荒:团宠小尼姑路子野 别慌,学霸老爹和我一起穿越了 薄先生突然黏她上瘾 团宠崽崽是只桃花精 我那老板柔弱不能自理 反派小媳妇的逆袭指南 我有一个大佬群 二婚后,我在豪门扮柔弱 诸天仙神热搜:主神老婆竟是魔尊 团宠小福妻又娇又软 替嫁医妃有空间 重生之农门贵夫 野性偏爱 嫁给病弱太子后我躺赢了 咸鱼娘娘一心只想翻墙 救命!破产后高冷总裁处处招惹我 冷酷将军每天都想要贴贴 何以赎光 甜腻!病娇傅少竟然暗恋我! 天价萌妻:偏执帝少心尖宠 有读心术后,战神把娘子宠上天 穿书后,我刷错了反派的好感度 摄政王怀里的团宠美人娇又软 满级千金不想掉马