<!--
Spring,Struts2,Hibernate三大框架的集成(SSH)
-->
前面已經講了三大框架的各自優點,這三大框架作爲J2EE開發的必備的東西,所以它們的集成也是很重要的!
這裏集成呢~光用文字說明是說不清楚的~所以要結合圖示,呵呵~開整~
首先需要導入的包是Struts2的核心包,除了核心包還需要另外一個類庫,見圖:
struts2導入後,接下來就是hibernate的包了~一般情況下只需要導入一個包即可(見圖)!
在struts2之後呢,就是最後一個框架了,Spring框架,這裏需要導入5個包:前面三個一級J2EE,WEB 3.0,如圖:
按照上面的步驟呢,三大框架就已經構建在我們的Web項目中了,但是,這並不夠,我們需要在web.xml中添加對spring的支持
通過添加監聽器的方法進行:
<!-- 配置對spring的支持 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
這樣我們的三大框架纔算真正的搭建在我們的web項目中了,接下來就是對dao和aciton的配置了,
我們通常建議在src目錄下建一個config文件夾來存放applicationContext_dao.xml和applicationContext_action.xml
對了~三個框架搭建好了後,在struts.xml中的配置會有一點點變化~怎樣變化?接下來慢慢道來~
我們在學了spring後呢,瞭解到面向接口編程的重要性,所以這裏依然用的是面向接口的思想,通常會在dao包下寫接口,然後在dao.impl寫
接口的實現類,接下來就以UserInfo爲對象進行代碼演示!
代碼示例(接口):
import com.shu.dao;
public interface IUserDao{
public List<UserInfo> queryUser(String hql);
public boolean addUser(UserInfo user);
}
代碼演示(實現類):
import com.shu.dao.impl;
import com.shu.dao.IUserDao;
//注意在dao類中需要繼承spring中的hibernateDaoSupport和實現接口
public class UserInfoDao extends HibernateDaoSupport implements IUserDao{
@override
public List<UserInfo> queryUser(String hql){
//這裏可以直接調用父類的方法進行各種操作,至於方法,總結在後面
return super(this).getHibernateTemplate().find(hql);
}
@override
public boolean addUser(UserInfo user){
try{
super.getHibernateTemplate().save(user);
return ture;
}catch(Exception e){
//由於使用了spring框架,如果事務配值好了,就不用擔心數據的問題了~事務的配置後面總結!
return false;
}
}
}
改寫的類已經寫好了,接下來就是在配值文件中將其配置進去!
首先在applicationContext_dao.xml中配置dao類信息
<!--標準的javaBean格式-->
<bean id="udao" class="com.shu.dao.impl.UserInfoDao">
<!--不要忘記把sessionFactory配進去了,引用自applicationContext.xml中的SF-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
在配置applicationContext_action.xml前,我們需要先把aciton類寫好
代碼示例(action):
import com.shu.action;
public class UserAction{
//定義各種屬性,但是不能忘記定義IUserDao這個接口!
private IUserDao userDao;//userDao這個名字我們會在配置文件中用到!
//....各種方法...
}
開始配置applicationContext_action.xml
代碼演示(applicationContext_action.xml):
<!--記住這裏的action的id,因爲我們會在struts.xml中使用-->
<bean id="uaction" class="com.shu.action.UserAction">
<!--屬性是在UserAction中已經定義好的屬性-->
<property name="userDao" ref="udao"/><!--udao來自applicationContext_dao.xml,userDao來自com.shu.action.UserAction-->
</bean>
struts.xml的配置:
代碼示例(struts.xml):
<package name="myPack" extends="struts_default" namespace="/">
<!--注意這裏的action的配置就不一樣了,uaction來自applicationContext_action.xml-->
<action name="*User" class="uaction" method="{1}">
<!--.....-->
</action>
</package>
以上配置好了後,接下來就是配置事務了~然後在applicationContext.xml中引入config文件夾下的xml文件!
代碼示例(applicationContext.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--這個是系統自帶的,不需要動他(在applicationContext_dao.xml和applicationContext_dao.xml中可以沒有這個)-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事務 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 開始配置事務 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
<tx:method name="update*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true" propagation="NEVER"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution (* com.shu.dao.impl.*.* (..))" id="pcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pcut"/>
</aop:config>
<!-- 引入dao -->
<import resource="config/applicationContext_dao.xml"/>
<!-- 引入action -->
<import resource="config/applicationContext_action.xml"/>
</beans>
就這樣~SSH三大框架就配置好了,這裏還需要補充上一個附加的知識點,就是關於延遲加載的問題!
Spring框架呢對這個進行了一系列的優化,我們只需要在web.xml中添加配置代碼,就能達到目的,
而不是在hibernate中去設置lazy="false",但是有時候又不得不採用lazy="false"!
<!-- 解決延遲加載的問題 -->
<filter>
<filter-name>spring_lazy</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>spring_lazy</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>spring_lazy</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
總結前的話:
1,在寫dao類時可以寫一個BaseDao繼承HibernateDaoSupport,然後接口的具體實現類繼承BaseDao,這樣做,你懂的~
2,如果在getHibernateTemplate()中沒有滿足你查找的方法,那麼試試Hibernate的方法吧:
Session se=this.getHibernateTemplate().getSessionFactory().openSession();
se點上各種hibernate的查詢方法,總有你要的!
3.如果你需要各種SQL語句的操作那麼推薦你在導入Spring的時候導入圖中的第個四包(JDBC Libraries)
然後實現類繼承自jdbcDaoSupport,然後getJdbcTemplate()中的方法進行操作~
<!--
Author:lovingshu's Forever
Date:2011-12-1 19:50
Remark:It should be finished few days ago,but I put it off~because I'm lazy~ ^_^
Today,We begin to learning the greatest database Oracle,Oh~man,It is not so easy as
SQL Server,and I still have passages do not finish~the ANINONATION,the DWR~I'll try my
best to make it!
-->
---------------以下內容轉自互聯網------------------
Spring中常用的hql查詢方法(getHibernateTemplate())
一、find(String queryString);
示例:this.getHibernateTemplate().find("from bean.User");
返回所有User對象
二、find(String queryString , Object value);
示例:this.getHibernateTemplate().find("from bean.User u where u.name=?", "test");
或模糊查詢:this.getHibernateTemplate().find("from bean.User u where u.name like ?", "%test%");
返回name屬性值爲test的對象(模糊查詢,返回name屬性值包含test的對象)
三、find(String queryString, Object[] values);
示例:String hql= "from bean.User u where u.name=? and u.password=?"
this.getHibernateTemplate().find(hql, new String[]{"test", "123"});
返回用戶名爲test並且密碼爲123的所有User對象
四、findByExample(Object exampleEntity)
示例:
User u=new User();
u.setPassword("123");//必須 符合的條件但是這兩個條件時並列的(象當於sql中的and)
u.setName("bb");
list=this.getHibernateTemplate().findByExample(u,start,max);
返回:用戶名爲bb密碼爲123的對象
五、findByExample(Object exampleEntity, int firstResult, int maxResults)
示例:
User u=new User();
u.setPassword("123");//必須 符合的條件但是這兩個條件時並列的(象當於sql中的and)
u.setName("bb");
list=this.getHibernateTemplate().findByExample(u,start,max);
返回:滿足用戶名爲bb密碼爲123,自start起共max個User對象。(對象從0開始計數)
六、findByNamedParam(String queryString , String paramName , Object value)
使用以下語句查詢:
String queryString = "select count(*) from bean.User u where u.name=:myName";
String paramName= "myName";
String value= "xiyue";
this.getHibernateTemplate().findByNamedParam(queryString, paramName, value);
System.out.println(list.get(0));
返回name爲xiyue的User對象的條數
七、findByNamedParam(String queryString , String[] paramName , Object[] value)
示例:
String queryString = "select count(*) from bean.User u where u.name=:myName and u.password=:myPassword";
String[] paramName= new String[]{"myName", "myPassword"};
String[] value= new String[]{"xiyue", "123"};
this.getHibernateTemplate().findByNamedParam(queryString, paramName, value);
返回用戶名爲xiyue密碼爲123的User對象
八、findByNamedQuery(String queryName)
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryAllUser"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User
]]>
</query>
</hibernate-mapping>
2、如下使用查詢:
this.getHibernateTemplate().findByNamedQuery("queryAllUser");
九、findByNamedQuery(String queryName, Object value)
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryByName"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User u where u.name = ?
]]>
</query>
</hibernate-mapping>
2、如下使用查詢:
this.getHibernateTemplate().findByNamedQuery("queryByName", "test");
十、findByNamedQuery(String queryName, Object[] value)
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryByNameAndPassword"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User u where u.name =? and u.password =?
]]>
</query>
</hibernate-mapping>
2、如下使用查詢:
String[] values= new String[]{"test", "123"};
this.getHibernateTemplate().findByNamedQuery("queryByNameAndPassword" , values);
十一、findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryByName"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User u where u.name =:myName
]]>
</query>
</hibernate-mapping>
2、如下使用查詢:
this.getHibernateTemplate().findByNamedQuery("queryByName" , "myName", "test");
十二、findByNamedQueryAndNamedParam(String queryName, String[] paramName, Object[] value)
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryByNameAndPassword"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User u where u.name =:myName and u.password=:myPassword
]]>
</query>
</hibernate-mapping>
2、如下使用查詢:
String[] names= new String[]{"myName", "myPassword"};
String[] values= new String[]{"test", "123"};
this.getHibernateTemplate().findByNamedQuery("queryByNameAndPassword" , names, values);
十三、findByValueBean(String queryString , Object value);
示例:
1、定義一個ValueBean,屬性名必須和HSQL語句中的:後面的變量名同名,此處必須至少有兩個屬性,分別爲myName和myPassword,使用setter方法設置屬性值後
ValueBean valueBean= new ValueBean();
valueBean.setMyName("test");
valueBean.setMyPasswrod("123");
2、
String queryString= "from bean.User u where u.name=:myName and u.password=:myPassword";
this.getHibernateTemplate().findByValueBean(queryString , valueBean);
十四、findByNamedQueryAndValueBean(String queryName , Object value);
示例:
1、首先需要在User.hbm.xml中定義命名查詢
<hibernate-mapping>
<class>......</class>
<query name="queryByNameAndPassword"><!--此查詢被調用的名字-->
<![CDATA[
from bean.User u where u.name =:myName and u.password=:myPassword
]]>
</query>
</hibernate-mapping>
2、定義一個ValueBean,屬性名必須和User.hbm.xml命名查詢語句中的:後面的變量名同名,此處必須至少有兩個屬性,分別爲myName和myPassword,使用setter方法設置屬性值後
ValueBean valueBean= new ValueBean();
valueBean.setMyName("test");
valueBean.setMyPasswrod("123");
3、 String queryString= "from bean.User u where u.name=:myName and u.password=:myPassword";
this.getHibernateTemplate().findByNamedQueryAndValueBean("queryByNameAndPassword", valueBean);