Spring结合Hibernate
一、建立项目
(1)建立数据库
DROP DATABASE mldn_hibernate;
CREATE DATABASE mldn_hibernate;
USE mldn_hibernate;
DROP TABLE news ;
CREATE TABLE news ( id int primary key auto_increment , title VARCHAR(50) not null, post_date datetime not null, abstractnote VARCHAR(200) not null, content text not null, imgurl VARCHAR(50) ); |
(2)项目:SpringHibernateDemo01
二、加入Spring和Hibernate的支持
注意:因为现在是需要借助Spring来管理或者组合其他框架,所以需要先加入Spring的支持。
1、加入Spring的支持
(1)项目—右键—MyEclipse—Add Spring Capabilities…
(2)加入Spring核心jar包以及和Hibernate结合需要的jar包
并且注意加入jar包的方式,还是选择放到lib目录下
(3)添加Spring的管理配置文件
一般采用默认名称applicationContext.xml
(4)Finish完成
2、加入Hibernate的支持
(1)项目—右键—MyEclipse—Add Hibernate Capabilities…
(2)加入Hibernate的支持jar包
注意:比之前没有使用Spring的时候多了一个jar包
(3)选择Hibernate的配置文件
因为此处使用了Spring,所以,选择在已有的Spring的配置文件中进行Hibernate的配置
(4)选择Spring的核心配置文件及填写SessionFactory的ID
选择是重新创建一个Spring的核心配置文件还是使用已经存在的,这里使用已经存在的即可
并且填写SessionFactory的ID,即定义SessionFactory的bean标签的id值
(5)配置数据库连接及数据源的bean的id
(6)是否由Hibernate自动创建SessionFactory类
取消选择
(7)对于重复添加的jar的处理方式
选择保留现有的或者替换都可以
3、查看加入Hibernate支持后Spring的applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/mldn_hibernate"> </property> <property name="username" value="root"></property> <property name="password" value="mysqladmin"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> </bean>
</beans> |
4、选择数据源连接方式
(1)现在默认的是使用公用的数据源连接池的连接方式
使用该中方式,需要再加入commons-pool.jar ,否则会报找不到类的错误。
在Spring开发包的spring-framework-2.0-m3\lib\jakarta-commons中可以找到该jar包。
(2)使用c3p0方式进行数据源处理
需要修改Spring配置文件如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource"> <property name="driverClass" value="org.gjt.mm.mysql.Driver"> </property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"> </property> <property name="user" value="root"></property> <property name="password" value="mysqladmin"></property> </bean> |
需要注意:当实现类继承HibernateDaoSupport,并通过Spring注入了SessionFactory,使用该方法对于c3p0的连接池,可以直接通过getSession 进行处理,但如果使用common-pool的连接池则还需要手工关闭连接
5、加入Hibernate的show_sql属性配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> |
6、生成数据映射,建立pojo类
生成映射没有变化
7、编写dao
package mldn.lin.dao;
import java.util.List;
import mldn.lin.pojo.News;
public interface NewsDAO {
public boolean doInsert(News news) throws Exception;
public boolean doUpdate(News news) throws Exception;
public boolean doRemove(int id) throws Exception;
public List<News> findAll() throws Exception;
public List<News> findAll(int cp,int ls,String keyword) throws Exception;
public int getAllCount() throws Exception;
public News findById(int id) throws Exception;
} |
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的连接池则还需要手工关闭连接。
第三种方法:使用HibernateTemplate类
实现类:
package mldn.lin.dao.impl;
import java.sql.SQLException; import java.util.List;
import mldn.lin.dao.NewsDAO; import mldn.lin.pojo.News;
import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; 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.getHibernateTemplate().save(news); return true; }
public boolean doRemove(int id) throws Exception { // TODO Auto-generated method stub this.getHibernateTemplate().delete(findById(id)); return true; }
public boolean doUpdate(News news) throws Exception { // TODO Auto-generated method stub this.getHibernateTemplate().update(news); return true; }
public List<News> findAll() throws Exception { // TODO Auto-generated method stub return this.getHibernateTemplate().find("FROM News"); }
public List<News> findAll(final int cp, final int ls, final String keyword) throws Exception { // TODO Auto-generated method stub List all = this.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub // 直接编写查询方法 Query q = session.createQuery("FROM News WHERE title=? OR abstractnote=? OR content=?"); q.setFirstResult((cp - 1)* ls); q.setMaxResults(ls); q.setString(0, "%"+keyword+"%"); q.setString(1, "%"+keyword+"%"); q.setString(2, "%"+keyword+"%"); return q.list(); } }); return all;
}
public News findById(int id) throws Exception { // TODO Auto-generated method stub return (News) this.getHibernateTemplate().get(News.class, id); }
public int getAllCount() throws Exception { // TODO Auto-generated method stub List all=this.getHibernateTemplate().find("SELECT count(*) FROM News"); int count=0; if(all!=null && all.size()>0){ count=((Long)all.get(0)).intValue(); } return count; } }
|
配置Spring管理文件
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean>
<bean id="newsdaoimpl" class="mldn.lin.dao.impl.NewsDAOImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"></ref> </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(); // } try { for (int i = 0; i < 100; i++) { System.out.println(newsdao.findAll()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
|
三、HibernateTemplate类的使用说明
HibernateTemplate是对Session操作的封装:
可以完成save()、update()、delete()所能完成的功能。
使用Query对象时,可以通过HQL的方式进行修改和删除操作
而使用HibernateTemplate可以通过bulkUpdate()来完成修改和删除功能,该方法有3个重载:
只传入一个字符串参数:直接执行该HQL语句,返回影响的记录数
传入一个HQL和一个Object参数:当HQL中只有一个?参数时,使用该方法可以为参数赋值,并执行修改或删除。
传入一个HQL和一个Object[]参数:当HQL中包含多个?参数时,使用该方法,按顺序为?赋值
public boolean doRemove(int id) throws Exception { // TODO Auto-generated method stub String hql = "DELETE FROM News AS n WHERE n.id = ?" ; if (this.getHibernateTemplate().bulkUpdate(hql,id) > 0) { return true ; } return false; } |
完成查询功能时:
查询全部(不包含参数的查询):可以使用 find方法进行查询,将HQL语句传入,返回List类型
public List<News> findAll() throws Exception { // TODO Auto-generated method stub return this.getHibernateTemplate().find("FROM News"); } |
如果包含?参数,则与bulkUpdate相同,可以通过传入Object参数或Object数组参数来进行处理。
public List<News> findAll() throws Exception { // TODO Auto-generated method stub return this.getHibernateTemplate().find("FROM News WHERE id = ?",3); }
|
如果HQL中使用的是:参数时,可以通过findByNamedParam进行查询。
后面的两个数组参数分别表示参数名和参数值,注意需要一一对应。
public List<News> findAll() throws Exception { // TODO Auto-generated method stub return this.getHibernateTemplate().findByNamedParam( "FROM News WHERE id = :id", "id", 3); } |
当对主键进行查询时,可以使用get方法进行处理,使用的方法与session.get完全相同。
完成分页查询
可以使用findByCriteria或findByExample,但这两个方法不是很灵活,如果要使用HQL方式进行查询
可以通过使用execute或executeFind方法 ,自行编写被封装的session操作,但需要使用匿名内部类:
public List<News> findAll(final int cp,final int ls) throws Exception { // TODO Auto-generated method stub List all = this.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub // 直接编写查询方法 Query q = session.createQuery("FROM News"); q.setFirstResult((cp - 1)* ls); q.setMaxResults(ls); return q.list(); } }); return all; } |
需要声明一个HibernateCallback对象,并在内部类中实现doInHibernate方法,在该方法中完成对session的操作,如果需要调用外部类的属性,注意要将属性设置成为final。
除了分页以外,还有一种情况需要使用内部类来完成查询:
当出现延迟加载问题时,需要在内部类中调用级联查询(因为只有在内部类中session并不会关闭)。
当使用HibernateTemplate查询返回数字或数组(带有select关键字的HQL),返回的数字类型为Long或Double。
public int getAllCount() throws Exception { // TODO Auto-generated method stub List all = this.getHibernateTemplate() .find("SELECT count(*) FROM News"); if (all != null && all.size() > 0) { return ((Long) all.get(0)).intValue(); } return 0; } |
四、SSH联合开发
建立项目,加入支持
加入支持的顺序:
Spring à Hibernate 或 Struts
注意,多加入了Spring对web的支持jar包
完成后台代码
编写接口和实现类,并将HibernateTemplate和实现类配置到applicationContext.xml中
<!-- 定义HibernateTemplate的Bean --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean>
<bean id="newsdaoimpl" class="org.liky.dao.impl.NewsDAOImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"></ref> </property> </bean> |
编写页面代码,建立连接,完成列表显示
<center> <a href="news.do?status=list">列表</a> </center> |
建立Struts的Action与ActionForm
package org.liky.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.liky.dao.NewsDAO; import org.liky.struts.form.NewsForm;
public class NewsAction extends DispatchAction {
private NewsDAO newsdao;
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { NewsForm newsForm = (NewsForm) form;// TODO Auto-generated method stub
List all = null; int allRecorders = 0; try { all = this.newsdao.findAll(newsForm.getCp(),newsForm.getLs()); allRecorders = this.newsdao.getAllCount(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } request.setAttribute("all", all); request.setAttribute("allRecorders", allRecorders); request.setAttribute("cp", newsForm.getCp()); request.setAttribute("ls", newsForm.getLs());
return mapping.findForward("list"); }
public void setNewsdao(NewsDAO newsdao) { this.newsdao = newsdao; } } |
在Spring中加入Action的Bean配置
<bean id="newsaction" class="org.liky.struts.action.NewsAction"> <property name="newsdao"> <ref bean="newsdaoimpl"></ref> </property> </bean> |
显示所有数据
<center> <logic:iterate id="news" name="all" scope="request"> ${news.title } -- ${news.postDate } <br> </logic:iterate> </center> |
将commons-pool.jar拷贝到项目中
运行后发现出现空指针异常,newsdao没有被初始化,原因是由于Struts的Action本身是由struts-config.xml中的type中设置的类型进行创建,创建方式为Class.forName(包.类名).newInstance()。而没有用到Spring。
需要在struts-config.xml中,加入以下配置:
加入一个spring的插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/> </plug-in> |
该配置是为了在读取struts-config.xml时,可以找到spring的配置文件,用来创建Spring对象
还需要设置一个controller类,在读取action配置时,使ActionServlet改为通过Spring创建Action对象,而不是直接创建。
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"> </controller> |
该配置可以在读取Action 时,先在spring的配置文件中查找是否有与该action对应的Bean,如果有,则使用spring进行创建,如果没有,需要通过new的方式直接创建。
修改spring中对于action的bean配置,spring与struts在查找对应关系时,通过path路径进行查找
<bean name="/news" class="org.liky.struts.action.NewsAction"> <property name="newsdao"> <ref bean="newsdaoimpl"></ref> </property> </bean> |
以上配置完成后,启动服务器测试
发现出现404错误,而且控制台没有错误信息提示。
将log4j.properties加入到项目中
发现启动时出现了找不到方法的异常
原因在于asm-2.2.3.jar与asm.jar有冲突,其中观察方法的参数,发现asm.jar为需要用的jar包,应该删除asm-2.2.3.jar包
补充:使用hibernate缓存
修改applicationContext.xml中sessionFactory的hibernate属性配置,加入使用二级缓存
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache"> true </prop> </props> </property> |
修改hbm.xml,加入使用缓存的方式
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="org.liky.pojo.News" table="news" catalog="testdb"> <cache usage="read-only"/> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="title" length="20" not-null="true" /> </property> <property name="keyword" type="java.lang.String"> <column name="keyword" length="10" not-null="true" /> </property> <property name="content" type="java.lang.String"> <column name="content" length="65535" not-null="true" /> </property> <property name="postDate" type="java.util.Date"> <column name="post_date" length="19" not-null="true" /> </property> </class> </hibernate-mapping>
|
加入ehcache.xml
<ehcache>
<!-- 表示缓存文件所保存的临时路径,java.io.tmpdir表示JDK所临时保存文件的路径 --> <diskStore path="java.io.tmpdir"/>
<!-- 以下为默认缓存配置,如果没有进行单独的配置,使用该配置进行缓存的保存:
maxInMemory - 设置内存中可以创建pojo数据的最大数量(针对一个pojo) eternal - 设置缓存中的数据是否永久存在.如果为true, 当超过时间后,数据也不销毁. timeToIdleSeconds - 设置从缓存对象创建后经过多长时间自动销毁. 只有当eternal为false时才可以使用. 单位为秒 timeToLiveSeconds - 设置缓存对象经过一次查询后,多长时间不再被查询后自动销毁(闲置时间). overflowToDisk - 如果为true,则当数据数量超出范围后(InMemory中的范围)后,是否保存到硬盘上(保存的位置在上面的diskStore中定义).
--> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="120" overflowToDisk="true" />
</ehcache>
|
在调用查询前需要设置是否使用缓存操作
在spring中,这个设置可以在配置文件中完成
<!-- 定义HibernateTemplate的Bean --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="cacheQueries"> <value>true</value> </property> <property name="sessionFactory"> <ref bean="sessionFactory"></ref> </property> </bean> |
修改HibenateTemplate的配置,加入使用缓存的设置属性,值为true
删除项目中的ehcache-1.1.jar包