这是我们团队小项目中设计的通用BaseDao:
public interface BaseDao {
/**
* 添加实体类
* @param t
* @return
*/
public T add(T t);
/**
* 更新实体类
* @param t
* @return
*/
public T update(T t);
/**
* 根据主键ID删除实体类
* @param id
*/
public void delete(Serializable id);
/**
* 根据JPQL语句更新或删除操作
* @param jpql
* @param obj
*/
public int executeUpdate(String jpql,Object... obj);
/**
* 根据主键ID查找单个实体类
* @param id
* @return
*/
public T load(Serializable id);
/**
* 根据JPQL语句查询单个实体类
* @param jpql
* @param obj(参数可有可无)
* @return
*/
public T load(String jpql,Object... obj);
/**
* 查找所有的实体类
* @return
*/
public List findAll();
/**
* 根据JPQL语句查询集合实体类
* @param jpql
* @param obj(参数可有可无)
* @return
*/
public List find(String jpql,Object... obj);
/**
* 聚合查询
* @param jpql
* @param obj
* @return
*/
public Object findByAggregate(String jpql,Object... obj);
/**
* 查找总记录数
* @return
*/
public int count();
/**
* 根据JPQL语句查询记录数
* @param jpql
* @param obj
* @return
*/
public int count(String jpql,Object... obj);
/**
* 分页查询集合实体类
* @param firstIndex
* @param maxResults
* @return
*/
public List findPage(Integer firstIndex, Integer maxResults);
/**
* 根据JPQL语句查询集合实体类
* @param firstIndex
* @param maxResults
* @param jpql
* @param obj
* @return
*/
public List findPage(Integer firstIndex, Integer maxResults,String jpql,Object... obj);
public Page findPage(Page page,String jpql,Object... obj);
}
BaseDao的基础实现类AbstractBaseDao:
public abstract class AbstractBaseDao implements BaseDao {
@PersistenceContext
private EntityManager em;
private Class clazz = null;
public AbstractBaseDao(){
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
this.clazz = (Class) pt.getActualTypeArguments()[0];
}
@Override
public T add(T t) {
em.persist(t);
return t;
}
@Override
public T update(T t) {
return em.merge(t);
}
@Override
public void delete(Serializable id) {
T t = em.getReference(clazz, id);
em.remove(t);
}
@Override
public int executeUpdate(String jpql, Object... obj) {
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
return query.executeUpdate();
}
@Override
public T load(Serializable id) {
return em.find(clazz, id);
}
@Override
public T load(String jpql, Object... obj) {
try{
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
return (T) query.getSingleResult();
}catch (Exception e){
return null;
}
}
@Override
public List findAll() {
return em.createQuery("from "+clazz.getSimpleName()).getResultList();
}
@Override
public List find(String jpql, Object... obj) {
try{
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
return query.getResultList();
}catch (Exception e){
return null;
}
}
@Override
public Object findByAggregate(String jpql, Object... obj) {
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
Object result = query.getSingleResult();
return result;
}
@Override
public int count() {
Long num = (Long) em.createQuery("select count(*) from "+clazz.getSimpleName()).getSingleResult();
return num.intValue();
}
@Override
public int count(String jpql,Object... obj) {
try{
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
Long num = (Long)query.getSingleResult();
return num.intValue();
}catch (Exception e){
return 0;
}
}
@Override
public List findPage(Integer firstIndex, Integer maxResults) {
return em.createQuery("from "+clazz.getSimpleName()).setFirstResult(firstIndex).setMaxResults(maxResults).getResultList();
}
@Override
public List findPage(Integer firstIndex, Integer maxResults,String jpql,Object... obj) {
try{
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
query.setFirstResult(firstIndex).setMaxResults(maxResults);
return query.getResultList();
}catch (Exception e){
return null;
}
}
@Override
public Page findPage(Page page, String jpql, Object... obj) {
Query query = em.createQuery(jpql);
if(obj.length > 0){
for (int i = 0; i < obj.length; i++) {
query.setParameter((i+1),obj[i]);
}
}
int total = ((Long)query.getSingleResult()).intValue();
page.setItemTotal(total);
query.setFirstResult(page.getFirstIndex()).setMaxResults(page.getPageCount());
List list = query.getResultList();
page.setItems(list);
return page;
}
}
这里我们AbstractBaseDao设计为abstract ,因为我们不想让他被实例化。IOC作为Spring中的核心技术之一使用它可以实现完全的面向接口编程,所以我们需要设计接口。这样我们就可以在service层中使用接口注入。
现在我们来设计接口,如果我现在想要做一个订单的持久层OrderDao要怎么做。
public interface OrderDao extends BaseDao {
List findOrderByStoreId(String storeId);
List findOrderByStatus(String storeId,Integer status);
int getActiveOrderCount(String storeId);
int getNewOrderCount(String storeId);
int getOrderCount(String storeId);
List findOrderByStoreId(String storeId,Integer page, Integer pageSize);
List findOrderByStatus(String storeId, Integer status, Integer page, Integer pageSize);
int getOrderByStatusCount(String storeId, Integer status);
}
然后写一个实现OrderDao的OrderDaoImpl:
@Repository
public class OrderDaoImpl extends AbstractBaseDao implements OrderDao {
public List findOrderByStoreId(String storeId){
List orders = this.find("SELECT o FROM Order o WHERE o.storeId = ? order by o.createTime desc", storeId);
return orders;
}
@Override
public List findOrderByStatus(String storeId,Integer status) {
List orders = this.find("SELECT o FROM Order o WHERE o.storeId = ? and o.status = ? order by o.createTime desc", storeId, status);
return orders;
}
@Override
public int getActiveOrderCount(String storeId) {
return this.count("SELECT count(o) FROM Order o WHERE o.storeId = ? and o.status in (1,2)", storeId);
}
@Override
public int getNewOrderCount(String storeId) {
return this.count("SELECT count(o) FROM Order o WHERE o.storeId = ? and o.status = 0", storeId);
}
@Override
public int getOrderCount(String storeId) {
return this.count("SELECT count(o) FROM Order o WHERE o.storeId = ?", storeId);
}
@Override
public List findOrderByStoreId(String storeId, Integer page, Integer pageSize) {
return this.findPage((page -1) * pageSize, pageSize, "SELECT o FROM Order o WHERE o.storeId = ? order by o.createTime desc, o.id", storeId);
}
@Override
public List findOrderByStatus(String storeId, Integer status, Integer page, Integer pageSize) {
return this.findPage((page -1) * pageSize, pageSize, "SELECT o FROM Order o WHERE o.storeId = ? and o.status = ? order by o.createTime desc, o.id", storeId, status);
}
@Override
public int getOrderByStatusCount(String storeId, Integer status) {
return this.count("SELECT count(o) FROM Order o WHERE o.storeId = ? and o.status = ?", storeId, status);
}
}
然后我们在service层中就可以直接注入。
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
//.....
}
这样就可以完全实现面向接口编程。