SSH(Spring + Struts + Hibernate)—— 中

8、编写实现类

三种结合的实现形式:

1、  手工编写实现类,并定义SessionFactory属性,但需要手工进行Session的打开关闭,而且需要通过Spring注入SessionFactory对象

2、  继承HibernateDaoSupport,并通过Spring注入了SessionFactory,使用该方法对于c3p0的连接池,可以直接通过getSession 进行处理,但如果使用common-pool的连接池则还需要手工关闭连接。

3、  继承HibernateDaoSupport,并注入了hibernateTemplate属性,使用该方法可以完美的关闭和处理事务,但需要通过gethibernateTemplate来进行操作,而不能直接使用getSession进行处理。


第一种方法:手工编写实现类(开发不用)



package mldn.lin.dao.impl;



import java.util.List;



import mldn.lin.dao.NewsDAO;

import mldn.lin.pojo.News;



import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;



public class NewsDAOImpl implements NewsDAO {

  

    private SessionFactory sessionFactory;



    public void setSessionFactory(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }



    public boolean doInsert(News news) throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       session.save(news);

       session.beginTransaction().commit();

       session.close();

       return true;

    }



    public boolean doRemove(int id) throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       session.delete(findById(id));

       session.beginTransaction().commit();

       session.close();

       return true;

    }



    public boolean doUpdate(News news) throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       session.update(news);

       session.beginTransaction().commit();

       session.close();

       return true;

    }



    public List<News> findAll() throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       List all = session.createCriteria(News.class).list();

       session.close();

       return all;

    }



    public List<News> findAll(int cp, int ls, String keyword) throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       String hql="FROM News WHERE title=? OR abstractnote=? OR content=?";

       Query q=session.createQuery(hql);

       q.setString(0, "%"+keyword+"%");

       q.setString(1, "%"+keyword+"%");

       q.setString(2, "%"+keyword+"%");

       List<News> all=q.list();

       session.close();

       return all;

    }



    public News findById(int id) throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       News news=(News) session.get(News.class, id);

       session.close();

       return news;

    }



    public int getAllCount() throws Exception {

       // TODO Auto-generated method stub

       Session session=sessionFactory.openSession();

       String hql="SELECT count(*) FROM News";

       Query q=session.createQuery(hql);

       List all=q.list();

       int count=0;

       if(all!=null && all.size()>0){

           count=((Long)all.get(0)).intValue();

       }

       session.close();

       return count;

    }

}



实现类中定义sessionfactory属性,需要使用Spring进行注入

将实现类配置到Spring管理中

    <bean id="newsdaoimpl" class="mldn.lin.dao.impl.NewsDAOImpl">

       <property name="sessionFactory">

           <ref bean="sessionFactory" />

       </property>

    </bean>

编写测试类进行测试

package mldn.lin.test;



import mldn.lin.dao.NewsDAO;



import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

    public static void main(String[] args){

       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

       NewsDAO newsdao=(NewsDAO) ctx.getBean("newsdaoimpl");

     

       try {

           System.out.println(newsdao.getAllCount());

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

     

    }

}



虽然这样可以使Spring结合hibernate进行开发,但是还需要用户自行编写Session的打开关闭、事务处理。




第二种方法:使用Spring中提供好的HibernateDaoSupport支持类

如果要解决以上问题,可以使用Spring中提供好的HibernateDaoSupport支持类来完成



编写实现类:

package mldn.lin.dao.impl;



import java.util.List;



import mldn.lin.dao.NewsDAO;

import mldn.lin.pojo.News;



import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



public class NewsDAOImpl extends HibernateDaoSupport implements NewsDAO {



    public boolean doInsert(News news) throws Exception {

       // TODO Auto-generated method stub

       this.getSession().save(news);

       return true;

    }



    public boolean doRemove(int id) throws Exception {

       // TODO Auto-generated method stub

       this.getSession().delete(findById(id));

       return true;

    }



    public boolean doUpdate(News news) throws Exception {

       // TODO Auto-generated method stub

       this.getSession().update(news);

       return true;

    }



    public List<News> findAll() throws Exception {

       // TODO Auto-generated method stub

       return this.getSession().createCriteria(News.class).list();

    }



    public List<News> findAll(int cp, int ls, String keyword) throws Exception {

       // TODO Auto-generated method stub

       String hql="FROM News WHERE title=? OR abstractnote=? OR content=?";

       Query q=this.getSession().createQuery(hql);

       q.setString(0, "%"+keyword+"%");

       q.setString(1, "%"+keyword+"%");

       q.setString(2, "%"+keyword+"%");

       return q.list();

    }



    public News findById(int id) throws Exception {

       // TODO Auto-generated method stub

       return (News)this.getSession().get(News.class, id);

    }



    public int getAllCount() throws Exception {

       // TODO Auto-generated method stub

       String hql="SELECT count(*) FROM News";

       Query q=this.getSession().createQuery(hql);

       List all=q.list();

       int count=0;

       if(all!=null && all.size()>0){

           count=((Long)all.get(0)).intValue();

       }

       return count;

    }

}





继承的类中定义sessionfactory属性,需要使用Spring进行注入

将实现类配置到Spring管理中

    <bean id="newsdaoimpl" class="mldn.lin.dao.impl.NewsDAOImpl">

       <property name="sessionFactory">

           <ref bean="sessionFactory" />

       </property>

    </bean>

编写测试类进行测试

package mldn.lin.test;



import mldn.lin.dao.NewsDAO;



import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

    public static void main(String[] args){

       ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

       NewsDAO newsdao=(NewsDAO) ctx.getBean("newsdaoimpl");

     

       try {

           System.out.println(newsdao.getAllCount());

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

     

    }

}



继承HibernateDaoSupport,并通过Spring注入了SessionFactory,使用该方法对于c3p0的连接池,可以直接通过getSession 进行处理,但如果使用common-pool的连接池则还需要手工关闭连接

猜你喜欢

转载自dapeng.iteye.com/blog/1612321