MyBatis逆向工程-通过数据库表生成pojo类
一. java代码实现
1. mybatis逆向工程开发文档
http://www.mybatis.org/generator/configreference/xmlconfig.html
2. 逆向工程 xml配置文件
generatorConfig.xml (开发文档网址复制xml配置)
http://www.mybatis.org/generator/running/runningWithJava.html
一. java代码实现
1. mybatis逆向工程开发文档
http://www.mybatis.org/generator/configreference/xmlconfig.html
2. 逆向工程 xml配置文件
generatorConfig.xml (开发文档网址复制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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/gcbs"
userId="root"
password="godce">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.gc.model" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Mapper.xml配置文件 -->
<sqlMapGenerator targetPackage="com.gc.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper 接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.gc.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="gcbs" tableName="t_user" domainObjectName="User" ></table>
<table schema="gcbs" tableName="t_parttimeInfo" domainObjectName="ParttimeInfo"></table>
<table schema="gcbs" tableName="t_category" domainObjectName="Category"></table>
</context>
</generatorConfiguration>
3.
运行MyBatisGenerator Java程序
http://www.mybatis.org/generator/running/runningWithJava.html
public class MyBatisGenerate {
public static void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//项目根路径不要有中文,我的有中文,所以使用绝对路径
File configFile = new File("conf/generatorConfig.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);
}
public static void main(String[] args) {
try {
generator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.
逆向生成成功你会发现 pojo类会多一个XxxExample,通过观察里面的 List<Criteria> 参数可知 此类用于 自定义查询。
XxxExample xxExample = new XxxExample();
XxxExample.Criteria criteria = xxExample.createCriteria();
criteria.andNameEqualTo("笔记本3");
//将 设置好的 xxExample 作为参数传入
List<Xxx> list = xxMapper.selectByExample(xxExample);
5. 生成的代码可能有很多mybatis的注释(大量重复,很烦),可以在配置文件中加入如下配置取消注释:
<commentGenerator >
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 注释控制完毕 -->
注意先后顺序。上面去除注释的配置要放在数据库配置的前面,不然会报错。