Java Web Dao層設計
UML設計圖
實體類
package cn.zzuli.oa.domain;
public class Role {
}
package cn.zzuli.oa.domain;
public class User {
private Long id;
private String username;
public User() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
BaseDao接口
package cn.zzuli.oa.base;
import java.util.List;
public interface BaseDao<T> {
/**
* 保存實體
* @param entity
*/
void save(T entity);
/**
* 刪除實體
* @param id
*/
void delete(Long id);
/**
* 更新實體
* @param entity
*/
void update(T entity);
/**
*
* @param id
* @return
*/
T getById(Long id);
/**
* 查詢實體
* @param ids id的集合
* @return
*/
List<T> listByIds(Long[] ids);
/**
* 查詢所有
* @return
*/
List<T> listAll();
}
BaseDaoImpl代碼,實現(xiàn)BaseDao接口
package cn.zzuli.oa.base.impl;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import cn.zzuli.oa.base.BaseDao;
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> implements BaseDao<T> {
@Resource
private SessionFactory sessionFactory;
protected Class<T> clazz;
public BaseDaoImpl() {
//通過反射得到T的真實類型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();//獲取這個父類的泛型類型
this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; //獲取第一個泛型T的類型
System.out.println("clazz = " + clazz.getName());
}
@Override
public void save(T entity) {
getSession().save(entity);
}
@Override
public void delete(Long id) {
Object obj = getSession().get(clazz, id);
getSession().delete(obj);
}
@Override
public void update(T entity) {
getSession().update(entity);
}
@Override
public T getById(Long id) {
return (T) getSession().get(clazz, id);
}
@Override
public List<T> listByIds(Long[] ids) {
if (ids == null || ids.length == 0) {
return Collections.EMPTY_LIST;
}
return getSession().createQuery("FROM " + clazz.getSimpleName() + " WHERE id IN(:ids)")
.setParameterList("ids", ids).list();
}
@Override
public List<T> listAll() {
return getSession().createQuery("FROM " + clazz.getSimpleName()).list();
}
/**
* 獲取當前可用的Session
*
* @return 當前可用的Session
*/
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
}
RoleDao接口,繼承BaseDao
package cn.zzuli.oa.dao;
import cn.zzuli.oa.base.BaseDao;
import cn.zzuli.oa.domain.Role;
public interface RoleDao extends BaseDao<Role> {
}
UserDao接口,繼承BaseDao
UserDao實現(xiàn)類UserDaoImpl可以實現(xiàn)的自己的功能代碼
package cn.zzuli.oa.dao;
import cn.zzuli.oa.base.BaseDao;
import cn.zzuli.oa.domain.User;
public interface UserDao extends BaseDao<User>{
}
RoleDaoImpl代碼,繼承BaseDaoImpl抡句,實現(xiàn)RoleDao
package cn.zzuli.oa.dao.impl;
import org.springframework.stereotype.Repository;
import cn.zzuli.oa.base.impl.BaseDaoImpl;
import cn.zzuli.oa.dao.RoleDao;
import cn.zzuli.oa.domain.Role;
@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao {
}
UserDaoImpl代碼,繼承BaseDaoImpl杠愧,實現(xiàn)UserDao
package cn.zzuli.oa.dao.impl;
import org.springframework.stereotype.Repository;
import cn.zzuli.oa.base.impl.BaseDaoImpl;
import cn.zzuli.oa.dao.UserDao;
import cn.zzuli.oa.domain.User;
@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{
}
測試類
package cn.zzuli.oa.base;
import org.junit.Test;
import cn.zzuli.oa.dao.RoleDao;
import cn.zzuli.oa.dao.UserDao;
import cn.zzuli.oa.dao.impl.RoleDaoImpl;
import cn.zzuli.oa.dao.impl.UserDaoImpl;
import cn.zzuli.oa.domain.Role;
import cn.zzuli.oa.domain.User;
public class BaseDaoTest {
@Test
public void testGetById() {
UserDao userDao = new UserDaoImpl();
RoleDao roleDao = new RoleDaoImpl();
}
}
實現(xiàn)效果
此處輸出的正是BaseDao實現(xiàn)類BaseDaoImple中構造方法獲取的泛型的類型待榔,