时间:2021-07-01 10:21:17 帮助过:33人阅读
四、测试类
package cn.jpa.test;
import cn.zrf.jpa.dao.CustomerDao;
import cn.zrf.jpa.entity.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import java.awt.print.Pageable;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestJpql {
@Autowired
CustomerDao customerDao;
//查询全部
@Test
public void getAllCustomerTest(){
List<Customer> list = customerDao.getAllCustomer();
for (Customer customer:list){
System.out.println(customer);
}
}
//查询全部并分页
@Test
public void getAllCustomerByPageTest(){
List<Customer> list = customerDao.getAllCustomerByPage(new PageRequest(0, 3));
for (Customer customer:list){
System.out.println(customer);
}
}
//根据ID查询
@Test
public void getCustomerByIdTest(){
Customer customer = customerDao.getCustomerById(1l);
System.out.println(customer);
}
//根据地址,姓名进行模糊查询
@Test
public void getCuseListTest(){
List<Customer> custList = customerDao.getCustList("%京%", "%长%");
for (Customer customer:custList){
System.out.println(customer);
}
}
//根据ID进行局部更新操作
@Test
@Transactional
@Commit
public void getUpdateByIdTest(){
customerDao.getUpdateById("张无忌",3l);
}
//原生sql语句模糊查询
@Test
public void getCustomerByNativeTest(){
List<Customer> list = customerDao.getCustomerListByNative("%长%");
for (Customer customer:list){
System.out.println(customer);
}
}
//方法命名规则查询测试
//根据ID查询
@Test
public void findByIdTest(){
Customer customer = customerDao.findByCustId(1L);
System.out.println(customer);
}
//根据地址,姓名进行模糊查询
@Test
public void findByCustNameLikeAndAddressLike(){
List<Customer> list = customerDao.findByCustNameLikeAndCustAddressLike("%张%","%京%");
for (Customer customer:list){
System.out.println(customer);
}
}
//根据地址进行分页查询
@Test
public void findByCustAddressLike(){
List<Customer> list = customerDao.findByCustAddressLike("%京%", new PageRequest(0, 3));
for (Customer customer:list){
System.out.println(customer);
}
}
}
springDataJPQL实现增删改查及分页,原生sql查询,根据方法命名规则实现查询
标签:cto Pageable pac sele executor extend 地址 应该 gen