1、导入mbg的核心包,下载地址;
https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.5
只需导入第一个
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis_0325"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator生成POJO
targetPackage:生成的POJO放在哪个包
targetProject:放在哪个工程下
-->
<javaModelGenerator targetPackage="com.atshiyou.bean" targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMapGenerator:sql映射文件生成器,指定xml生成的地方 -->
<sqlMapGenerator targetPackage="com.atshiyou.dao" targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator:生成dao接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atshiyou.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- tableName知道你生成哪个表
domainObjectName:这个表对应的对象名
-->
<table tableName="t_cat" domainObjectName="Cat" ></table>
<table tableName="t_employee" domainObjectName="Employee" ></table>
<table tableName="t_teacher" domainObjectName="Teacher" ></table>
</context>
</generatorConfiguration>
可以使用命令行运行,也可以使用java代码运行,这里展示用java代码:
MGBTest:
package com.atshiyou.test;
import java.io.File;
import java.io.IOException;
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.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MBGTest {
public static void main(String[] args) throws IOException, 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);
}
}
上面生成的是复杂的查询,我们可以更改配置实现简单的CRUD,看下面的官方文档介绍:
代码就只需要修改一处
所有源码打包
链接:https://pan.baidu.com/s/1JB8oB1UQpzgM9dsU_r9BcA
提取码:1234