Spring整合SpringDataJpa----JPQL

本文主要介绍了如何在Spring项目中整合SpringDataJpa,并重点讲解了JPQL的使用,通过具体的 dao 层代码和测试方法来展示其实现过程。
摘要由CSDN通过智能技术生成

Spring整合SpringDataJpa----JPQL

话不多说,直接上代码
dao层:

package dao;

import domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 符合SpringDataJpa的Dao层接口规范
 *      JpaRepository<操作的实体类类型,实体类中的主键属性的类型>
 *          *封装了基本CRUD操作
 *      JpaSpecificationExecutor<操作的实体类型>
 *          *封装了复杂查询(分页)
 */
public interface CustomerDao extends JpaRepository<Customer,Long> , JpaSpecificationExecutor<Customer> {

    /**
     * 根据客户名称查询客户
     */
    @Query(value = "from Customer where custName = ?1")
    public Customer findJpql(String custName);

    /**
     * 根据客户名称和客户id查询客户
     * @param custName
     * @param id
     * @return
     */
    @Query(value = "from Customer where custName = ?1 and custId = ?2")//1,2分别指下面方法参数的位置例1指custName
    public Customer findCustNameAndId(String custName,Long id);

    /**
     * 更新操作
     * @Query:代表是进行查询
     * @Modifying:代表进行查询
     * @param custId
     * @param custName
     */
    @Query(value = "update Customer set custName = ?2 where custId = ?1")
    @Modifying
    public void updateCustomer(Long custId,String custName);

    /**
     * 模糊查询
     * @param
     * @return
     */
    @Query(value = "select * from cst_customer",nativeQuery = true)
    public List<Object [] > findSqlAll();

    /**
     * 模糊查询
     * @param name
     * @return
     */
    @Query(value = "select * from cst_customer where cust_name like ?1",nativeQuery = true)
    public List<Object [] > findSql(String name);

}

测试方法:

import dao.CustomerDao;
import domain.Customer;
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")//指定spring容器配置信息
public class jpqlTest {

    @Autowired
    private CustomerDao customerDao;

    @Test
    public void testFindJPQL(){
        Customer customer = customerDao.findJpql("张三");
        System.out.println(customer);
    }

    @Test
    public void testFindCustNameAndId(){
        Customer customer = customerDao.findCustNameAndId("张三",1l);
        System.out.println(customer);
    }

    @Test
    @Transactional
    @Rollback(value = false)//设置事务不自动回滚
    public void testUpdateCustomer(){
        customerDao.updateCustomer(3l,"赵六");
    }

    @Test
    public void testFindSqlAll(){
        List<Object [] > list = customerDao.findSqlAll();
        for (Object[] objects : list) {
            System.out.println(Arrays.toString(objects));
        }
    }

    @Test
    public void testFindSql(){
        List<Object [] > list = customerDao.findSql("张%");
        for (Object[] objects : list) {
            System.out.println(Arrays.toString(objects));
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值