import org.springframework.data.repository.CrudRepository;
import com.kmu.entity.Employee;
public interface EmployeeDao extends CrudRepository{
}
import static org.junit.Assert.fail;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kmu.entity.Employee;
import com.kmu.repoistory.EmployeeDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-beans.xml"})
public class EmployeeDaoTest {
/**
* EmployeeDao是一个接口,我们并没有为它编写一个实现类,那如何使用?
* Spring会自动根据我们的接口,动态生成一个代理对象,提供EmployeeDao的功能
*/
@Autowired
EmployeeDao dao;
/**
* 保存单个对象
*/
@Test
public void testSaveS() {
Employee emp = new Employee();
emp.setEmpName("生蚝");
emp.setEmpAge(2);
emp.setEmpGender("男");
Employee save = dao.save(emp);
/*
* 当保存完毕一个新的对象,会返回主键值,赋值到对象主键属性中
*/
System.out.println(save);
}
@Test
public void testSaveIterableOfS() {
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.setEmpName("桥桥");
emp2.setEmpName("美玉");
ArrayList arrayList = new ArrayList();
arrayList.add(emp1);
arrayList.add(emp2);
//save方法可以保存Iterable类型集合,ArrayList实现了List接口,继承Collection接口,继承了Iterable接口
dao.save(arrayList);
}
/**
* 它是根据主键,查询一个实体对象
*/
@Test
public void testFindOne() {
Employee findOne = dao.findOne(309);
System.out.println(findOne);
}
/**
* 判断指定的id对应的数据,是否存在,传入的参数,为对应的主键类型数据
*/
@Test
public void testExists() {
boolean exists = dao.exists(309);
System.out.println(exists);//true
exists = dao.exists(903);
System.out.println(exists);//false
}
/**
* 查询所有实体信息
*/
@Test
public void testFindAll() {
Iterable findAll = dao.findAll();
for (Employee employee : findAll) {
System.out.println(employee);
}
}
/**
* 集合中存储了多个员工的主键,查询这些主键对应的员工信息
*/
@Test
public void testFindAllIterableOfID() {
ArrayList arrayList = new ArrayList();
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
arrayList.add(7);
Iterable findAll = dao.findAll(arrayList);
for (Employee employee : findAll) {
System.out.println(employee);
}
}
/**
* 进行统计
*/
@Test
public void testCount() {
long count = dao.count();
System.out.println(count);
}
/**
* 根据主键ID删除
*/
@Test
public void testDeleteID() {
dao.delete(10);
}
/**
* 按照实体对象进行删除
*/
@Test
public void testDeleteT() {
Employee findOne = dao.findOne(4);
dao.delete(findOne);
}
/**
* 集合中存储所有要删除的员工信息主键,删除这个集合中的所有主键对应的员工信息
*/
@Test
public void testDeleteIterableOfQextendsT() {
ArrayList arrayList = new ArrayList();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.setEmpId(5);
emp2.setEmpId(6);
arrayList.add(emp1);
arrayList.add(emp2);
dao.delete(arrayList);
}
/**
* 删除所有
*/
@Test
public void testDeleteAll() {
dao.deleteAll();
}
}
注:下面两个配置文件与Repository其他子接口的一致
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/jpa
jdbc.user=root
jdbc.password=123123
import org.springframework.data.repository.PagingAndSortingRepository;
import com.kmu.entity.Employee;
public interface EmployeeDao extends PagingAndSortingRepository{
}
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kmu.entity.Employee;
import com.kmu.repoistory.EmployeeDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-beans.xml"})
public class EmployeeDaoPSTest {
@Autowired
EmployeeDao dao;
@Test
public void testFindAllSort() {
/*
* Sort表示一个排序规则对象
* 默认按照empAge升序排序
*/
// Sort sort = new Sort("empAge");
// Iterable emps = dao.findAll(sort);
// for (Employee employee : emps) {
// System.out.println(employee.getEmpName()+":"+employee.getEmpAge());
// }
//按照年龄降序排序
// Sort sort = new Sort(Direction.DESC,"empAge");
// Iterable emps = dao.findAll(sort);
// for (Employee employee : emps) {
// System.out.println(employee.getEmpName()+":"+employee.getEmpAge());
// }
/*
* 参数中,可以传入一个或多个Order对象,每一个Order对象,表示一个排序规则
* 使用场景:
* 按照年龄降序排序,如果年龄一样,则按照主键id进行升序排序
*/
Order order1 = new Order(Direction.DESC,"empAge");
Order order2 = new Order(Direction.ASC,"empId");
Sort sort = new Sort(order1,order2);
Iterable findAll = dao.findAll(sort);
for (Employee employee : findAll) {
System.out.println(employee.getEmpName()+":"+employee.getEmpAge());
}
}
@Test
public void testFindAllPageable() {
/*
* PageRequest是pageable的实现类,用来封装分页信息
*/
PageRequest pageRequest = new PageRequest(0, 5);
/*
* Page表示分页查询的结果,通过该对象,可以获取分页相关的数据
*/
Page empsPage = dao.findAll(pageRequest);
//获取分页查询到的所有员工信息
List content = empsPage.getContent();
System.out.println("从page对象中,获取page中的员工信息");
for (Employee employee : content) {
System.out.println(employee.getEmpId()+","+employee.getEmpName());
}
long totalElements = empsPage.getTotalElements();
System.out.println("获取总数:"+totalElements);//305
int totalPages = empsPage.getTotalPages();
System.out.println("获取总页数:"+totalPages);//61
int number = empsPage.getNumber();
System.out.println("当前页码:"+number);
int size = empsPage.getSize();
System.out.println("每页大小:"+size);
System.out.println("------------------");
/*
* 查询第5页数据,每页10条,按照年龄降序排序
*/
//分页的时候,也可以使用排序
pageRequest = new PageRequest(4,10,new Sort(Direction.DESC,"empAge"));
Page findAll = dao.findAll(pageRequest);
List content2 = findAll.getContent();
for (Employee employee : content2) {
System.out.println(employee.getEmpName()+":"+employee.getEmpAge());
}
}
}
import org.springframework.data.jpa.repository.JpaRepository;
import com.kmu.entity.Employee;
public interface EmployeeDao extends JpaRepository{
}
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kmu.entity.Employee;
import com.kmu.repoistory.EmployeeDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-beans.xml"})
public class EmployeeDaoJpaTest {
@Autowired
EmployeeDao dao;
@Test
public void testFindAll() {//查询全部,CrudRepository子接口测试过
}
@Test
public void testFindAllSort() {//在查询时进行排序,PagingAndSortingRepository子接口测试过
}
@Test
public void testFindAllIterableOfID() {//集合中存储了多个员工的主键,查询这些主键对应的员工信息,CrudRepository子接口测试过
}
@Test
public void testSaveIterableOfS() {//把对象放入集合中进行保存,CrudRepository子接口测试过
}
@Test
public void testFlush() {//刷新缓存效果
fail("Not yet implemented");
}
@Test
public void testSaveAndFlush() {
Employee employee = new Employee();
employee.setEmpName("小黑黑");
//会立刻将这个保存操作保存出去
dao.saveAndFlush(employee);
}
@Test
public void testDeleteInBatch() {//集合中存储所有要删除的员工信息主键,删除这个集合中的所有主键对应的员工信息,CrudRepository子接口测试中有与之类似的
ArrayList arrayList = new ArrayList();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.setEmpId(5);
emp2.setEmpId(6);
arrayList.add(emp1);
arrayList.add(emp2);
dao.deleteInBatch(arrayList);
}
@Test
public void testDeleteAllInBatch() {//删除所有
dao.deleteAllInBatch();
}
}
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.kmu.entity.Employee;
public interface EmployeeDao extends JpaSpecificationExecutor{
}
import static org.junit.Assert.fail;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.kmu.entity.Employee;
import com.kmu.repoistory.EmployeeDao;
/**
* JpaSpecificationExecutor,是JPA提供的,用于实现条件查询的一种方式。
* 意思就是:你先按照你的需求,拼接条件,通过它通过的编程式的方式进行拼接条件。
* 由谁来负责描述这个条件呢?
* 由:Specification描述说明,但是它可不知道你要查询使用什么条件,于是希望我们通过
* 手动实现匿名内部类的方式,来设置拼接条件。
* 然后,将拼接后的条件,交给JpaSpecificationExecutor去执行就可以了。
* JpaSpecificationExecutor提供的所有方法,都需要Specification参数。
* Specification参数,封装了查询条件。
* @author boge
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-beans.xml"})
public class EmployeeDaoSpecTest {
@Autowired
EmployeeDao dao;
@Test
public void testFindOneSpecificationOfT() {
/*
* 匿名内部类,根据父类、父接口,来创建一个匿名内部类,在匿名内部类中
* 可以重写抽象方法
*/
Specification specification = new Specification() {
/*
* 拼接查询条件
*/
@Override
public Predicate toPredicate(Root root, CriteriaQuery> cq, CriteriaBuilder cb) {
// TODO Auto-generated method stub
return null;
}
};
//specification作为查询条件,交给findOne去查询就可以
dao.findOne(specification);
}
@Test
public void testFindAllSpecificationOfT() {
fail("Not yet implemented");
}
@Test
public void testFindAllSpecificationOfTPageable() {
fail("Not yet implemented");
}
@Test
public void testFindAllSpecificationOfTSort() {
fail("Not yet implemented");
}
@Test
public void testCountSpecificationOfT() {
fail("Not yet implemented");
}
}