MyBatis逆向工程

MyBatis逆向工程-通过数据库表生成pojo类
一. 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>  
     <!-- 注释控制完毕 -->  
注意先后顺序。上面去除注释的配置要放在数据库配置的前面,不然会报错。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值