若還沒看過這三篇的話~請先去看Struts2 架構配置教學、Struts2+Spring架構配置教學(Spring)、Struts2+Spring+Hibernate架構配置教學(Hibernate)上
雖然討厭鬼本身沒有很厲害
但還是想在開始前跟大家小講一下小觀念
我在這邊用的是 Action <> Service <> DAO
所以action layer是不會直接用到DAO layer
DAO是用來對資料庫做事的
所以Service layer 是不會直接存取資料庫
而在Spring 的設定檔中 我會用三個設定檔
分別用來管理 Action 、Service、DAO
這樣看起來是比較清楚~也比較好debug
也因為今天要講的Hibernate會用到資料庫的東西
所以我會將設定放在dao-context.xml
可是今天會只做到跟資料庫連線~~~~
那討厭鬼就開始了!!!!!
首先先建立一個SessionFactory
在這邊呢~我們會參照到一個dataSource所以等等會設定一個dataSource
hibernateProperties是指Hibernate裡的一些設定,還可以指定是否要show出sql之類的等等
而annotatedClasses這個是指你要跟資料庫連線的entity
如果在這邊沒有加的話~之後用Hibernate存取資料庫時會失敗
<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>
當然連線方式是看各位客倌的習慣是要使用JDBC還是JNDI
討厭鬼這邊是使用JNDI
在這邊使用value中的"java:comp/env/"是前導字必填~後面是name
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
<property name="jndiName" value="java:comp/env/jdbc/leather"></property>
</bean>
下面是我的配置
<?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>
而在這邊要宣告連線方式有兩種
一個是在tomcat的Server.xml的<context>裡宣告
一個是在META-INF裡加入一個context.xml中宣告
我用的是後者~比較方便
所以就在META-INF裡加入一個context.xml並加上
<context>
<Resource name="jdbc/leather" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.113:3306/leather"
username="leather" password="123456" maxWait="10000" maxIdle="30" maxActive="100" />
</context>
下面是我的配置
<?xml version="1.0" encoding="UTF-8"?>
<context>
<Resource name="jdbc/leather" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.113:3306/leather"
username="leather" password="123456" maxWait="10000" maxIdle="30" maxActive="100" />
</context>
基本上Struts2+Spring+Hibernate的架構設定已經完成了
下次會發一篇Spring + Hibernate的用法~不是架構了喔