一:项目搭建
1.使用ideal搭建项目—maven项目选择webapp
2. 完整结构为
3. 导入项目需要的jar依赖
UTF-8
1.8
1.8
4.2.5.RELEASE
4.3.8.Final
1.9.0.RELEASE
2.5.0
1.6.3
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-context-support
${org.springframework.version}
事务需要
org.springframework
spring-tx
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
test
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-webmvc
4.2.4.RELEASE
org.apache.commons
commons-io
1.3.2
commons-fileupload
commons-fileupload
1.2.2
com.fasterxml.jackson.core
jackson-core
${com.fasterxml.jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${com.fasterxml.jackson.version}
com.fasterxml.jackson.core
jackson-databind
${com.fasterxml.jackson.version}
org.hibernate
hibernate-core
${org.hibernate.version}
org.hibernate
hibernate-entitymanager
${org.hibernate.version}
org.springframework.data
spring-data-jpa
${spring-data-jpa.version}
com.github.wenhao
jpa-spec
3.1.1
*
*
commons-dbcp
commons-dbcp
1.2.2
mysql
mysql-connector-java
5.1.6
org.apache.commons
commons-lang3
3.5
junit
junit
4.12
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.slf4j
slf4j-api
${org.slf4j.version}
org.slf4j
slf4j-log4j12
${org.slf4j.version}
runtime
log4j
log4j
1.2.14
org.apache.velocity
velocity
1.6
org.apache.shiro
shiro-all
1.4.0
pom
org.apache.shiro
shiro-spring
1.4.0
org.apache.poi
poi
3.11
org.apache.poi
poi-ooxml
3.11
net.coobird
thumbnailator
0.4.6
quartz
quartz
1.5.2
javax.mail
mail
1.4.1
`
4 创建applicationContext.xml配置文件配置如下
6 完成实体类—由于主键id很多类都需要,抽取一个父类专门实现主键id
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/*
使用@MappedSuperclass注解,通过这个注解,我们可以将该实体类当成基类实体
在这里插入代码片,它不会映射到数据库表,
但继承它的子类实体在映射时会自动扫描该基类实体的映射属性,
添加到子类实体的对应数据库表中。
*/
@MappedSuperclass
public class BaseDomain {
@Id
@GeneratedValue
protected Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
7 Employee实体类类实现
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee extends BaseDomain {
//雇员姓名
private String username;
//雇员密码
private String password;
//雇员邮件
private String email;
//员工年龄
private Integer age;
@Override
public String toString() {
return "Employee{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", age=" + age +
", id=" + id +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
8 Repository(也就是Dao)层实现----SpringDataJPA进一步封装JPA,只需写一个接口继承一个接口即可完成crud操作。
public interface EmployeeRepository extends JpaRepository{
}
9 通过8可以完成一些简单的crud即分页排序与高级查询(通过根据规则写查询方法或者使用@Query注解)
规则如下:
public interface EmployeeRepository extends JpaRepository, JpaSpecificationExecutor {
//通过定义的规则提供简单查询
List findByUsernameLike(String username);
//使用query注解使用jpql或者sql语句查询
//@Query("select o from Employee o where o.username like ?1")
@Query(value = "select * from employee where username like ?",nativeQuery = true)
List findUsername(String name);
List findByUsernameLikeAndEmailLike(String username,String email);
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestEmployee {
@Autowired
private EmployeeRepository employeeRepository;
/*查询数据*/
@Test
public void testFind() throws Exception{
List list = employeeRepository.findAll();
list.forEach(e -> System.out.println(e));
}
//添加数据
@Test
public void testAdd() throws Exception{
Employee employee = new Employee();
employee.setUsername("张三");
employee.setAge(12);
employee.setEmail("[email protected]");
employeeRepository.save(employee);
}
//修改数据
@Test
public void testUpdate() throws Exception{
Employee employee = new Employee();
employee.setUsername("张三");
employee.setAge(12);
employee.setId(274L);
employee.setEmail("[email protected]");
employeeRepository.save(employee);
}
//删除数据
@Test
public void testDelete() throws Exception{
employeeRepository.delete(274L);
}
//分页
@Test
public void testPage() throws Exception{
/*当前页从0开始*/
Pageable pageable=new PageRequest(0, 10);
Page list = employeeRepository.findAll(pageable);
list.forEach(e-> System.out.println(e));
}
//排序
@Test
public void testOrder() throws Exception{
Sort sort=new Sort(Sort.Direction.ASC,"username");
List li = employeeRepository.findAll(sort);
li.forEach(e-> System.out.println(e));
}
10 但是一些高级查询还是要写查询语句----这里继承JpaSpecificationExecutor接口可以不用写jpql便能完成高级查询,使用方法如下
//查询+排序+分页
@Test
public void testQuery3() throws Exception{
Sort sort=new Sort(Sort.Direction.ASC, "username");
Pageable pageable=new PageRequest(0, 10,sort);
Page username = employeeRepository.findAll(new Specification() {
/*
* root相当于类或者表
* cb相当于查询条件
* */
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
//拿到要查询的字段
Path path = root.get("username");
//设置查询的值
Predicate like = cb.like(path, "%1%");
return like;
}
},pageable/*sort*/);
username.forEach(e-> System.out.println(e));
}
11 发现代码还是有点多—这里用了文浩大佬集成的一个jpa-specbao
导入依赖
com.github.wenhao
jpa-spec
3.1.1
*
*
发现代码变得简单多了(具体使用请进入https://github.com/wenhao/jpa-spec查看)
//jpa-spec 分页+排序+查询
@Test
public void testQuery4() throws Exception{
Sort sort=new Sort(Sort.Direction.ASC, "username");
Pageable pageable=new PageRequest(0, 10,sort);
Specification username = Specifications.and()
.like("username","%1%")
.build();
Page list = employeeRepository.findAll(username,pageable);
list.forEach(e-> System.out.println(e));
}
12 这里我们发现如果前台传入查询条件我们就必须要写一个类去接收
父类实现一些公共的查询条件(比如当前页,每页分页条数,排序类型–升序/降序,排序字段)
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
/*提供公共的查询条件
* 规范
* */
public abstract class BaseQuery {
/*一些公共的查询条件*/
//当前页
private int currentPage=1;
//当前页数量
private int pageSize=5;
//排序名称
private String OrderByname;
//排序类型
private String OrderByType= "ASC";
//获得从0开始的分页
public int getJpaCurrentPage(){
return this.currentPage-1;
}
/*规定子类必须实现这个方法,用来获得Specification对象*/
protected abstract Specification getSpec();
/*获得排序*/
public Sort getSort(){
Sort sort=null;
Sort.Direction desc = Sort.Direction.DESC;
if(StringUtils.isNoneBlank(OrderByname)){
if("ASC".equals(OrderByType.toUpperCase())){
//如果用户传入的是ASC
desc=Sort.Direction.ASC;
}
sort=new Sort(desc,OrderByname);
}
return sort;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int curentPage) {
this.currentPage = curentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getOrderByname() {
return OrderByname;
}
public void setOrderByname(String orderByname) {
OrderByname = orderByname;
}
public String getOrderByType() {
return OrderByType;
}
public void setOrderByType(String orderByType) {
OrderByType = orderByType;
}
}
import cn.itsource.aisale.domain.Employee;
import com.github.wenhao.jpa.Specifications;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
public class EmployeeQuery extends BaseQuery {
/*写自己独有的查询条件*/
private String username;
private String email;
private Integer age;
public String getUsername() {
return username;
}
//获得Specifications
public Specification getSpec(){
Specification spec = Specifications.and()
/*问题:
* 因为无法判断用户是否传入条件,需要判断条件是否为空
* */
.like(StringUtils.isNoneBlank(username),"username",username)
.like(StringUtils.isNoneBlank(email),"email", email)
.lt(age!=null,"age",age)
.build();
return spec;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}