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>
配置好后,右键点击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}