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">
<propertyname="beginningDelimiter"value="'"></property>
<propertyname="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();
}
}
- MybatisGenerator的使用记录
- MybatisGenerator的使用
- MybatisGenerator工具使用简介
- MybatisGenerator的自动生成
- myBatisGenerator
- Mybatis自动代码生成插件MybatisGenerator使用
- idea使用MybatisGenerator逆向生成DAO文件
- MyBatisGenerator的自动生成代码之Eclipse
- MyBatisGenerator的自动生成代码之MyEclipse
- MyBatisGenerator的自动生成代码之MyEclipse
- MyBatisGenerator的自动生成代码之Eclipse
- 2分钟使用mybatisGenerator自动生成dao和mapper文件——使用cmd命令行
- eclipse使用MyBatisGenerator自动生成相关文件出错:project does not exist
- 记录的使用
- FCK使用的记录
- 使用HessianKit的记录
- sourceinsight的使用记录
- movefileex的使用记录
- sklearn 自带手写字体数据集
- 手把手教你写一个生成对抗网络
- 【量化小讲堂-Python&Pandas系列17】简易波动指标(EMV)策略在A股的实证
- 防止程序启动两次的方法CreateMutex()
- leetCode之Two Sum python实现
- MybatisGenerator的使用记录
- ON1 Resize 2017(无损照片放大工具) v11.6.0.3853 官方免费版下载
- nyoj 491 幸运三角形
- RGB彩色空间和YIQ、灰度等的转化
- 颜色模式
- Opencv之mat介绍
- 为什么-128用二进制表示为1000000000000000?
- Struts2简单实现
- 数组