如果還沒有看過Struts2 架構配置教學、Struts2+Spring架構配置教學(Spring)、Struts2+Spring+Hibernate架構配置教學(Hibernate)上、Struts2+Spring+Hibernate架構配置教學(Hibernate+JNDI)下
這四篇的人~可以先去看一下
討厭鬼的專案是從上面幾篇一步一步建出來的
如果有問題想問~或是有什麼文章想要討厭鬼po的
討厭鬼會的話~就會po出來
討厭鬼預計接下的的文章還有Struts tag的用法、Junit(Spring+Hibernate)建置及用法、jqgrid建置及用法
因為討厭鬼本身還要上班~所以可能有點慢、先跟大家說聲抱歉
那討厭鬼就開始了喔!!!!!!!!!!!!!
這一篇主要是在講說Spring的注入方式、以及如何用Hibernate去資料庫撈資料
Srping 主要的注入方式有兩種
一種是在class中加入property
並且要在class加入一個全域變數並設置應變數的setter
如下
<bean id="leatherTypeDAO" class="nerdy.entity.dao.LeatherTypeDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
public class LeatherTypeDAO {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
如此一來Spring才能將sessionFactory的值注入
缺點是加果要注入的值一多~會變的不好管理以及忘記在class中設定setter導致變數是null
另一種就是我現在用的annotation
像我現在已經在Spring中設定檔中設定了這個sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
</value>
</property>
<property name="annotatedClasses">
<list>
<value>nerdy.entity.LeatherType</value>
</list>
</property>
</bean>
那我只要在Spring的設定檔中加入<context:annotation-config/>
並且在你想要用到sessionFactory的class中加入一個全域變數並在該變數上加入@Resource就可以了
public class LeatherTypeDAO {
@Resource
private SessionFactory sessionFactory;
}
這樣的好處是很方便~Spring的設定檔不會落落長也比較好看
class中程式碼也會少一點
以下是我的範例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
</value>
</property>
<property name="annotatedClasses">
<list>
<value>nerdy.entity.LeatherType</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
<property name="jndiName" value="java:comp/env/jdbc/leather"></property>
</bean>
</beans>
而Hibernate的用法就更簡單了
在這邊我不教各位客倌在Hibernate中下SQL與HQL
因為Hibernate本來就是一個不需要會SQL的人就能用
那為什麼要學HQL呢?如果要學HQL的話~討厭鬼寧可學SQL
所以在這邊要用的是Criteria
用上面Spring注入的方式取得sessionFactory後
就可以宣告Criteria如下
Criteria criteria = sessionFactory.openSession().createCriteria(LeatherType.class);
criteria.add(Restrictions.eq("typeId", 1));
其中LeatherType.class就是你要去查詢的資料表
而要加入條件的話就用add()若有多個條件就用多少個add
typeId為LeatherType.class的欄位名稱~而非資料庫中的欄位名稱
回傳時若為多筆則用list()
若唯一時用uniqueResult()、但用uniqueResult()但回傳值為一筆以上時會報錯
以下是撈出所有LeatherType的範例
public class LeatherTypeDAO {
@Resource
private SessionFactory sessionFactory;
public List<LeatherType> doQueryLeatherTypeList() {
Criteria criteria = sessionFactory.openSession().createCriteria(
LeatherType.class);
return criteria.list();
}
}
備註
一個資料表會對應到一個entity
一個DAO只能對一個資料表做事
例如我的資料庫有leather_type這個資料表
所以我會有一個LeatherType的entity
也會有一個LeatherTypeDAO的這class
專門對leather_type這個資料表做增刪修改
雖然這個LeatherTypeDAO是可以對其它的資料表做事
但是請不要這樣做~這樣會不好管理、也會不好維護

感謝版大分享心得...
謝謝~大家可以多多交流
請問版大... Spring的設定檔中加入
是加入dao-context.xml這個嗎還是3個都要加入...謝謝...新手不太了解...
只要在你web.xml有加入spring的設定檔的其中一個有加入就行了 以我的範例來說~就是action-context.xml、service-context.xml、dao-context.xml其中一個加入就可以了 原理就是對"人"來說是多個檔案~對"機器"來說就像是一個檔案
午安板大...感謝妳撥空回覆...從Spring+Hibernate用法教學開始學習就覺得有點抽像..如"一個資料表會對應到一個entity 一個DAO只能對一個資料表做事 例如我的資料庫有leather_type這個資料表 所以我會有一個LeatherType的entity 也會有一個LeatherTypeDAO的這class 專門對leather_type這個資料表做增刪修改 用法的部分能有個例子說明嗎,連結各種用法的觀念... 謝謝板大...
ok~我會再發一篇文章大致上講解一下觀念
請問板大有否推薦的書籍或網路資源可以加深SSH使用的觀念 這樣新手的我才不會常常問板大笨笨的問題...呵呵..
這個有點困難~我都是從實作中學習的...沒看過什麼書
請問我在dao-context.xml用annotation方式增加sessionFactory後,主控台報出以下錯誤,請問我是哪裡設定錯誤呢? SEVERE: Context initialization failed org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in class path resource [nerdy/config/dao-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
抱歉,上面的錯誤原來是我少放了一個jar檔,放入新jar檔以後跑tomcat主控台出現下列錯誤,請問我是哪裡設定錯誤呢? SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [nerdy/config/dao-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/annotations/Entity 以下為我的dao-context.xml如下:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
nerdy.entity.User
請問 學習 Struts 2+Spring 2+Hibernate 3 台灣有好的書籍可以學?
這個討厭鬼就不清楚了,因為討厭鬼都是在網路上學的,很抱歉沒辦法提供什麼意見給客倌您
我也出現了上面的錯誤,可以請問是少了甚麼jar 檔嘛? 謝謝