pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.0.7.Final</hibernate.version>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<c3p0.version>0.9.1.2</c3p0.version>
<mysql.version>8.0.19</mysql.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
package cn.neu.test;
import cn.neu.dao.CustomerDao;
import cn.neu.domain.Customer;
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.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.persistence.criteria.*;
import java.util.List;
/**
* @Author WCJ
* @Description
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Spec {
@Autowired
private CustomerDao customerDao;
/**
* 查询单个对象:根据条件
* @throws Exception
*/
@Test
public void testSpec() throws Exception{
Specification spec = (root,query,cb)->{
Path custName = root.get("custName");
Predicate perd = cb.equal(custName, "传智播客");
return perd;
};
Customer c = customerDao.findOne(spec);
System.out.println(c);
}
/**
* 查询单个对象:根据多个条件
* @throws Exception
*/
@Test
public void testSpec1() throws Exception{
Specification spec = (root,query,cb)->{
Path custName = root.get("custName");
Path custIndustry = root.get("custIndustry");
Predicate p1 = cb.equal(custName, "传智播客");
Predicate p2 = cb.equal(custIndustry, "教育");
Predicate predicate = cb.and(p1, p2);
return predicate;
};
Customer c = customerDao.findOne(spec);
System.out.println(c);
}
@Test
public void testSpec3() throws Exception{
Specification<Customer> specification = new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Object> custName = root.get("custName");
Predicate like = criteriaBuilder.like(custName.as(String.class), "传智播客%");
return like;
}
};
//List all = customerDao.findAll(specification);
//all.forEach(System.out::println);
Sort sort = new Sort(Sort.Direction.DESC,"custId");
List<Customer> all = customerDao.findAll(specification, sort);
all.forEach(System.out::println);
}
@Test
public void testPage() throws Exception{
Pageable pageable = new PageRequest(0,2);
Page<Customer> page = customerDao.findAll(null, pageable);
System.out.println(page.getTotalElements());
}
}
package cn.neu.test;
import cn.neu.dao.CustomerDao;
import cn.neu.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;
/**
* @Author WCJ
* @Description
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class CustomerTest2 {
@Autowired
private CustomerDao customerDao;
@Test
public void testJpql() throws Exception{
Customer c = customerDao.findJpql("传智播客");
System.out.println(c);
}
@Test
public void testJpql2() throws Exception{
Customer c = customerDao.findCustomerByCustIdAndCustName("传智播客", 1L);
System.out.println(c);
}
@Test
@Transactional
@Rollback(value = false)
public void testUpdate() throws Exception{
customerDao.updateCustomer("黑马",4L);
}
@Test
public void testFindAllSql() throws Exception{
List<Object[]> list = customerDao.findAllSql();
list.stream().forEach(objects -> System.out.println(Arrays.toString(objects)));
}
@Test
public void testFindAllSql2() throws Exception{
List<Object[]> list = customerDao.findAllSql2("传智播客%");
list.stream().forEach(objects -> System.out.println(Arrays.toString(objects)));
}
@Test
public void testName() throws Exception{
Customer c = customerDao.findByCustName("黑马");
System.out.println(c);
}
@Test
public void testName2() throws Exception{
List<Customer> c = customerDao.findByCustNameLike("传智播客%");
c.stream().forEach(System.out::println);
}
}
package cn.neu.test;
import cn.neu.dao.CustomerDao;
import cn.neu.domain.Customer;
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 org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @Author WCJ
* @Description
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class CustomerTest {
@Autowired
private CustomerDao customerDao;
@Test
public void testFindOne() throws Exception{
Customer c = customerDao.findOne(3L);
System.out.println(c);
}
@Test
public void testSave() throws Exception{
Customer c = new Customer();
c.setCustName("java工程师");
c.setCustLevel("高级");
c.setCustIndustry("电商");
customerDao.save(c);
}
@Test
public void testDelete() throws Exception{
customerDao.delete(2L);
}
@Test
public void testFindAll() throws Exception{
List<Customer> customers = customerDao.findAll();
customers.forEach(System.out::println);
}
/**
* 测试统计查询
* @throws Exception
*/
@Test
public void testCount() throws Exception{
long count = customerDao.count();
System.out.println(count);
}
@Test
public void testExist() throws Exception{
boolean exists = customerDao.exists(1L);
System.out.println(exists);
}
@Test
@Transactional
public void testGetOne() throws Exception{
Customer c = customerDao.getOne(1L);
System.out.println(c);
}
}