设为首页 收藏本站
查看: 1952|回复: 0

[经验分享] mybatis generator生成代码工具的使用, 附demo

[复制链接]

尚未签到

发表于 2016-11-26 09:00:29 | 显示全部楼层 |阅读模式
  使用Hibernate时, 可以很方便的生成model,dao,和映射配置文件.在mybatis里, 也有生成器, 即mybatis generator, 简称MBG. 下面为大家介绍一下MBG的使用.
  下载mybatis-generator-core-1.3.1-bundle.zip之后, 解压得到mybatis-generator-core-1.3.1.jar, 即生成器的jar包, 将mybatis-3.0.6.jar和mybatis-generator-core-1.3.1.jar加入到项目的lib里, 然后编写一个生成器的配置文件generatorConfig.xml. 
  介绍一下genernatorConfig.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="com/yuan/mybatis/mbg/util/generatorConfig.properties" />
<classPathEntry location="${classPath}" />
<context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="fileName" value="GeneratedMapperConfig.xml" />
<property name="targetPackage" value="com.yuan.mybatis.mbg.util" />
<property name="targetProject" value="${targetProject}" />
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}" userId="${userId}" password="${password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="${modelPackage}"
targetProject="${targetProject}">
<property name="enableSubPackages" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="${sqlMapperPackage}"
targetProject="${targetProject}">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="${daoMapperPackage}" targetProject="${targetProject}">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="minghan" tableName="sys_group" domainObjectName="Group">
<generatedKey column="groupId" sqlStatement="mysql"
identity="true" />
</table>
<table schema="minghan" tableName="sys_admin" domainObjectName="Admin"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
  其中的properties元素里引用了一个generatorConfig.properties配置文件是为了方便移植的时候,只需修改resource的路径值和generatorConfig.properties里的值即进行生成操作.
  generatorConfig.properties代码如下:

classPath=D\:/ylink/myeclipse/MBG/lib/mysql-connector-java-5.0.7-bin.jar
targetProject=D\:/ylink/myeclipse/MBG/src
driverClass=com.mysql.jdbc.Driver
connectionURL=jdbc\:mysql\://127.0.0.1\:3306/minghan?useUnicode\=true&amp;characterEncoding\=UTF-8
userId=root
password=root
modelPackage=com.yuan.mybatis.mbg.model
sqlMapperPackage=com.yuan.mybatis.mbg.model.mapper
daoMapperPackage=com.yuan.mybatis.mbg.dao.mapper

  其中, targetProject是工程的源代码存放位置
  classPath是数据库驱动包的位置, modelPackage是存放model实体的包, 对应数据库表, sqlMapperPackage是存放实体映射的XML配置文件, daoMapperPackage是存放Mapper接口的地方.
  注意: 要事先建立这些包才可以生成.
  如果不想生成mybatis的注释信息, 可以将commentGenerator里的suppressAllComments的值设为true,
  table元素对应数据库表.
  如

<table schema="minghan" tableName="sys_admin" domainObjectName="Admin"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>

  schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 就不会生成对应的Example类了.
  配置文件配好了之后,
  进行生成代码, 既可以使用命令的方式, 也可以自已写一个带main的类来运行, 下面提供了一个带main的运行类
  MyBatisGeneratorTool.java

package com.yuan.mybatis.mbg.util;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyBatisGeneratorTool {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = "/generatorConfig.xml"; //src的一级目录下
File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

  运行此类即可生成对应于数据库表的实体类, Mapper配置文件, Mapper接口, 生成后可以测试其可用性.
  附件里是Demo

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305694-1-1.html 上篇帖子: Mybatis sqlMapConfig.xml中的常用配置 下篇帖子: Mybatis ORA-00911: 无效字符
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表