4.0.0
com.xwq
aisell
1.0-SNAPSHOT
war
aisell Maven Webapp
http://www.example.com
UTF-8
4.2.5.RELEASE
4.3.8.Final
1.9.0.RELEASE
2.5.0
1.6.1
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
${org.springframework.version}
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
org.apache.maven.plugins
maven-compiler-plugin
1.8
org.mortbay.jetty
jetty-maven-plugin
8.1.15.v20140411
9966
foo
/
由于domain中都有id属性,且为了方便以后扩展功能,抽取父类,并使用@MappedSuperclass标明这是一个父类
@MappedSuperclass
public class BaseDomain {
@Id
@GeneratedValue
protected Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
org.springframework.data
spring-data-jpa
${spring-data-jpa.version}
interface EmployeeRepository extends JpaRepository, JpaSpecificationExecutor
employeeRepository.findAll();
employeeRepository.findOne(Long id);
employeeRepository.save(对象); //添加或者修改
employeeRepository.delete(id/对象);
employeeRepository.findAll(Pageable) -> 分页
Pageable pageable = new PageRequest(0, 5); //0就是第一页
employeeRepository.findAll(Sort) -> 排序
Sort sort = new Sort(Sort.Direction.DESC,"username");
如果要把分页和排序结合起来: (把排序对象传到分页中)
new PageRequest(0, 5,sort);
findByUsername(String username) //根据名称查询 username = ?
findByUsernameLike(String username) //username like ?
findByUsernameLikeAndEmailLike(String username,String email) // username like ? and email like ?
//jpql
@Query("select o from Employee o where o.name like ?1")
@Query("select o from Employee o where o.name like :name")
//原生sql
@Query(nativeQuery = true,value="select * from employee")
可以不写SQL也能够完成功能
1.我们的Repository继承JpaSpecificationExecutor
EmployeeRepository extends JpaRepository,JpaSpecificationExecutor
2.findAll的查询方法
employeeRepository.findAll(Specification spec)
employeeRepository.findAll(Specification spec,Pageable pageable)
new Specification{
//root:(根,表)获取字段
//query:where,group by ,order by...
//cb: 条件判断(一个或者多个)
@Override
public Predicate toPredicate(Root root,
CriteriaQuery> query,
CriteriaBuilder cb) {
Path path = root.get("username");//拿到要做查询的字段
Predicate p = cb.like(path, "%1%");//like代表做模糊查询,后面就是它的条件值
return p;
}
}
com.github.wenhao
jpa-spec
3.1.0
//第一个参数: true -> 条件过滤启用
//第二个参数: 需要过滤的属性
//第三个参数: 过滤条件的 ?对应的值
Specification specification = Specifications.and()
.eq(StringUtils.isNotBlank(request.getName()), "name", name)
.gt(Objects.nonNull(request.getAge()), "age", 18)
.build();