我只发了源码和配置文件 jsf用的1。1 hibernate用的3。1
数据库有个l_user表 有username和password两个字段
我只发了源码和配置文件 jsf用的1。1 hibernate用的3。1
数据库有个l_user表 有username和password两个字段
package org.sunme.model.impl;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.sunme.model.LUser;
import org.sunme.model.LUserDAO;
import org.sunme.model.hibernate.HibernateSessionFactory;
public class LUserDAOImpl implements LUserDAO {
private Session session=HibernateSessionFactory.getSession();
private Transaction tx=session.beginTransaction();
public List<LUser> getByName(String username) {
String sql="from LUser user where username=:name";
Query query=session.createQuery(sql);
query.setParameter("name", username);
return query.list();
}
public boolean save(LUser user) {
if(user==null)
return false;
else{
session.save(user);
tx.commit();
return true;
}
}
}
package org.sunme.service.impl;
import java.util.Iterator;
import java.util.List;
import org.sunme.model.LUser;
import org.sunme.model.LUserDAO;
import org.sunme.model.impl.LUserDAOImpl;
import org.sunme.service.LUserService;
public class LUserServiceImpl implements LUserService {
private LUserDAO dao=new LUserDAOImpl();
private List<LUser> list;
public boolean login(String username,String password) {
boolean b=false;
list=(List<LUser>)dao.getByName(username);
if(list!=null){
Iterator it=list.iterator();
while(it.hasNext()){
LUser user=(LUser)it.next();
if(user.getPassword().equals(password))
b=true;
break;
}
}
else
b=false;
return b;
}
public boolean sign(String username,String password) {
list=(List<LUser>)dao.getByName(username);
boolean b=true;
Iterator it=list.iterator();
while(it.hasNext()){
LUser user=(LUser)it.next();
if(user.getUsername().equals(username)){
b=false;
}
}
if(b){
LUser userbean=new LUser();
userbean.setUsername(username);
userbean.setPassword(password);
b=dao.save(userbean);
}
return b;
}
}
package org.sunme.view;
import org.sunme.service.LUserService;
import org.sunme.service.impl.LUserServiceImpl;
public class UserBean {
private String username;
private String password;
private LUserService service=new LUserServiceImpl();
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String login(){
if(service.login(username, password))
return "success";
else
return "failure";
}
public String sign(){
if(service.sign(username, password))
return "success";
else
return "failure";
}
}