--------------------------------------------------------------------VO-------------------------------------------------------------------------
package vo;
public class User {
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
------------------------------------------------------------DAO-----------------------------------------------------------------------------------
package dao;
import vo.User;
public interface Userdao
{
public boolean dologin(User user);
}
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import javax.naming.NamingException;
import org.hibernate.Query;
import org.hibernate.Session;
import daofactory.DefaultSessionFactory;
import vo.User;
public class Userdaoimpl implements Userdao
{
private Session session;
public Userdaoimpl()
{
this.session = DefaultSessionFactory.getSession();
}
public boolean dologin(User user)
{
boolean flag = false ;
// String hql = "FROM User AS p WHERE p.username='";
// hql=hql+user.getUsername()+"' AND p.password='"+user.getPassword()+"'";
String hql = "from User where username = ? and password= ?";
Query q = this.session.createQuery(hql);
q.setString(0, user.getUsername());
q.setString(1, user.getPassword());
System.out.println(q.getQueryString());
List l = q.list();
Iterator iter = l.iterator();
if(iter.hasNext())
{
flag = true ;
}
// 关闭session连接
this.session.close() ;
return flag;
}
}
---------------------------------------------------------------DAOFactory---------------------------------------------------------------------
package daofactory;
import java.sql.SQLException;
import javax.naming.NamingException;
import dao.Userdao;
import dao.Userdaoimpl;
public class daofactory {
private static daofactory instance = null;
private daofactory()
{
}
public static daofactory getInstance()
{
if ( instance == null )
instance = new daofactory();
return instance;
}
public Userdao getlogin() throws NamingException, SQLException
{
return new Userdaoimpl();
}
}
-------------------------------------------------------------DefaultSessionFactory ------------------------------------------------------
package daofactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class DefaultSessionFactory {
public static Session getSession() {
Session session = null;
session = new Configuration().configure().buildSessionFactory()
.openSession();
return session;
}
}
<!-- 以下一句话为解决weblogic与hibernate冲突 -->
<property name="query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</property>