criteriaBuilder.equal(name, "冬马和纱") 即
name = "冬马和纱"
返回结果:
1 2 3 4 5
Hibernate: select user0_.u_id as u_id1_1_, user0_.u_address_id as u_addres3_1_, user0_.u_name as u_name2_1_ from user user0_ where user0_.u_name=? Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? ID: 1, Name: 冬马和纱, Address: 北京
List<User> userList = userDao.findAll(spec); for (User user : userList) { System.out.println(user); } }
返回结果:
1 2 3 4 5
Hibernate: select user0_.u_id as u_id1_1_, user0_.u_address_id as u_addres3_1_, user0_.u_name as u_name2_1_ from user user0_ where user0_.u_name like ? Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? ID: 1, Name: 冬马和纱, Address: 北京
Process finished with exit code 0
Specification多条件查询
toPredicate的criteriaBuilder参数提供了条件处理的方法:and
/ or / not.
List<User> userList = userDao.findAll(spec); for (User user : userList) { System.out.println(user); } }
返回结果:
1 2 3 4 5 6
Hibernate: select user0_.u_id as u_id1_1_, user0_.u_address_id as u_addres3_1_, user0_.u_name as u_name2_1_ from user user0_ where cast(user0_.u_id as signed)>=2 and user0_.u_address_id=10001 Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? ID: 3, Name: 鹿目圆香, Address: 北京 ID: 5, Name: 雪之下雪乃, Address: 北京
List<User> userList = userDao.findAll(spec, Sort.by(Sort.Direction.DESC, "id")); for (User user : userList) { System.out.println(user); } }
返回结果:
1 2 3 4 5 6 7
Hibernate: select user0_.u_id as u_id1_1_, user0_.u_address_id as u_addres3_1_, user0_.u_name as u_name2_1_ from user user0_ where user0_.u_address_id=10001 order by user0_.u_id desc Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? ID: 5, Name: 雪之下雪乃, Address: 北京 ID: 3, Name: 鹿目圆香, Address: 北京 ID: 1, Name: 冬马和纱, Address: 北京
Hibernate: select user0_.u_id as u_id1_1_, user0_.u_address_id as u_addres3_1_, user0_.u_name as u_name2_1_ from user user0_ where cast(user0_.u_id as signed)>1 limit ?, ? Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? Hibernate: select address0_.a_id as a_id1_0_0_, address0_.a_name as a_name2_0_0_ from address address0_ where address0_.a_id=? Hibernate: select count(user0_.u_id) as col_0_0_ from user user0_ where cast(user0_.u_id as signed)>1 ID: 5, Name: 雪之下雪乃, Address: 北京 ID: 10, Name: 由比滨结衣, Address: 深圳