pom:
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--百度搜索-->
1.BaseDao
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
@NoRepositoryBean
public interface BaseRepo<T> extends JpaRepository<T,Integer>,JpaSpecificationExecutor<T>{
}
2.BaseEntity
import java.io.Serializable;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 5316912770609152144L;
}
3.BaseService
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
/**
* 底层封装Service
* 提供一些基本操作
* @author x_luwl
*
*/
@Transactional(rollbackFor = Exception.class)
public class BaseService<T extends BaseRepo<D>, D extends BaseEntity> {
private T dao;
protected void setDao(T dao) {
this.dao = dao;
}
public T getDao() {
return this.dao;
}
@Resource
@PersistenceContext
private EntityManager entityManager;
}