MyBatis框架
7.3 逆向工程的使用
- 基本查询的测试
@Test public void testSelect() throws Exception { SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession();
try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); List<Employee> emps = mapper.selectAll(); for (Employee employee : emps) { System.out.println(employee); } } finally { session.close(); } } |
- 带条件查询的测试
@Test public void testSelect() throws Exception { SqlSessionFactory ssf = getSqlSessionFactory(); SqlSession session = ssf.openSession(); try { EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); //条件查询: 名字中带有'张' 并且 email中'j' 或者 did = 2 EmployeeExample example = new EmployeeExample(); Criteria criteria = example.createCriteria(); criteria.andLastNameLike("%张%"); criteria.andEmailLike("%j%"); //or Criteria criteriaOr = example.createCriteria(); criteriaOr.andDIdEqualTo(2); example.or(criteriaOr); List<Employee> emps = mapper.selectByExample(example); for (Employee employee : emps) { System.out.println(employee); } } finally { session.close(); } } |