标签:
1. 所有dao和daoimpl模块都不用, 加上 @Deprecated
2. 建立DaoSupport类和DaoSupportImpl类
DaoSupport.java
package cn.itcast.oa.base;
import java.util.List;
public interface DaoSupport<T> {
void save(T entity);
void delete(Long id);
void update(T entity);
T getById(Long id);
List<T> getByIds(Long[] ids);
List<T> findAll();
}
DaoSupportImpl.java
package cn.itcast.oa.base; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory;
@SuppressWarnings("unchecked") public class DaoSupportImpl<T> implements DaoSupport<T> { @Resource private SessionFactory sessionFactory; private Class<T> clazz = null; public DaoSupportImpl(){ //使用反射技术得到T的真实类型 ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();//获取当前new的对象的泛型的父类类型 this.clazz = (Class<T>)pt.getActualTypeArguments()[0]; System.out.println("clazz===>"+clazz.getName()); System.out.println("clazz===>"+clazz.getSimpleName()); } protected Session getSession() { return sessionFactory.getCurrentSession(); } public void save(T entity) { getSession().save(entity); } public void update(T entity) { getSession().update(entity); } public void delete(Long id) { Object obj = getById(id); if (obj != null) { getSession().delete(obj); } } public T getById(Long id) { if(id == null) return null; else return (T) getSession().get(clazz, id); } public List<T> getByIds(Long[] ids) { return getSession().createQuery(// "FROM " + clazz.getSimpleName()+"WHERE id IN(:ids)")// .setParameterList("ids",ids). list(); } public List<T> findAll() { return getSession().createQuery(// "FROM " + clazz.getSimpleName()).// list(); } }
3. 抽取BaseAction, 这样每个action都可以专注写自己的方法
package cn.itcast.oa.base;
import java.lang.reflect.ParameterizedType;
import javax.annotation.Resource;
import cn.itcast.oa.service.DepartmentService;
import cn.itcast.oa.service.RoleService;
import cn.itcast.oa.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
protected T model ;
public BaseAction(){
try {
//通过反射活的model的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
Class<T> clazz = (Class<T>)pt.getActualTypeArguments()[0];
//通过反射创建model的实例
model = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException();
}
}
public T getModel() {
return model;
}
//******************service实例的声明******************
@Resource
protected RoleService roleService;
@Resource
protected DepartmentService departmentService;
@Resource
protected UserService userService;
}
整合完毕!!!
增加新模块的步骤:
一. 创建Action的准备
1. 写新的action, UserAction.java,
1) extends BaseAction
2) @Controller @Scope("prototype")
2. 定义出Action的方法, 写出方法名, 作用, 返回值
/** 列表 */
public String list() throws Exception {
return "list";
}
/** 删除 */
public String delete() throws Exception {
return "toList";
}
/** 添加页面 */
public String addUI() throws Exception {
return "saveUI";
}
/** 添加 */
public String add() throws Exception {
return "toList";
}
/** 修改页面 */
public String editUI() throws Exception {
return "saveUI";
}
/** 修改 */
public String edit() throws Exception {
return "toList";
}
/** 初始化密码1234 **/
public String initPassword() throws Exception {
return "toList";
}
3. 创建所用到的jsp页面(list.jsp, saveUI.jsp)
4. struts.xml配置
<!-- 用户管理 -->
<action name="user_*" class="userAction" method="{1}">
<result name="list">/WEB-INF/jsp/userAction/list.jsp</result>
<result name="saveUI">/WEB-INF/jsp/userAction/saveUI.jsp</result>
<result name="toList" type="redirectAction">user_list?parentId=${parentId}</result>
</action>
二. 准备service
1. 创建接口UserService.java, extends DaoSupport<User>
package cn.itcast.oa.service;
import cn.itcast.oa.base.DaoSupport;
import cn.itcast.oa.domain.User;
public interface UserService extends DaoSupport<User>{
}
2. 创建实现类 UserServiceImpl.java , extends DaoSupportImpl<User> implements UserService
3. 配置: 在UserServiceImpl上写注解 @Service @Transactional
package cn.itcast.oa.service.impl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.oa.base.DaoSupportImpl;
import cn.itcast.oa.domain.User;
import cn.itcast.oa.service.UserService;
@Service
@Transactional
public class UserServiceImpl extends DaoSupportImpl<User> implements UserService{
}
4. BaseAction里写service的声明
package cn.itcast.oa.base;
import java.lang.reflect.ParameterizedType;
import javax.annotation.Resource;
import cn.itcast.oa.service.DepartmentService;
import cn.itcast.oa.service.RoleService;
import cn.itcast.oa.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
protected T model ;
public BaseAction(){
try {
//通过反射活的model的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
Class<T> clazz = (Class<T>)pt.getActualTypeArguments()[0];
//通过反射创建model的实例
model = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException();
}
}
public T getModel() {
return model;
}
//******************service实例的声明******************
@Resource
protected RoleService roleService;
@Resource
protected DepartmentService departmentService;
@Resource
protected UserService userService;
}
标签:
原文地址:http://www.cnblogs.com/wujixing/p/5511045.html