MyBatis Generator 自动生成Dao、Bean、Mapping相关

1、配置eclipse使用MyBatis Generator

从 https://github.com/mybatis/generator/releases 下载MyBatis Generator
把解压后的MyBatis Generator放入eclipse对应的目录中(features/plugins)

重启eclipse,在 File->New->Other->MyBatis会找到相应选项

2、使用MyBatis Generator 自动生成Dao、Bean、Mapping

配置详情参考:http://mbg.cndocs.tk/index.html
generatorConfig.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>  
    <!-- 数据库驱动包位置 -->  
    <classPathEntry  
        location="D:\eclipse\workspace\vip_admin\WebContent\WEB-INF\lib\mysql-connector-java-5.1.6-bin.jar" />  
    <context id="context1">  
        <commentGenerator>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!-- 数据库链接URL、用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
            connectionURL="jdbc:mysql://IP:PORT/springmvc" userId="root" password="root" />  
        <!-- 生成模型的包名和位置 -->  
        <javaModelGenerator targetPackage="com.zp.bean" targetProject="vip_admin/mbg" />  
        <!-- 生成的映射文件报名和位置 -->  
        <sqlMapGenerator targetPackage="com.zp.dao.mapper" targetProject="vip_admin/mbg" />  
        <!-- 生成DAO的包名和位置 -->  
        <javaClientGenerator targetPackage="com.zp.dao" targetProject="vip_admin/mbg" type="XMLMAPPER" />  
        <!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->   
        <table schema="springmvc" tableName="studentinfo" domainObjectName="StudentInfoVO" enableCountByExample="true" enableUpdateByExample="true"  
            enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">  
            <columnOverride column="id" property="id"  />  
            <columnOverride column="name" property="name" />  
            <columnOverride column="classname" property="classname" />  
            <columnOverride column="teacher_id" property="teacherId" />  
            <columnOverride column="comment" property="comment" />  
            <columnOverride column="type" property="type" />   
        </table> 
    </context>  
</generatorConfiguration>  


3、生成类

配置好后,右键点击generatorConfig.xml,选择Generate MyBatis/iBATIS  会生产相应的类

4、测试

	@Test
	public void handler() throws JSONException {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		StudentInfoVOMapper dao = context.getBean(StudentInfoVOMapper.class);
		StudentInfoVOExample example = new StudentInfoVOExample();
		com.zp.bean.StudentInfoVOExample.Criteria criteria = example.createCriteria();
		criteria.andIdGreaterThanOrEqualTo(7);
		criteria.andNameLike("%aa%");
		
		List<StudentInfoVO> list = dao.selectByExampleWithBLOBs(example);
		for (StudentInfoVO studentInfoVO : list) {
			System.out.println(JSONUtil.serialize(studentInfoVO));
		}

5、结果

16:20:47.725 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
16:20:47.725 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@24111ef1] will not be managed by Spring
16:20:47.737 [main] DEBUG c.z.d.S.selectByExampleWithBLOBs - ==>  Preparing: select 'true' as QUERYID, id, name, classname, teacher_id, type , comment from studentinfo WHERE ( id >= ? and name like ? ) 
16:20:47.819 [main] DEBUG c.z.d.S.selectByExampleWithBLOBs - ==> Parameters: 7(Integer), %aa%(String)
16:20:47.858 [main] DEBUG c.z.d.S.selectByExampleWithBLOBs - <==      Total: 2
16:20:47.859 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58be6e8]
16:20:47.859 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
{"classname":"一班","comment":"你好 欢迎您id=7","id":7,"name":"aa2","teacherId":1,"type":1}
{"classname":"一班","comment":null,"id":10,"name":"aa11","teacherId":1,"type":2}



  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值