1spring data jpa
spring data jpa 复杂查询
1、利用接口进行查询
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class JpaTest {}
1.1、查询总数量:
/*
* 测试统计查询:查询客户的总数量
* count:统计总条数
* */
@Test
public void testCount(){
long count = customerDao.count();
System.out.println(count);//查询全部的客户数量
}
查看结果:
1.2、判断id为2的客户是否存在
/*
* 测试:判断id为2的客户是否存在
* 1.可以查询一下id为4的客户
* 如果值为空,代表不存在,如果不为空,代表存在
* 2.判断数据库中id为4的客户的数量
* 如果数量为0,代表不存在,如果大于0,代表存在
* */
@Test
public void testExists(){
boolean exists = customerDao.exists(2l);
System.out.println(exists);
}
查看结果:
1.3、根据id查询:(延迟加载)
/*
* 根据id从数据库查询
* @Transactional:保证getOne正常运行
*
* findOne:
* em.find() :立即加载
* getOne:
* em.getReference :延迟加载
* * 返回的是一个客户的动态代理对象
* * 什么时候用,就什么时候查询
* */
@Test
@Transactional
public void testGetOne(){
Customer customer = customerDao.getOne(2l);java
System.out.println(customer);
}
查看结果:
2、jpql的查询方式
jpql:jpa query language(jpa查询语言)
特点:语法或关键字和sql语句类似
查询的是类和类中的属性
* 需要将JPQL语句配置到接口方法上
1.特有的查询:需要在dao接口上配置方法
2.在新添加的方法上,使用注解的形式配置jpql查询语句
3.注解:@Query
2.1、使用jpql完成基本的查询
案例:根据客户名臣查询客户
public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
/*
* 案例:根据客户名臣查询客户
* 使用jpql的形式查询
* jpql:from Customer where custName = ?
*
* 配置jpql语句,使用的@Query注解
* */
@Query(value = "from Customer where custName = ?1")
public Customer findJpql(String custName);
}
测试方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JpqlTest {
@Autowired
CustomerDao customerDao;
@Test
public void testJqpl(){
Customer customer = customerDao.findJpql("实达");
System.out.println(customer);
}
}
查看结果:
案例:根据客户名称和客户id查询客户
/*
* 案例:根据客户名称和客户id查询客户
* jpql:from Customer where custName = ? and custId = ?
*
* 对于多个占位符参数
* 赋值的时候,默认的情况下,占位符的位置需要和方法参数中的位置保持一致
*
* 可以指定占位符
* ?索引的方式,执行此占位的取值来源
* */
@Query(value = "from Customer where custName = ?1 and custId = ?2")
public Customer findCustomerNameAndId(String name,Long id);
查看结果:
使用jpql完成更新操作
/*
* 使用jpql完成更新操作
* 案例:根据id更新,客户的名称
* 更新2号客户的名称,将名称改为"sddm"
*
* sql :update cst_customer set cust_name = ? where cust_id = ?
* jpql :update Cutomer set custName = ? where custId = ?
*
* @Query:代表的是进行查询
* * 声明此方法是用来进行更新操作
* @Modifying
* * 当前执行的是一个更新操作
*
* */
@Query("update Customer set custName = ?2 where custId = ?1")
@Modifying
void updateCustomer(long custId,String custName);
测试:
/*
* 测试jpql的更新操作
* * springDataJpa中是哦那个jpql完成 更新/删除操作
* * 需要手动添加事务的支持
* * 默认会执行结束之后,回滚事务
* @Rollback:设置是否自动回滚
* false | true
* */
@Test
@Transactional
@Rollback(value = false)
public void updateCustomer(){
customerDao.updateCustomer(2l,"sddm");
}
查看结果:
3、sql语句的查询
1.特有的查询:需要在dao接口上配置方法
2.在新添加的方法上,使用注解的形式配置sql查询语句
3.注解:@Query
value:@Query
nativeQuery:false(使用jpql查询)| true (使用本地查询:sql查询)
是否使用本地查询
3.1、查询全部
/*
* 使用sql的形式查询:
* 查询全部的客户
* sql:select * from cst_customer
* Query:配置sql查询
* value:sql语句
* nativeQuery:查询方式
* true :sql查询
* false :jpql查询
* */
@Query(value = "select * from cst_customer",nativeQuery = true)
public List<Object[]> findSql();
测试:
/*
* 测试sql查询
* */
@Test
public void findSql(){
List<Object[]> lists = customerDao.findSql();
for (Object[] obj:lists){
System.out.println(Arrays.toString(obj));
}
}
查看结果:
条件查询:
/*
* 条件查询
* */
@Query(value = "select * from cst_customer where cust_name like ?1",nativeQuery = true)
public List<Object[]> findSql(String name);
测试:
/*
* 测试sql查询
* */
@Test
public void findCondition(){
List<Object[]> lists = customerDao.findSql("%s%");
for (Object[] obj:lists){
System.out.println(Arrays.toString(obj));
}
}
查看结果:
4、方法名称规则查询
-
是对jpql查询,更加深入的一层封装,
-
我们只需要按照SpringDataJpa提供的方法名称规则定义方法,不需要再去配置jpql语句,完成查询
-
findBy开头:代表查询
对象中属性的名称(首字母大写)
含义:根据属性名称进行查询
- findBy + 属性名称(根据属性名称进行完成匹配的查询=)
- findBy + 属性名称 + “查询凡是(Like | isnull)
4.1、根据客户名称查询
/*
* 方法名的约定:
* findBy :查询
* 对象中的属性名(首字母大写):查询的条件
* CustName
* findByCustName -- 根据客户名称查询
*
* 在springdataJpa的运行阶段
* 会根据方法名称进行解析 findBy from xxx(实体类)
* 属性名称 where custName =
* */
public Customer findByCustName(String custName);
查看结果:
4.2 模糊查询
/*
* findBy + 属性名称(根据属性名称进行完成匹配的查询=)
* findBy + 属性名称 + “查询凡是(Like | isnull)”
* findByCustNameLike
* */
public List<Customer> findByCustNameLike(String custName);
测试:
/*
* 测试方法查询
* */
@Test
public void findByCustNameLike(){
List<Customer> customers = customerDao.findByCustNameLike("实%");
System.out.println(customers);
}
查看结果:
4.3、多条件查询
/*
* 多条件查询
* findBy + 属性名 + “查询凡是”+ “多条件的连接符(and|or)” + 属性名 + “查询方式”
* */java
//使用客户名称模糊匹配和客户所属行业精准匹配的查询
public List<Customer> findByCustNameLikeAndCustIndustry(String custName,String custIndustry);
测试:
@Test
public void findByCustNameLikeAndCustIndustry(){
List<Customer> customers = customerDao.findByCustNameLikeAndCustIndustry("实达","福%");java
System.out.println(customers);
}