Spring Data Jpa复杂查询

1、查询总数
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class daoTest {
    @Autowired
    private CustomerDao customerDao;
	@Test
	public void testCount(){
	     long count = customerDao.count();
	     System.out.println(count);
	 }
}
2、根据id判断是否存在
@Test
public void testExists(){
     System.out.println(customerDao.exists(4l));
 }
3、立即加载和延迟加载

在上一篇文章中我们学过一个教findOne的方法,这个方法是立即加载
这里我们介绍一个叫getOne的方法,这是一个延迟加载

@Test
@Transactional
public void testGetOne(){
    Customer customer = customerDao.getOne(4l);
    System.out.println(customer);
}
4、引入jpql查询

使用@Query注解

通过名字查询
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
    @Query(value = "from Customer where name = ? ")
    public Customer findByName(String custName);
}
public class TestJpql {
    @Autowired
    private CustomerDao customerDao;
    @Test
    public void testCustByName(){
        Customer customer = customerDao.findByName("aaa");
        System.out.println(customer);
    }
}
多占位符赋值
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {

    @Query(value = "from Customer where name = ? ")
    public Customer findByName(String custName);

    @Query(value = "from Customer where name = ? and id = ? ")
    public Customer findByNameAndId(String custName,Long id);
}

这里面的占位符是根据参数排列的,或者可以在?后面加入数字,代表索引的位置。

@Test
public void testCustByNameAndId(){
    Customer customer = customerDao.findByNameAndId("aaa",4l);
    System.out.println(customer);
}
更新操作

@Query:代表的是进行查询
@Modfying:当前执行的是一个更新操作

@Query(value = "update Customer set name = ?2 where id = ?1 ")
@Modifying
public void updateCustomerNameById(long custId,String custName);

测试jpql的更新操作
springdatajpa中使用jpql完成 更新/删除操作
需要手动添加事务支持,默认会执行结束之后回滚事务,

@Test
@Transactional
@Rollback(value = false)
public void testUpNameById(){
    customerDao.updateCustomerNameById(4l,"bbbb");
}
5、引入sql查询

在@Query注解中有一个属性是nativeQuery:是否使用本地查询(指的就是sql)

查询全部
@Query(value = "select * from t_customer",nativeQuery = true)
public List<Customer> selectAllBySql();
@Test
public void testFindAllSql(){
    List<Customer> list = customerDao.selectAllBySql();
    for(Customer customer:list){
        System.out.println(customer);
    }
}
根据名字查询
@Query(value = "select * from t_customer where cust_name like ?1",nativeQuery = true)
 public List<Customer> selectAllByNameSql(String name);
@Test
public void testFindAllByNameSql(){
   List<Customer> list = customerDao.selectAllByNameSql("b%");
   for(Customer customer:list)
       System.out.println(customer);
}
6、方法名称规则查询
普通查询

是对jpql查询,更加深入的一层封装,我们只需要按照springdatajpa提供的方法名称规则定义方法,不需要再去配置jpql语句,完成查询。

  • 方法的预定:
    findBy:查询
    • 对象中的属性名(首字母大写):查询条件
  • findByCustName:根据名字查询
  • 在springdatajpa的运行阶段:
    会根据方法名称进行解析 findBy from xxx(实体类)属性名称 where custName =
public Customer findByName(String custName);
@Test
public void findByCustName(){
  Customer customer = customerDao.findByName("bbbb");
    System.out.println(customer);
}
模糊查询

findBy + 属性名称 + (查询方式)like | isnull。

 public List<Customer> findByNameLike(String name);
@Test
public void findByCustNameLike(){
    List<Customer> list = customerDao.findByNameLike("bb%");
    for (Customer customer:list){
        System.out.println(customer);
    }
}
多条件查询

findBy + 属性名称 + 查询方式(like | isnull) + 多条件连接符(and | or)+ 属性名 + 查询方式。

public Customer findByNameLikeAndId(String name,long id);
@Test
public void findByCustNameLikeAndId(){
     Customer customer = customerDao.findByNameLikeAndId("bb%",4l);
     System.out.println(customer);
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值