Spring Data JPA 整合Spring 第二篇

时间:2022-05-20 16:45:49

主要是在CustomerDao中去写一些代码,在调用Query中去用SQL

例如

public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
/**
* 案例:根据客户名称查询客户
* 使用jpql的形式查询
* jpql:from Customer where custName = ?
*
* 配置jpql语句,使用的@Query注解
*/
@Query(value="from Customer where custName =?")
public Customer findJpql(String custName); /**
* 案例:根据客户名称和客户id查询客户
* jpql form Customer where custName=? and custId=?
* 对于多个占位符参数
* 赋值的时候,默认的情况下,占位符的位置需要和方法参数中的位置保持一致
* 可以指定占位符参数的位置
* ?索引的方式,指定此占位的取值来源
*/
@Query(value = "from Customer where custName=? and custId=?")
public Customer fingCustNameAndId(String name ,long id); /**
*使用jpql完成更新操作
* 案例:根据id更新,客户的名称
* 更新3号客户的名称
* sql: update cust_customer set cust_name=? where cust_id=?
* jpql: update Customer set custName = ? where custId =?
*
* @Query :代表的是进行查询
* 声明此方法是用来进行更新查询
* @Modifying
* 当前执行的是一个更新操作
*/
@Query(value ="update Customer set custName = ? where custId =?")
@Modifying
public void upadateCustomer(String custName,long custId); /**
* 使用sql的形式查询:
* 查询全部的客户
* sql : select * from cst_customer;
* Query : 配置sql查询
* value : sql语句
* nativeQuery : 查询方式
* true : sql查询
* false:jpql查询
*
*/
//@Query(value = " select * from cst_customer" ,nativeQuery = true)
@Query(value="select * from cst_customer where cust_name like ?",nativeQuery = true)
public List<Object [] > findSql(String name); }

  主要是在@Query中去调用数据库代码跟自己写的jpql代码

springDataJpa的入门操作
案例:客户的基本CRUD
i.搭建环境
创建工程导入坐标
配置spring的配置文件(配置spring Data jpa的整合)
编写实体类(Customer),使用jpa注解配置映射关系
ii.编写一个符合springDataJpa的dao层接口
* 只需要编写dao层接口,不需要编写dao层接口的实现类
* dao层接口规范
1.需要继承两个接口(JpaRepository,JpaSpecificationExecutor)
2.需要提供响应的泛型

*
findOne(id) :根据id查询
save(customer):保存或者更新(依据:传递的实体类对象中,是否包含id属性)
delete(id) :根据id删除
findAll() : 查询全部

第三 springDataJpa的运行过程和原理剖析
1.通过JdkDynamicAopProxy的invoke方法创建了一个动态代理对象
2.SimpleJpaRepository当中封装了JPA的操作(借助JPA的api完成数据库的CRUD)
3.通过hibernate完成数据库操作(封装了jdbc)

第四 复杂查询
i.借助接口中的定义好的方法完成查询
findOne(id):根据id查询
ii.jpql的查询方式
jpql : jpa query language (jpq查询语言)
特点:语法或关键字和sql语句类似
查询的是类和类中的属性

* 需要将JPQL语句配置到接口方法上
1.特有的查询:需要在dao接口上配置方法
2.在新添加的方法上,使用注解的形式配置jpql查询语句
3.注解 : @Query

iii.sql语句的查询
1.特有的查询:需要在dao接口上配置方法
2.在新添加的方法上,使用注解的形式配置sql查询语句
3.注解 : @Query
value :jpql语句 | sql语句
nativeQuery :false(使用jpql查询) | true(使用本地查询:sql查询)
是否使用本地查询
iiii.方法名称规则查询

注意:主要是跟第一篇一样所以都是有些内容如:Customer类spring配置文件也是一样的。

测试方法

package cn.itcast.domain;

import cn.itcast.dao.CustomerDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import java.util.Arrays;
import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class JpqlTest {
@Autowired
public CustomerDao customerDao; @Test
public void testJpql(){
Customer customer = customerDao.findJpql("二");
System.out.println(customer);
} @Test
public void testfingCustNameAndId(){
Customer customer = customerDao.fingCustNameAndId("杰哥", 1);
System.out.println(customer);
} /**
* 测试jpql更新操作
* springDataJpa中使用jpql完成,更新/删除操作
* 需要手动添加事务的支持
* 默认会执行结束之后,回滚事务
* @Rollback : 设置是否自动回滚
* false | true
*/
@Test
@Transactional//添加事务的支持
@Rollback(value = false)
public void testupadateCustomer(){
customerDao.upadateCustomer("浩总",3);
} //测试sql查询
@Test
public void testFindSql(){
List<Object[]> list = customerDao.findSql("杰哥%");
for (Object[] object : list){
System.out.println(Arrays.toString(object));
}
}
}