如果還沒有看過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中程式碼也會少一點

以下是我的範例

daocontext

<?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的範例

daoclass

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是可以對其它的資料表做事

但是請不要這樣做~這樣會不好管理、也會不好維護

arrow
arrow
    文章標籤
    spring hibernate
    全站熱搜

    討厭鬼 發表在 痞客邦 留言(8) 人氣()