1 使用原生的sql 语句(步骤:
1.1 dao接口中定义方法
1.2 在方法上添加@Query注解
1.3 在注解中添加原生的 sql语句和nativeQuery = true
dao接口代码:
/**
* JpaRepository需要注意的 继承后里面的泛型
* 第一个是实体类类型 第二个是该类主键的数据类型
*/
public interface CustomerDao extends JpaRepository {
//原生
@Query(value = "select * from cst_customer where cust_name like ?",nativeQuery = true)
List findByNameLikeCustomer(String name);
}
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataJpaTest {
@Autowired
private CustomerDao customerDao;
/*
原生方法 根据name模糊查询
*/
@Test
public void findByNameLikeCustomer(){
List customerList = customerDao.findByNameLikeCustomer("%山%");
for (Customer customer : customerList) {
System.out.println(customer);
}
}
}
2 方法命名规则(重点,推荐使用:
通过一定规则定义一个方法 框架可以根据方法名生成一个SQL语句进行查询
规则:
2.1 方法应该使用findBy开头
2.2 查询某个字段findBy后跟实体类的属性名称
2.3 如果有多个条件就在方法的后面+And+实体的属性名
2.4 方法的参数对应查询的定义
2.5 返回值根据返回值的类型的定义
dao接口代码:
/**
* JpaRepository需要注意的 继承后里面的泛型
* 第一个是实体类类型 第二个是该类主键的数据类型
*/
public interface CustomerDao extends JpaRepository {
/*
方法命名规则
*/
//根据id查
Customer findByCustId(long id);
//根据name和address模糊查询
List findByCustNameLikeAndCustAddressLike(String name,String address);
//根据name模糊查询并分页
Page findByCustNameLike(String name,Pageable pageable);
}
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataJpaTest {
@Autowired
private CustomerDao customerDao;
/*
方法命名规则查询
*/
//根据id查
@Test
public void findByCustId(){
Customer customer = customerDao.findByCustId(23l);
System.out.println(customer);
}
//根据name和address模糊查询
@Test
public void findByCustNameLikeAndCustAddressLike(){
List customerList = customerDao.findByCustNameLikeAndCustAddressLike("%山%", "%智%");
for (Customer customer : customerList) {
System.out.println(customer);
}
}
//根据name查询并分页
@Test
public void findByCustNameLike(){
Page list = customerDao.findByCustNameLike("%斗%", new PageRequest(0, 5));
System.out.println(list.getTotalElements());
System.out.println(list.getTotalPages());
List customerList = list.getContent();
for (Customer customer : customerList) {
System.out.println(customer);
}
}
}
3 使用Specification方式进行查询(难点
说明书方式查询 ---最强大的查询方式 最复杂的查询方式
使用
3.1 dao接口继承JpaSpecificationExecutor
3.2 使用JpaSpecificationExecutor接口中提供的方法进行查询
注:每个方法都需要使用Specification对象作为参数(匿名内部类
dao接口代码:
/**
* JpaRepository需要注意的 继承后里面的泛型
* 第一个是实体类类型 第二个是该类主键的数据类型
*
* JpaSpecificationExecutor参数是实体类类型
*/
public interface CustomerDao extends JpaRepository,JpaSpecificationExecutor {
}
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataJpaTest {
@Autowired
private CustomerDao customerDao;
/*
使用Specification方式进行查询
*/
//根据id查询
@Test
public void findByIdSpecification(){
Customer customer = customerDao.findOne(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
Predicate custId = cb.equal(root.get("custId"), 2l);
return custId;
}
});
System.out.println(customer);
}
//Specification方式模糊查询name和address
@Test
public void findByNameAndAddress(){
customerDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
Predicate custName = cb.like(root.get("custName"), "%山%");
Predicate custAddress = cb.like(root.get("custAddress"), "%利%");
Predicate predicate = cb.and(custName, custAddress);
return predicate;
}
}).forEach(customer -> System.out.println(customer));
}
//Specification方式模糊查询name和address并分页(Specification说明式分页
@Test
public void findByNameAndAddressByPage(){
Page all = customerDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
Predicate custName = cb.like(root.get("custName"), "%斗%");
Predicate custAddress = cb.like(root.get("custAddress"), "%西%");
Predicate predicate = cb.and(custName, custAddress);
return predicate;
}
}, new PageRequest(0, 5));
for (Customer customer : all) {
System.out.println(customer);
}
}
//name模糊查询,排序
@Test
public void findByNameLike(){
customerDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
Predicate custName = cb.like(root.get("custName"),"%斗%");
return custName;
}
},new Sort(Sort.Direction.DESC,"custId")).forEach(System.out::println);
}
}
over~