1.将jar包(mybatis-generator-core-1.3.2.jar)导入项目bin文件目录中
2.在项目顶级文件目录(与src同级)创建后缀为XML的文件(此处示例文件名为mbg.xml)
mbg.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>
<!--需要的jar包,但是我们不需要,我们把mybatis-generator-core-1.3.2.jar添加项目路径中了-->
<!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
-->
<!--<context>元素用于指定生成一组对象的环境。子元素用于指定要连接的数据库,要生成的对象的类
型以及要进行内省的表。可以在<generatorConfiguration> 元素内列出多个<context>元素
, 以允许在MyBatis Generator(MBG)的同一运行中从不同的数据库或具有不同的生成参数
生成对象。
targetRuntime:MyBatis3 MyBatis3Simple
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator >
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc连接数据库的环境-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/smbms"
userId="root"
password="123456">
</jdbcConnection>
<!--<javaTypeResolver>元素用于定义Java Type Resolver的属性。Java类型解析器用于根据数据库列信息计算Java类型。缺省的Java Type Resolver尝试通过替换Integral类型(Long,Integer,Short等)来使JDBC DECIMAL和NUMERIC类型更易于使用。如果这种行为是不希望的,请将属性“ forceBigDecimals”设置为“ true”。如果您想要不同于默认行为的行为,也可以替换自己的实现。该元素是<context>元素的可选子元素。-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成java实体类代码-->
<javaModelGenerator targetPackage="cn.kgc.pojo" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--接口的生成-->
<sqlMapGenerator targetPackage="cn.kgc.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--映射器-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.kgc.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--数据对应信息-->
<table schema="DB2ADMIN" tableName="smbms_address" domainObjectName="Address" ></table>
<table schema="DB2ADMIN" tableName="smbms_bill" domainObjectName="Bill" ></table>
<table schema="DB2ADMIN" tableName="smbms_provider" domainObjectName="Provider" ></table>
<table schema="DB2ADMIN" tableName="smbms_role" domainObjectName="Role" ></table>
<table schema="DB2ADMIN" tableName="smbms_user" domainObjectName="User" ></table>
</context>
</generatorConfiguration>
#配置文件结束(配置文件内容根据需求自行更改)
3.将相关的其余jar包导入:如 MyBatis(必须) 数据库加载驱动,log4j等
4.使用XML配置文件从Java运行MBG:创建java文件,代码如下:
public void test01() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.xml");
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);
}
运行完成后查看项目src目录,生成各层包实体类,映射文件等,到此完成
也可通过打开jar内的index.HTML文件,通过MyBatis Generator简介完成工具的使用操作