在MBG.xml 配置文件中,有 多种 targetRuntime
可以设置。
<!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD MyBatis3:豪华版 -->
1、当用simple 方式 生成代码时,可以用下面的代码测试效果。
@Test public void testMyBatis3Simple() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); List<Employee> list = mapper.selectByExample(null); for (Employee employee : list) { System.out.println(employee.getId()); } }finally{ openSession.close(); } }
2、当用 MyBatis3 方式时,可以用下面代码测试:
@Test public void testMyBatis3() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //xxxExample就是封装查询条件的 //1、查询所有 //List<Employee> emps = mapper.selectByExample(null); //2、查询员工名字中有e字母的,和员工性别是1的 //封装员工查询条件的example EmployeeExample example = new EmployeeExample(); //创建一个Criteria,这个Criteria就是拼装查询条件 //select id, last_name, email, gender, d_id from tbl_employee //WHERE ( last_name like ? and gender = ? ) or email like "%e%" Criteria criteria = example.createCriteria(); criteria.andLastNameLike("%e%"); criteria.andGenderEqualTo("1"); Criteria criteria2 = example.createCriteria(); criteria2.andEmailLike("%e%"); example.or(criteria2); List<Employee> list = mapper.selectByExample(example); for (Employee employee : list) { System.out.println(employee.getId()); } }finally{ openSession.close(); } }