SpringData使用

SpringData是简化持久层的代码量通过@Query(‘SQL’)来实现接口方法的增删改查
1.继承抽象类Repository
2.定义持久层接口 定义方法
3.通过@Query实现持久操作
4.注意SQL语句在此叫JPQL语句,JPQL不支持INSERT插入语句

 public interface UserDao extends Repository<AccountInfo, Long> { 
		 @Query("select a from AccountInfo a where a.accountId = ?1") 
		 public AccountInfo findByAccountId(Long accountId); 
		
		    @Query("select a from AccountInfo a where a.balance > ?1") 
		 public Page<AccountInfo> findByBalanceGreaterThan( 
		 Integer balance,Pageable pageable); 
 } 

查询条件参数指定
语句中通过": 变量"的格式来指定参数,同时在方法的参数前面使用 @Param 将方法参数与 JP QL 中的命名参数对应。

查询使用属性
在这里插入图片描述

public interface UserDao extends Repository<AccountInfo, Long> { 

 public AccountInfo save(AccountInfo accountInfo); 

 @Query("from AccountInfo a where a.accountId = :id") 
 public AccountInfo findByAccountId(@Param("id")Long accountId); 

   @Query("from AccountInfo a where a.balance > :balance") 
   public Page<AccountInfo> findByBalanceGreaterThan( 
 @Param("balance")Integer balance,Pageable pageable); 
 } 

使用 @Query 来执行一个更新操作
用 @Modifying 来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询
在这里插入图片描述
Spring Data JPA 对事务的支持
针对查询类型的方法,其等价于 @Transactional(readOnly=true);增删改类型的方法,等价于 @Transactional。可以看出,除了将查询的方法设为只读事务外,其他事务属性均采用默认值。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值