1. /**
    2. * 符合SpringData的dao层的接口规范
    3. * JpaRepository<操作的实体类型,实体类中主键的类型>
    4. * * 封装了基本CRUD操作
    5. * JpaSpecificationExecutor<操作的实体类型>
    6. * * 封装了复杂查询(分页)
    7. */
    8. public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {
    9. /**
    10. * 案例:根据客户名称查询客户
    11. *
    12. * jpql:from Customer where custName = ?
    13. *
    14. * 配置jpql语句,使用@Query注解
    15. */
    16. @Query(value = "from Customer where custName = ?")
    17. Customer findJpql(String custName);
    18. /**
    19. * 案例:根据客户名称和客户id查询客户
    20. * jpql:from Customer where custName = ? and custId = ?
    21. *
    22. * 可以指定占位符参数的位置
    23. * ? 索引的方式,指定此占位的取值来源
    24. */
    25. @Query(value = "from Customer where custName = ?2 and custId = ?1")
    26. Customer findCustNameAndId(Long id,String name);
    27. /**
    28. * 使用jpql完成更新操作
    29. * 案例:根据id更新客户名称
    30. * 更新4号客户名称,将名称改为”农夫山泉“
    31. * sql:update cst_customer set cust_name = ? where cust_id = ?
    32. * jpql:update Customer set custName=? where custId = ?
    33. *
    34. * @Query : 代表的是进行查询
    35. * * 声明此方法使用了做更新操作
    36. * @Modifying
    37. * * 当前执行的是一个更新操作
    38. */
    39. @Query(value = "update Customer set custName = ?2 where custId = ?1")
    40. @Modifying
    41. void updateCustomer(long custId,String name);
    42. /**
    43. * 使用sql的形式查询:
    44. * 查询全部的客户
    45. * sql:select * from cst_customer
    46. * Query:配置sql查询
    47. * value : sql语句
    48. * nativeQuery : 查询方式
    49. * true : sql查询
    50. * false : jpql查询
    51. *
    52. * @return
    53. */
    54. //@Query(value = "select * from cst_customer",nativeQuery = true)
    55. @Query(value = "select * from cst_customer where cust_name like ?1",nativeQuery = true)
    56. List<Object []> findSql(String name);
    57. /**
    58. * 通过方法名称规则查询
    59. * 是对jpql查询更深入的一层封装
    60. * 我们只需要按照SpringDataJpa提供的方法名称规则定义方法,不需要再去配置jpql语句,完成查询
    61. *
    62. * findBy开头:代表查询
    63. * 对象中属性的名称(首字母大写)
    64. * * 含义:根据属性名称进行查询
    65. *
    66. * findByCustName -- 根据客户名称查询
    67. *
    68. * 在springDataJpa的运行阶段
    69. * 会根据方法名进行解析 findBy from xxx(实体类)
    70. * 属性名称 where custName =
    71. * * 默认情况 : 使用等于的方式查询
    72. * 特殊的查询方式
    73. *
    74. * 1. findBy + 属性名称(根据属性名称进行完成匹配的查询)
    75. * 2. findBy + 属性名称 + ”查询方式(Like | is null)“
    76. * findByCustNameLike
    77. * 3.多条件查询
    78. * findBy + 属性名 + ”查询方式" + “多条件的连接符(and | or)” + 属性名 + “查询方式”
    79. */
    80. Customer findByCustName(String name);
    81. List<Customer> findByCustNameLike(String custName);
    82. //使用客户名称模糊匹配和客户所属行业精准匹配的查询
    83. Customer findByCustNameLikeAndCustIndustry(String name,String custIndustry);
    84. }
    1. package com.cedric.test;
    2. import com.cedric.dao.CustomerDao;
    3. import com.cedric.domain.Customer;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.annotation.Rollback;
    8. import org.springframework.test.context.ContextConfiguration;
    9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10. import org.springframework.transaction.annotation.Transactional;
    11. import java.util.Arrays;
    12. import java.util.List;
    13. @RunWith(SpringJUnit4ClassRunner.class)
    14. @ContextConfiguration(locations = "classpath:applicationContext.xml")
    15. public class JpqlTest {
    16. @Autowired
    17. private CustomerDao customerDao;
    18. @Test
    19. public void testFindJPQL(){
    20. Customer customer = customerDao.findJpql("京东");
    21. System.out.println(customer);
    22. }
    23. @Test
    24. public void testFindCustNameAndId(){
    25. Customer customer = customerDao.findCustNameAndId(3l,"京东");
    26. System.out.println(customer);
    27. }
    28. /**
    29. * 测试jpql更新操作
    30. * * springDataJpa中使用jpql完成 更新/删除操作
    31. * * 需要手动添加事务的支持
    32. * * 默认会执行结束之后,回滚事务
    33. *
    34. * @Rollback : 设置是否自动回滚
    35. */
    36. @Test
    37. @Transactional // 添加事务的支持
    38. @Rollback(value = false)
    39. public void testUpdateCustomer(){
    40. customerDao.updateCustomer(4l,"农夫山泉");
    41. }
    42. // 测试sql查询
    43. @Test
    44. public void testFindSql(){
    45. List<Object[]> list = customerDao.findSql("阿里%");
    46. for (Object[] obj : list){
    47. System.out.println(Arrays.toString(obj));
    48. }
    49. }
    50. @Test
    51. public void testNaming(){
    52. Customer customer = customerDao.findByCustName("京东");
    53. System.out.println(customer);
    54. }
    55. // 测试方法命名规则的查询
    56. @Test
    57. public void testByCustNameLike(){
    58. List<Customer> list = customerDao.findByCustNameLike("阿里%");
    59. for (Customer customer : list){
    60. System.out.println(customer);
    61. }
    62. }
    63. @Test
    64. public void testFindByNameLikeAndCustIndustry(){
    65. Customer customer = customerDao.findByCustNameLikeAndCustIndustry("阿里%", "电商");
    66. System.out.println(customer);
    67. }
    68. }