mybatis generator生成代码工具的使用, 附demo

使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值