DAOFactory设计模式+Hibernate

--------------------------------------------------------------------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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值