Spring 全家桶之 Spring Data JPA(二)(下)

简介: Spring 全家桶之 Spring Data JPA(二)

delete()删除操作

@Test
public void testDelete(){
    customerDao.delete(3l);
}

findAll()查询所有操作

@Test
public void testFindAll(){
    List<Customer> customerList = customerDao.findAll();
    for (Customer customer : customerList) {
        System.out.println(customer);
    }
}

image.png

执行过程

image.png

I:实例化的customerDao是一个动态代理对象SimpleJpaRepository

image.png

II:SimpleJpaRepository调用findOne()方法,findOne()会通过实体类管理器em调用find()方法完成查询

image.png

Spring Data JPA 完成复杂查询

统计查询count()

@Test
public void testCount(){
    long count = customerDao.count();
    System.out.println(count);
}

是否存在exists()

@Test
public void testExists(){
    boolean isExists = customerDao.exists(1l);
    System.out.println(isExists);
}

根据ID查询getOne()

@Test
@Transactional
public void testGetOne(){
    Customer one = customerDao.getOne(1l);
    System.out.println(one);
}

底层调用的是EntityManager的getReference(),延迟加载,find()是立即加载

JPA中的查询方法:

image.png

使用JPQL完成复杂查询

JPQL:JPA Query Language

特点:语法或关键字与sql语句类似,查询的是类和类中的属性

需要将JPQL语句配置到接口方法上

  • 特有的查询,需要在dao接口上配置方法
  • 在新添加的方法上使用注解的形式配置JPQL语句
  • 注解为@Query

在CustomerDao接口中新增方法,根据客户名称查询客户,使用JPQL语句

@Query(value = "from Customer where custName= ?")
Customer findByCustName(String custName);

在CustomerDao中测试该方法

@Test
public void testFindByCustName(){
    Customer thor = customerDao.findByCustName("Thor");
    System.out.println(thor);
}

输出结果

image.png

多条件(多占位符)查询

在CustomerDao接口中新增方法

// 根据客户名称和id查询客户
@Query(value = "from Customer where custName = ? and custId=?")
Customer findByCustNameAndCustId(String custName, Long custId);

在CustomerDaoTest中测试该方法

@Test
public void testFindByCustNameAndCustId(){
    Customer thor = customerDao.findByCustNameAndCustId("Thor",3L);
    System.out.println(thor);
}

image.png

通过custId更新custName

CustomerDao接口中新增方法updateCustNameByCustId

// 要指定参数位置
// 声明为更新操作
@Query(value = "update Customer set custName = ?2 where custId = ?1")
@Modifying
@Transactional
void updateCustNameByCustId(Long custId, String custName);

在CustomerDaoTest中测试该方法

@Test
public void testUpdateCustName(){
    customerDao.updateCustNameByCustId(3L,"Thor Odin");
}

使用SQL语句完成复杂查询

  • 特有的查询,需要在dao接口上配置方法
  • 在新添加的方法上,使用注解的形式配置SQL查询语句
  • 注解为@Query(value=,nativeQuery=),value表示jpql语句或者sql语句,nativeQuery为boolean,false表示使用jpql查询,true表示sql查询

SQL语句查询全部

定义方法selectAll()

@Query(value = "SELECT * FROM customer", nativeQuery = true)
List<Customer> selectAll();

测试该方法

@Test
public void testSelectAll(){
    List<Customer> customers = customerDao.selectAll();
    for (Customer customer : customers) {
        System.out.println(customer);
    }
}

输出结果

image.png

SQL语句模糊查询

定义方法selectByCustNameLike

@Query(value = "SELECT * FROM customer where cust_name like ?1", nativeQuery = true)
List<Customer> selectByCustNameLike(String like);

测试该方法

@Test
public void testSelectByCustNameLike(){
    List<Customer> customers = customerDao.selectByCustNameLike("Thor%");
    for (Customer customer : customers) {
        System.out.println(customer);
    }
}

输出结果

image.png

方法名称规则查询

  是对jpql查询更加深入的一层封装,只需要按照Spring Data JPA提供的方法名规则定义方法,不需要在配置jpql语句即可完成查询

命名规则:查询使用findBy,对象中的属性为查询的条件,如想要通过custName查询Customer,方法名命名为findByCustName,入参为custName,翻译成sql语句就是select * from customer where cust_name = ?,默认使用=,如果是模糊查询需要Like关键字

findByCustName

Customer findByCustName(String custName);
@Test
public void testFindByCustName(){
    Customer thor = customerDao.findByCustName("Thor Odin");
    System.out.println(thor);
}

输出结果

image.png

findByCustNameLike 模糊查询

List<Customer> findByCustNameLike(String custName);
@Test
public void testFindByCustNameLike(){
    List<Customer> customers = customerDao.findByCustNameLike("Thor%");
    for (Customer customer : customers) {
        System.out.println(customer);
    }
}

输出结果

image.png

findByCustNameAndCustId 多条件查询使用And关键字,查询的属性的顺序要与入参顺序一致

Customer findByCustNameLikeAndCustIndustry(String custName, String custIndustry);
@Test
public void testFindByCustNameLikeAndCustIndustry(){
    Customer stark_industry = customerDao.findByCustNameLikeAndCustIndustry("Iron%", "Military Industry");
    System.out.println(stark_industry);
}

image.png


相关文章
|
8天前
|
druid Java 数据库连接
SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis
SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis
77 0
|
8天前
|
XML Java 数据库连接
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
54 0
|
8天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
8天前
|
Java Spring
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
Spring Boot利用Spring Data JPA实现排序与分页查询实战(附源码,超详细)
112 0
|
8天前
|
Java 数据库 Spring
如何使用Spring Data JPA完成审计功能
如何使用Spring Data JPA完成审计功能
|
8天前
|
Java 数据库连接 API
Spring Boot整合Spring Data JPA进行CRUD和模糊查询
Spring Boot整合Spring Data JPA进行CRUD和模糊查询
42 0
|
8天前
|
缓存 NoSQL Java
Spring Data Redis对象缓存序列化问题
在使用 Redis 时,有没有遇到同我一样,对象缓存序列化问题的呢?
74 6
Spring Data Redis对象缓存序列化问题
|
8天前
|
存储 Java 数据库连接
Spring Boot 嵌入式服务器、Hibernate 关系和 Spring Data 全解析
Spring Boot 的嵌入式服务器功能是一项方便而强大的功能,它允许你在应用程序中直接运行 Web 服务器,无需将其部署到单独的独立 Web 服务器中。这使得开发、测试和部署 Web 应用程序变得容易,而且它还是轻量级的、易于启动和停止的,易于配置。
70 0
|
8天前
|
存储 Java 网络架构
Spring Data Elasticsearch基础入门详解
Spring Data Elasticsearch基础入门详解
137 0
|
8天前
|
SQL Java Spring
Spring Data JPA之JpaSpecificationExecutor复杂动态查询实例
Spring Data JPA之JpaSpecificationExecutor复杂动态查询实例
34 0