spring-data的基本用法

xiaoxiao2021-02-28  95

Repository接口讲解

1.Repository接口是Spring data的核心接口,不提供如何方法 2.public interface Repository<T,ID extends Serializable> 标记接口 3.@RepositoryDefinition

一、使用的两种方式:

//1. public interface IUserDao extends JpaRepository<TUser, Integer>{ //通过年龄来查询 public List<TUser> findByAge(Integer age); } //2. @RepositoryDefinition(domainClass=TUser.class,idClass=Integer.class) public interface IUserDao{ //通过年龄来查询 public List<TUser> findByAge(Integer age); }

二、子接口讲解:

CrudRepository:实现CRUD相关方法 PageingAndSortingRepository:实现分页排序相关的方法 JpaRepository:实现JPA规范相关的方法

三、Repository中查询方法定义规则和使用

四、Query注解使用

1.在Respository方法中使用,不需要遵循查询方法命名规则 2.只需要将@Query定义在Respository中的方法上即可 3.支持命名参数及索引参数的使用 4.本地查询

@Query("select o from Employee o where id = (select max(id) from Employee )") public Employee getMaxEmployee(); //Employee 是类名,不是表名称

说实话,我对HQL无任何好感! 那么重点来了,直接使用sql,很舒服!

@Query(nativeQuery=true,value="select * from user") public List<Map<String,Object>> grid();

五、更新及删除操作整合事务的使用

1、@Modifying注解的使用 2、@Modifying结合@Query注解执行更新操作 3、@Transactional在spring data中的使用

转载请注明原文地址: https://www.6miu.com/read-64047.html

最新回复(0)