package com.dao;
import java.util.List;
import com.entity.TblUsers;
public interface UsersDao {
public TblUsers login(String username);
public List<TblUsers> findAllUsers();
}
-----------------------------------------------------------------------------------
package com.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dao.UsersDao;
import com.entity.TblUsers;
public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{
public TblUsers login(String username) {
//获取会话,有session工厂才有session
Session session=this.getSession();
//创建查询,从属性或者类
String hql="from TblUsers where username = ?";//是类型而不是表名
TblUsers user=null;
try {
Query query=session.createQuery(hql);
query.setParameter(0, username);
user = (TblUsers)query.uniqueResult();//用来接收,查数据,返回一个
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
return user;
}
public List<TblUsers> findAllUsers() {
Session session=this.getSession();
String hql="from TblUsers";
List<TblUsers> ulist=null;
try {
Query query=session.createQuery(hql);
ulist=query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return ulist;
}
}
------------------------------------------------------------------------------------------
package com.service;
import java.util.List;
import com.entity.TblUsers;
public interface UsersService {
TblUsers login(String username);
List<TblUsers> findAllUsers();
}
-----------------------------------------------------------------------------------
package com.service.impl;
import java.util.List;
import com.dao.UsersDao;
import com.entity.TblUsers;
import com.service.UsersService;
public class UsersServiceImpl implements UsersService{
private UsersDao usersDao;
public UsersDao getUsersDao() {
return usersDao;
}
public void setUsersDao(UsersDao usersDao) {
this.usersDao = usersDao;
}
public TblUsers login(String username){
return usersDao.login(username);
}
public List<TblUsers> findAllUsers() {
return usersDao.findAllUsers();
}
}
------------------------------------------------------------------------------
package com.action;
import java.util.Map;
import com.entity.TblUsers;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UsersService;
public class UsersAction extends ActionSupport{
private UsersService usersService;
private String username;
private String password;
private String errMsg;
private Map<String,Object> session;
public String login(){
TblUsers user=usersService.login(username);
if(user!=null){
//判断密码是否匹配
if(user.getPassword().equals(password)){
session=ActionContext.getContext().getSession();
session.put("userinfo", user);
return SUCCESS;
}else{
this.errMsg="用户名或密码不匹配";
return INPUT;
}
}else{
//用户不存在
this.errMsg="用户不存在";
return INPUT;
}
}
public UsersService getUsersService() {
return usersService;
}
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
}