基于上一篇修改
1.Generator配置文件修改,将targetRuntime改为MyBatis3
2.项目结构目录
这个xxxExample就是拼接条件用的
3.测试代码
注释写的很详细
public static void main(String[] args) throws Exception { SqlSession session = getSqlSession(); try { // 面向接口方式 MembersMapper mapper = session.getMapper(MembersMapper.class); // 测试返回list集合 // 查询(member_name like %a% and gender = '1') or email like &y& MembersExample example = new MembersExample(); Criteria criteria = example.createCriteria(); criteria.andMemberNameLike("%a%"); criteria.andGenderEqualTo(1); // 每一个or条件就是一个新的Criteria Criteria criteria1 = example.createCriteria(); criteria1.andEmailLike("%y%"); // 直接拼接 example.or(criteria1); List<Members> list = mapper.selectByExample(example); for (Members member : list) { System.out.println(member); } } finally { // 关闭session session.close(); } // generatorAuto(); }
4.查询结果
发送的sql语句如下
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5f150435] ==> Preparing: select id, member_name, age, gender, email from members WHERE ( member_name like ? and gender = ? ) or( email like ? ) ==> Parameters: %a%(String), 1(Integer), %y%(String) <== Columns: id, member_name, age, gender, email <== Row: 1, jack, 25, 1, jack@beijing.com <== Row: 2, pnoy, 26, 0, pnoy@beijing.com <== Total: 2 com.Entity.Members@42e26948 com.Entity.Members@57baeedf
ok~就这样