您的位置:首页 > 编程语言

SSH项目搭建-03-DAO层,Controller层创建及代码抽取

2017-09-20 16:42 357 查看

1.DAO层抽取

创建抽取公共方法的接口ICommonDao和它的实现类CommonDaoImpl,DAO层所有接口继承ICommonDao,所有实现类继承CommonDaoImpl,这样就达到了抽取公共方法的目的。

public interface ICommonDao<T> {
void save(T entity);
void update(T entity);
T findObjectByID(Serializable id);
void deleteObjectByIds(Serializable... ids);
void deleteObjectByCollection(List<T> list);
List<T> findCollectionByConditionNoPage(String condition, Object[] params, Map<String, String> orderby);

}


public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {

/**T型转换*/
Class entityClass = TUtils.getActualType(this.getClass());

@Resource(name="sessionFactory")
public void setDi(SessionFactory sessionFactory){
this.setSessionFactory(sessionFactory);
}

/**保存*/
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}

/**更新*/
public void update(T entity) {
this.getHibernateTemplate().update(entity);
}

/**使用主键ID,查询对象*/
public T findObjectByID(Serializable id) {
return (T) this.getHibernateTemplate().get(entityClass, id);
}

/**删除(使用主键ID删除)*/
public void deleteObjectByIds(Serializable... ids) {
if(ids!=null && ids.length>0){
for(Serializable id:ids){
Object entity = this.findObjectByID(id);
this.getHibernateTemplate().delete(entity);
}
}
}

/**删除(使用集合List进行删除)*/
public void deleteObjectByCollection(List<T> list) {
this.getHib
4000
ernateTemplate().deleteAll(list);
}

/**数据库查询方法*/
public List<T> findCollectionByConditionNoPage(String condition,
final Object[] params, Map<String, String> orderby) {
/**
* 1.先写出hql语句的基本内容
*/
String hql = "from "+entityClass.getSimpleName()+" o where 1=1 ";
/**
* 2.添加查询结果的排序约束
* 将Map集合中存放的字段排序,组织成ORDER BY o.textDate ASC,o.textName DESC
*/
String orderbyCondition = this.orderbyHql(orderby);
/**
* 3.将各个语句结合拼装成最终的hql语句
*/
final String finalHql = hql + condition + orderbyCondition;
//查询,执行hql语句
List<T> list = this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Query query = session.createQuery(finalHql);
if(params!=null && params.length>0){
for(int i=0;i<params.length;i++){
query.setParameter(i, params[i]);
}
}
return query.list();
}

});
return list;
}

/**
* 将Map集合中存放的字段排序,组织成
*  ORDER BY o.textDate ASC,o.textName DESC
*
* map集合orderby内,key值为属性名,例如 o.textDate
*                 value值为排序方式,例如 DESC(降序)
*                 加起来组成的语句 o.textDate DESC 的含义是按textDate降序
*
*/
private String orderbyHql(Map<String, String> orderby) {
StringBuffer buffer = new StringBuffer("");
if(orderby!=null && orderby.size()>0){
buffer.append(" ORDER BY ");
for(Map.Entry<String, String> map:orderby.entrySet()){
buffer.append(map.getKey()+" "+map.getValue()+",");
}
//在循环后,删除最后一个逗号
buffer.deleteCharAt(buffer.length()-1);
}
return buffer.toString();
}
}


2.Controller层抽取

抽取模型驱动的实现方法getModel(),同时在utils模块中创建一个泛型转换的工具类TUtils

public class BaseAction<T> extends ActionSupport
implements ModelDriven<T>,ServletRequestAware,ServletResponseAware {

T entity;
protected HttpServletResponse response;
protected HttpServletRequest request;

public BaseAction() {
Class entityClass = TUtils.getActualType(this.getClass());
try{
entity = (T) entityClass.newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}

@Override
public T getModel() {
return entity;
}

@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
}

@Override
public void setServletResponse(HttpServletResponse httpServletResponse){
this.response = httpServletResponse;
}

}


public class TUtils {
public static Class getActualType(Class entity){
ParameterizedType parameterizedType = (ParameterizedType) entity.getGenericSuperclass();
Class entityClass = (Class) parameterizedType.getActualTypeArguments()[0];
return entityClass;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: