展开全部
这里只写了DAO和业务62616964757a686964616fe59b9ee7ad9431333264623331逻辑组件、ACTION的具体实现类,PO和和接口自己应该会写吧,HQL采用的是结合SQL的那种写法,增删改查全在里面了,修改下马上就能跑了,不清楚再问,我详细解答,因为这个是我第一给回答问题,分给我吧。呵呵
package dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
//根据标识属性加载User实例
public User get(String sysid)
{
return (User)getHibernateTemplate().get(User.class, sysid);
}
//持久化指定的User实例
public String save(User user)
{
return (String)getHibernateTemplate().save(user);
}
public void update(User user)
{
getHibernateTemplate().update(user);
}
public void delete(User user)
{
getHibernateTemplate().delete(user);
}
public void delete (String sysid)
{
getHibernateTemplate().delete(get(sysid));
}
public List findAll(String hsql) {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Query query = session.createQuery(hsql);
query.setFirstResult(0);
query.setMaxResults(10);
List list = (List) query.list();
session.close();
return list;
}
public List findUserByRequest(User user) {
// TODO Auto-generated method stub
return null;
}
}
package service.impl;
import java.util.List;
import pojo.User;
import dao.impl.UserDao;
public class UserServiceImpl implements UserService
{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public User save(User user) {
userDao.save(user);
return user;
}
public User delete(String sysid) {
userDao.delete(sysid);
return userDao.get(sysid);
}
public List findAllUser(User user) {
String hsql="from User";
return userDao.findAll(hsql);
}
public User update(User user) {
userDao.update(user);
return user;
}
public List findUserByRequest(User user) {
String hsql="from User ";
String where = " where 1=1";
if (user.getSysid()!=null && !"".equals(user.getSysid())) {
where+=" and sysid='"+user.getSysid()+"'";
}
if(null!=user.getName() && !"".equals(user.getName())){
where+=" and name='"+user.getName()+"'";
}
hsql+=where;
return userDao.findAll(hsql);
}
public User delete(User user) {
userDao.delete(user);
return user;
}
}
//ACTION
package action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import pojo.User;
import service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private User user;
private UserService userServer;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserServer() {
return userServer;
}
public void setUserServer(UserService userServer) {
this.userServer = userServer;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return findAll();
}
// public String list(){
public String findAll(){
List list = userServer.findAllUser(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String findUserByRequest(){
List list = userServer.findUserByRequest(user);
ServletActionContext.getRequest().setAttribute("list", list);
return "list";
}
public String add()
{
return "add";
}
public String addSure()
{
userServer.save(user);
return "list";
}
public String deleteBySysid()
{
userServer.delete(user);
return "list";
}
public String showUpdate() {
List list = userServer.findUserByRequest(user);
user = list.get(0);
ServletActionContext.getRequest().setAttribute("list", list);
return "update";
}
public String updateSure() {
userServer.update(user);
return "list";
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起