SSH架構由 Spring+Struts2+Hibernate整合而成
Spring service層
Struts2 web層
Hibernate dao層
Struts2和Spring整合: 把struts2的action對象創建交給spring進行管理<bean id=" " class=" " scope=“prototype”/>
Hibernate和spring整合: 把hibernate核心配置文件裏面的數據庫配置,直接寫在spring配置文件中(hibernate有個問題,第一次訪問很慢,因爲第一次訪問的時候創建sessionFactory對象,現在交由spring來創建,服務器啓動時就創建了)
一、spring整合struts2
spring基本jar包
struts2 jar包
struts2整合springjar包
spring整合web項目jar包
環境搭建好後,創建action類,創建struts.xml並配置action
public class UserAction extends ActionSupport {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
public String execute() throws Exception {
System.out.println("action........");
// userService.add();
userService.getUser();
return NONE;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo1" extends="struts-default" namespace="/">
<!-- class不寫action全路徑了,因爲spring配置文件中已有bean的創建,不然會創建兩次
這裏class中寫spring配置文件中配置的action的bean的id
完成這一步需要struts-spring-plugin jar包
-->
<!--配置action 本來是根據路徑找action名字,然後找路徑反射創建對象,現在是找到名字後再去spring配置文件中找 -->
<action name="userAction" class="userAction"></action>
</package>
</struts>
web.xml配置監聽器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh-demo</display-name>
<!-- 指定配置文件的位置 src下叫類路徑(classpath)在\build\classes下-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applictionContext.xml</param-value>
</context-param>
<!-- 過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置過濾器,服務器啓動時加載spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、spring整合hibernate
spring整合orm框架jar包
需要的hibernatejar包
搭建好hibernate環境後
創建實體類
public class User {
private Integer id;
private String name;
private String password;
//略去 setter getter方法
}
創建映射配置文件user.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 映射配置文件 -->
<hibernate-mapping>
<!-- 配置類和表的對應
name屬性 實體類全路徑
table數據庫表名
-->
<class name="com.idea.entity.User" table="user">
<id name="id" column="id">
<!-- id生成策略 -->
<generator class="native"></generator>
</id>
<!-- 配置其他屬性 column可省略 但是name必須和字段一致 -->
<property name="name" column="name"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>
創建hibernate核心配置文件 hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 核心配置文件 -->
<hibernate-configuration>
<session-factory>
<!-- 第一部分,配置數據庫 單獨hibernate必須的,現在交由spring完成 -->
<!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:5176/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">ajpx_201403</property> -->
<!-- 第二部分,配置hibernate信息 可選的 -->
<!-- 輸出底層sql語句 -->
<property name="hibernate.show_sql">true</property>
<!-- 輸出底層sql格式 -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate幫創建表 需要配置
update:如果已有表 更新 如果沒有 創建
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置數據庫方言
比如:在mysql裏面實現分頁用limit
oracle數據庫分頁用 rownum
讓hibernate識別不同數據庫自己特有的語言
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 在hibernate核心配置文件中配置 -->
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<!-- 第三部分,把映射文件放到核心配置文件中 必須的-->
<mapping resource="com/idea/entity/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、搭建spring環境
配置監聽器(spring配置文件自動加載)
指定spring配置文件的路徑
spring配置文件
文件中約束比較全
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置c3p0連接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入屬性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:5176/test"></property>
<property name="user" value="root"></property>
<property name="password" value="ajpx_201403"></property>
</bean>
<!-- 註解方式配置事務 -->
<!-- 1配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 這裏注入sessionFactory 因爲其中已經包含DataSource屬性 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 開啓事務註解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 將sessionFactory交給spring管理 這裏幫我們創建了對象
省略了//加載核心配置文件
cfg = new Configuration();
cfg.configure();這些工具代碼類
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 因爲hibernate核心配置文件中沒有數據庫配置(spring配置文件中),這裏將數據庫屬性注入進來 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定hibernate核心配置文件 value是核心配置文件的全路徑和名稱 可以加包路徑-->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 將struts2的action對象交給spring管理 即配置action對象 多實例對象-->
<bean id="userAction" class="com.idea.action.UserAction" scope="prototype">
<!-- 注入service -->
<property name="userService" ref="userService"></property>
</bean>
<!-- 創建service對象 -->
<bean id="userService" class="com.idea.service.UserService">
<!-- 注入dao name爲接口 ref爲實現類-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<!-- 創建dao層的實現類對象 new的是實現類 -->
<bean id="userDaoImpl" class="com.idea.dao.UserDaoImpl">
<!-- 注入hibernateTemplate對象 -->
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 創建hibernateTemplate模板對象 他需要sessionFactory-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
錯誤提示:Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition. 如果方法沒有被Spring事務管理器所管理到,默認的話事務是隻讀模式 需要配置事務