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




  
      
      
      
          
              
              
          
          
          
          
          
          
          
          
          
           
          
              
              
              
              
               
        


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 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}



你可能感兴趣的:(Java框架)