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<Users> findAll();
public List<Users> 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<Users> findAll() {
return usersDAO.findAll();
}
public List<Users> 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<Users> userList=new ArrayList<Users>();//用於查詢
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<Users> getUserList() {
return userList;
}
public void setUserList(List<Users> userList) {
this.userList = userList;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
}