?? day3.txt
字號:
一、數(shù)據(jù)訪問
1、Spring的數(shù)據(jù)訪問設計思想(DAO、模板方法)PPT77
DataIntegrityViolationException: insert或update數(shù)據(jù)時違反了完整性,如違反了唯一性限制
DataAccessResourceFailureException:數(shù)據(jù)訪問資源失敗,如不能連接數(shù)據(jù)庫
InvalidDataAccessResourceUsageException:使用錯誤的SQL語法訪問數(shù)據(jù)庫
2、數(shù)據(jù)源配置:
方式一:Spring內置實現(xiàn) DriverManagerDataSource
<bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hibdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>windows</value>
</property>
</bean>
方式二:DBCP提供的BasicDataSource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hibdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>windows</value>
</property>
</bean>
方式三:JNDI數(shù)據(jù)源 (在講解SSH整合時再說明)
JNDI數(shù)據(jù)源:(mysql5,tomcat5.5)
step1:
在server.xml中:
<Resource name="jdbc/mydatasource" auth="Container" description="DB Connection"
type="javax.sql.DataSource" username="root" password="windows"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/tarena" maxActive="5" />
step2:
在context.xml中(conf\context.xml):
<ResourceLink name="jdbc/mydatasource" global="jdbc/mydatasource" type="javax.sql.DataSourcer"/>
step3:
在beans-config.xml:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mydatasource</value>
</property>
</bean>
3、JDBC支持:
step1: 配置數(shù)據(jù)源
step2: 配置JdbcTemplate
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
step3:配置DAO
<bean id="orderDao" class="lab5.OrderDAOImpl">
<property name="jt"><ref bean="jdbcTemplate"/></property>
</bean>
注意: 查詢時,使用RowMapper
4、hibernate支持:
step1: 配置數(shù)據(jù)源
step2: 配置sessionfactory
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>lab6/Order.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
注意:配置映射文件位置的另一種用法
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/bo</value>
</list>
</property>
或者
直接使用hibernate.cfg.xml,此時,配置內容如下:
<property name="configLocation">
<value>exec\t2\hibernate.cfg.xml</value>
</property>
注意,此時,因為由hibernate來管理事務,所以,必須在spring配置文件中,配置事務后才能使用,否則,結果不會真正寫入數(shù)據(jù)庫!
可以參照工程spring下的t2
step3: 配置DAO
<bean id="orderDao" class="lab6.OrderDAOHibernateImpl">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
注意:以上配置是要求dao 繼承HibernateDaoSupport
二、事務:
1、Spring事務機制
聲明式事務、事務管理器
2、hibernate事務p72
step1: 配置數(shù)據(jù)源
step2:配置sessionfactory (同上)
step3:配置事務管理器
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
step4:創(chuàng)建事務服務代理
<bean id="saleService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<value>lab7.SaleService</value>
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="target">
<ref bean="saleServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
注:
事務屬性描述格式:
傳播行為,隔離級別,只讀事務(readonly),回滾規(guī)則
在默認情況下,Spring的容器對于非受查異常(服務模塊中拋出的非受查異常)
,會回滾事務。對于受查異常,會提交事務。
如果即使發(fā)生了某種受查異常,也要回滾事務,可以用 “- 異常類型“來聲明。回滾事物
同樣,對于非受查異常,如果不要求回滾事務,可以用"+異常類型"來聲明。 提交事物
3、簡化事務配置
繼承、自動代理
4、使用標注來進行事務管理(可選)
(1)引入相應命名空間,參見spring參考文檔
(2)加入<tx:annotation-driven transaction-manager="myTransactionManager"/>
(3)在service類中,使用@Transactional標記
可參考工程spring中的t4
三、Spring與struts整合:
前提:
必須在Web應用啟動時,創(chuàng)建Spring的ApplicationContext實例
方式:
1、采用ContextLoaderListener來創(chuàng)建ApplicationContext:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-config/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、采用ContextLoaderPlugIn來創(chuàng)建ApplicationContext
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
</plug-in>
或者:
通過listener裝載spring應用上下文
方式一:通過Spring的ActionSupport類
ActionSupport類:
知道ApplicationContext的獲得方式。
步驟:
1、Action直接繼承ActionSupport
2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
3、取得相應Bean
注意:有可能需要替換commons-attributes-compiler.jar包。
優(yōu)點:
簡單
缺點:
耦合高
違反IOC
無法使用多方法的Action
方式二:通過Spring的DelegatingActionProxy類
步驟:
1、Action中,使用IOC獲得服務
2、配置struts-config.xml
<action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
3、在Spring配置文件中
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
注意,要用bean name命名。
/somepath:Action的path
優(yōu)點:
不使用Spring api編寫 Action
利用了IOC裝配。
可以利用容器的scope="prototype"來保證每一個請求有一個單獨的Action來處理,
避免struts中Action的線程安全問題。
缺點:
struts配置文件中,所有path都映射到同一個代理類
方式三:通過Spring的DelegatingRequestProcessor類
步驟:
1、Action中,使用IOC獲得服務
2、配置struts-config.xml
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
3、在Spring配置文件中
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
小結:
Spring與Struts整合方式只有兩種:
(1)由Spring容器來管理Action(方式二,方式三)
(2)Action處于容器之外(方式一)
注意:
中文問題:
設置過濾器,設置頁面編碼,數(shù)據(jù)庫編碼
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -