1. 分包
a) 在src文件夹下,创建包结构:model(实体类)、dao(数据库访
问类)、service(业务逻辑类)、action(控制类)
2. model、dao生成
利用Myeclipse生成model和dao(最好自己构建可以减少冗余代码)
转到MyEclipse数据库浏览视图下,双击sshteat001进行连接
右键点击users表,选择Hibernate反向工程
选择生成目录为app.model,勾选创建POJO即创建表对应的Javabean,
同时创建映射文件hbm.xml,勾选更新hibernate配置,勾选创建spring
dao。
下一步,选择id生成策略为native,因为users主键自增,或者在下一
步中选择也可以
点击完成,回到代码视图查看工程变化,model包下面增加了
Users.java、UsersDAO.java、Users.hbm.xml三个文件
将UsersDAO移动到dao包中,并抽出接口IUsersDAO
3. service、action编写
a)编写service层代码,创建IUsersService接口和UsersService实现类
,编写CRUD的方法。
package service;import java.util.List;import model.Users;
public interface IUsersService { public List findAll(); public List findByName(String username); public Users findByUid(Integer uid); public void addUsers(Users u) throws Exception ; public void updateUsers(Users u) throws Exception ; public void delUsers(Integer uid) throws Exception ; public Users login(String username,String password);
}
----------------------package service;
import java.util.List;
import model.Users;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import dao.IUsersDAO;import dao.UsersDAO;
public class UsersService implements IUsersService { private static final Logger log = LoggerFactory.getLogger
(UsersService.class); private IUsersDAO usersDAO=null;
public void setUsersDAO(IUsersDAO usersDAO) { this.usersDAO = usersDAO; }
public void addUsers(Users u) throws Exception { usersDAO.save(u); }
public void delUsers (Integer uid) throws Exception { usersDAO.delete(findByUid(uid)); }
public List findAll() { return usersDAO.findAll(); }
public List findByName(String username) { return usersDAO.findByUsername(username); }
public Users findByUid(Integer uid) { return usersDAO.findById(uid); }
public Users login(String username, String password) { List list = usersDAO.findByUsername(username);
if(null==list||list.size()==0) return null; Users u=(Users) usersDAO.findByUsername(username).get
(0); if(null==u) return null; if(username.equals(u.getUsername())&&password.equals
(u.getPassword())) return u; else return null; }
public void updateUsers (Users u) throws Exception { usersDAO.attachDirty(u); }}
b) 编写action层UsersAction代码,用于对用户增删改查进行处理
package action;import java.util.ArrayList;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import model.Users;import service.IUsersService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UsersAction extends ActionSupport{
private static final Logger log = LoggerFactory.getLogger
(UsersAction.class); private Integer uid=new Integer(0);// 用于修改删除时传参数 private Users u=new Users();//用于增改 private List userList=new ArrayList();//用于查询 private IUsersService usersService=null; public String addUser() throws Exception{ try { usersService.addUsers(u); } catch (Exception e) { log.error("保存用户错误",e); return ERROR; } return SUCCESS; }
public String listUser() throws Exception{ userList=usersService.findAll(); return SUCCESS; }
public String editUser() throws Exception{ try { u=usersService.findByUid(uid); } catch (Exception e) { log.error("修改用户错误",e); return ERROR; } return SUCCESS; }
public String updateUser() throws Exception{ try { usersService.updateUsers(u); } catch (Exception e) { log.error("更新用户错误",e); return ERROR; } return SUCCESS; }
public String delUser() throws Exception{ try { usersService.delUsers(uid); } catch (Exception e) { log.error("删除用户错误",e); return ERROR; } return SUCCESS; }
public String login() throws Exception{ try { Users loginUser=usersService.login(u.getUsername(),
u.getPassword()); if(null!=loginUser){ ActionContext context=ActionContext.getContext
(); context.getSession().put("LOGINUSER",loginUser); return SUCCESS; } } catch (Exception e) { log.error("登录查询错误",e); return ERROR; } return INPUT; }
public void setUsersService(IUsersService usersService) { this.usersService = usersService; } public Users getU() { return u; } public void setU(Users u) { this.u = u; }
public List getUserList() { return userList; }
public void setUserList(List userList) { this.userList = userList; }
public Integer getUid() { return uid; }
public void setUid(Integer uid) { this.uid = uid; }}