這裏是我的對SSH框架搭建的一點心得,僅供新手,勿噴java
首先,搞清楚分層,mysql
視圖層 --》 控制層 --》 業務層 --》 DAO層--》 持久層web
搭建的順序是從後向前,搭建一點測試一點,省得都最後找錯誤太過於繁瑣spring
先搭建持久層,這裏是hibernate框架接管sql
一、創建javaBean對象(ElecText.jvav)
屬於持久層對象(PO對象)
屬性ID、名稱、日期、備註
二、建立映射文件ElecText.hbm.xml
創建PO對象與數據庫表Elec_Text的關聯關係
三、建立Hibernate.cfg.xml文件,配置鏈接數據庫的信息數據庫
4.寫測試文件,是否搭建成功apache
DAO層:緩存
這裏掌管的是和數據庫有關的CRUD方法,採用的是面向接口的方法,一個DAO類實現一個接口,經過接口session
使用方法app
業務層:
①先spring
②引jar包
③編寫beans.xml文件,放在src目錄下
④測試spring是否可以工做
⑤加入hibernate
⑥由於是SSH,因此hibernate被spring接管了hibernate.cfg.xml文件對象映射文件,sessionfactory在spring文件中配置便可
⑦在beans.xml文件中配置數據源
<!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/elec"/> <property name="username" value="root"/> <property name="password" value="123456"/> <!-- 鏈接池啓動時的初始值 --> <property name="initialSize" value="30"/> <!-- 鏈接池的最大值 --> <property name="maxActive" value="500"/> <!-- 最大空閒值.當通過一個高峯時間後,鏈接池能夠慢慢將已經用不到的鏈接慢慢釋放一部分,一直減小到maxIdle爲止 --> <property name="maxIdle" value="2"/> <!-- 最小空閒值.當空閒的鏈接數少於閥值時,鏈接池就會預申請去一些鏈接,以避免洪峯來時來不及申請 --> <property name="minIdle" value="1"/> </bean>
這裏也可以所有交給hibernate.cfg.xml來進行配置
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 1配置註解的自動掃描範圍 --> <context:component-scan base-package="cn.itcast.elec"></context:component-scan> <!-- 2配置數據源--> <!--3 建立sessionfactory工廠--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value> classpath:hibernate.cfg.xml </value> </property> </bean> <!--4 事物管理器--> <bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!--5 以註解的 形式管理事物--> <tx:annotation-driven transaction-manager="txManage" /> <!-- 配置本身的sessionfactory工廠,模擬模板的機能實現 --> <bean id="ElecText_impl" class="cn.itcast.elec.impl.ElecText_impl"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> </beans>
⑧配置sessionfactory工廠
<!--3 建立sessionfactory工廠--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 設置數據源 --> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <!-- 接管hibernate的對象映射文件 --> <list> <value>cn/itcast/elec/domain/ElecText.hbm.xml</value> </list> </property> <!-- hibernate的文件 --> <property name="hibernateProperties"> <value> <!-- SQL dialect --> hibernate.dialect = org.hibernate.dialect.MySQL5Dialect hibernate.connection.autocommit = true hibernate.show_sql = true hibernate.hbm2ddl.auto = update </value> </property> </bean>
⑨編寫domain對象和映射文件,而後進行簡單的測試
⑩考慮分層 使用事務管理器統一管理事務
<!--4 事物管理器--> <bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!--5 以註解的 形式管理事物--> <tx:annotation-driven transaction-manager="txManage" />
啓動事務註解
@Transactional(readOnly=true)
⑩②在hibernate裏面配置二級緩存
⑩③整合struts2
<1>引包
<2>建立struts.xml,放在/WEB-INF目錄下
14.在初始化的struts的同時初始化spring容器
<!-- 指定spring的配置文件,默認從web根目錄尋找配置文件,咱們能夠經過spring提供的classpath:前綴指定從類路徑下尋找 -->
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 對Spring容器進行實例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
經過獲取spring容器實例
HttpServletRequest request = ServletActionContext.getRequest(); //經過預加載後對spring的容器進行獲取 WebApplicationContext app = WebApplicationContextUtils. getWebApplicationContext(request.getSession().getServletContext());
經過上面的配置直接代替了下面的
ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
14.讓spring接管struts組件
<1>在struts.xml添加代碼
<!-- 配置代理請求處理 DelegatingRequestProcessor ,它的用戶是 -->
<controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller>
<2>在beans.1中配置action
<bean name="elecTextAction" class="cn.itcast.elec.action.Elect_Test_Action" />
15.解決中文亂碼問題
1.本身配置過虐器
2.使用spring框架提供的處理中文亂碼的過虐器
16.spring能夠用註解的方式配置屬性
<1>在須要注入的屬性加入@Resource 啓動byname方式進行注入屬性值
<2><!-- 啓動註解掃描 -->
<context:annotation-config/>