1 jar整合(總共39個(gè))
SSH版本:
struts:2.3.15.3
hibernate : 3.6.10
spring: 3.2.0
1.1 struts(14個(gè))
1.1.1基礎(chǔ)包(13個(gè))
- 解壓一個(gè)空的例子搀捷,直接拷貝里面的jar包即可
- 路徑:struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib
模板技術(shù) 歇僧,一般用于頁(yè)面靜態(tài)化
freemarker:擴(kuò)展名:*.ftl
velocity :擴(kuò)展名 *.vm
1.1.2 整合Spring
- struts 整合spring:struts2-spring-plugin-2.3.15.3.jar
1.2 spring(15個(gè))
1.2.1基礎(chǔ)包(12個(gè))
- 基礎(chǔ)(4個(gè)):4+1 , beans、core土铺、context掀序、expression 拐叉, commons-logging (struts已經(jīng)導(dǎo)入)
- AOP(4個(gè)):aop聯(lián)盟、spring aop 吩坝、aspect規(guī)范、spring aspect
- db(2個(gè)):jdbc哑蔫、tx
- 測(cè)試(1個(gè)):test
- web開(kāi)發(fā)(1個(gè)):spring web
1.2.2 其他(3個(gè))
- 數(shù)據(jù)庫(kù)驅(qū)動(dòng)(1個(gè)):mysql
- 連接池(1個(gè)):c3p0
- 整合hibernate(1個(gè)):spring orm
1.2.3 Struts2+Spring
1.3 Hibernate(11個(gè))
- 核心(1個(gè))
路徑:%h%\hibernate3.jar
- 核心(1個(gè))
- 2.必須(6個(gè))
路徑:%h%\lib\required
3.jpa規(guī)范 (1個(gè))
(java persistent api 持久api)钉寝,hibernate注解開(kāi)發(fā) @Entity @Id 等
路徑:%h%\lib\jpa4.整合log4j(1個(gè))
導(dǎo)入 log4j...jar (struts已經(jīng)導(dǎo)入)
整合(過(guò)渡):slf4j-log4j12-1.7.5.jar
- 5.二級(jí)緩存(2個(gè))
核心:ehcache-1.5.0.jar
依賴(lài):
backport-util-concurrent-2.1.jar
commons-logging (存在)
1.4 刪除重復(fù)jar包
2 spring整合hibernate:有hibernate.cfg.xml
2.1 創(chuàng)建表
create table t_user(
id int primary key auto_increment,
username varchar(50),
password varchar(32),
age int
);
2.2 PO 類(lèi)
- javabean
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
- 映射文件
- 新建xml添加約束
打開(kāi) 路徑:/org/hibernate/hibernate-mapping-3.0.dtd 配置文件,拷貝文件頭部的約束即可
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itheima.domain.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
2.3 dao層
- spring提供 HibernateTemplate 用于操作PO對(duì)象闸迷,類(lèi)似Hibernate Session對(duì)象嵌纲。
public class UserDaoImpl implements UserDao {
//需要spring注入模板
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User user) {
this.hibernateTemplate.save(user);
}
}
2.4 service層
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void register(User user) {
userDao.save(user);
}
}
2.5 hibernate.cfg.xml
-
新建xml添加約束
打開(kāi) 路徑:/org/hibernate/hibernate-configuration-3.0.dtd 配置文件,拷貝文件頭部的約束即可
<session-factory>
<!-- 1基本4項(xiàng) -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ee19_spring_day03</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<!-- 2 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 3 sql語(yǔ)句 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 4 自動(dòng)生成表(一般沒(méi)用) -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 5本地線(xiàn)程綁定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 導(dǎo)入映射文件 -->
<mapping resource="com/itheima/domain/User.hbm.xml"/>
</session-factory>
2.6 applicationContext.xml
2.6.1 添加命名空間
文件位置:/spring-framework-3.2.0.RELEASE-dist/spring-framework-3.2.0.RELEASE/docs/spring-framework-reference/html/xsd-config.html
打開(kāi)之后腥沽,湊齊,注意添加對(duì)應(yīng)的xsi:schemaLocation
xmlns:xsi
xmlns:aop
xmlns:tx
xmlns:context
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
2.6.2 加載hibernate配置文件
<!-- 1 加載hibenrate.cfg.xml 獲得SessionFactory
* configLocation確定配置文件位置
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 2創(chuàng)建模板
* 底層使用session逮走,session 有sessionFactory獲得
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2.6.3 dao和service
<!-- 3 dao -->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 4 service -->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
2.6.4 事務(wù)管理
<!-- 5 事務(wù)管理 -->
<!-- 5.1 事務(wù)管理器 :HibernateTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.2 事務(wù)詳情 ,給ABC進(jìn)行具體事務(wù)設(shè)置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 AOP編程今阳,ABCD 篩選 ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/>
</aop:config>
2.7 測(cè)試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestApp {
@Autowired
private UserService userService;
@Test
public void demo01(){
User user = new User();
user.setUsername("jack");
user.setPassword("1234");
user.setAge(18);
userService.register(user);
}
}
3 spring整合hibernate:沒(méi)有hibernate.cfg.xml
- 刪除hibernate.cfg.xml文件师溅,但需要保存文件內(nèi)容,將其配置spring中
- 修改dao層盾舌,繼承HibernateDaoSupport
3.1 修改spring墓臭,配置SessionFactory
<!-- 1.1加載properties文件 -->
<!-- 1.2 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<!-- 1.3配置 LocalSessionFactoryBean,獲得SessionFactory
* configLocation確定配置文件位置
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
1)dataSource 數(shù)據(jù)源
2)hibernateProperties hibernate其他配置項(xiàng)
3) 導(dǎo)入映射文件
mappingLocations 妖谴,確定映射文件位置窿锉,需要“classpath:” ,支持通配符 【】
<property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property>
<property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
mappingResources ,加載執(zhí)行映射文件膝舅,從src下開(kāi)始 嗡载。不支持通配符*
<property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property>
mappingDirectoryLocations ,加載指定目錄下的仍稀,所有配置文件
<property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property>
mappingJarLocations 鼻疮, 從jar包中獲得映射文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
</bean>
3.2 修改dao,使用HibernateDaoSupport
- 繼承HibernateDaoSupport
// 底層需要SessionFactory琳轿,自動(dòng)創(chuàng)建HibernateTemplate模板
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void save(User user) {
this.getHibernateTemplate().save(user);
}
}
- spring 刪除模板判沟,給dao注入SessionFactory
<!-- 3 dao -->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
4 struts整合spring:spring創(chuàng)建action
1.編寫(xiě)action類(lèi),并將其配置給spring 崭篡,spring可以注入service
2.編寫(xiě)struts.xml
3.表單jsp頁(yè)面
4.web.xml 配置
1.確定配置文件contextConfigLocation
2.配置監(jiān)聽(tīng)器 ContextLoaderListener
3.配置前端控制器 StrutsPrepareAndExecuteFitler
4.1 action類(lèi)
- 通用
public class UserAction extends ActionSupport implements ModelDriven<User> {
//1 封裝數(shù)據(jù)
private User user = new User();
@Override
public User getModel() {
return user;
}
//2 service
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
- 功能
/**
* 注冊(cè)
* @return
*/
public String register(){
userService.register(user);
return "success";
}
4.2 spring配置
<!-- 6 配置action -->
<bean id="userAction" class="com.itheima.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
4.3 struts配置
解壓一個(gè)空的例子挪哄,直接拷貝里面的 struts.xml 修改即可
路徑:struts-2.3.15.3\apps\struts2-blank\WEB-INF\classes\struts.xml
<struts>
<!-- 開(kāi)發(fā)模式 -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!-- 底層自動(dòng)從spring容器中通過(guò)名稱(chēng)獲得內(nèi)容, getBean("userAction") -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="success">/messag.jsp</result>
</action>
</package>
</struts>
4.4 jsp表單
<form action="${pageContext.request.contextPath}/userAction_register" method="post">
用戶(hù)名:<input type="text" name="username"/> <br/>
密碼:<input type="password" name="password"/> <br/>
年齡:<input type="text" name="age"/> <br/>
<input type="submit" />
</form>
4.5 配置web.xml
<!-- 1 確定spring xml位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 2 spring監(jiān)聽(tīng)器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3 struts 前端控制器 -->
<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>
5 struts整合spring:struts創(chuàng)建action
- 刪除spring action配置
- struts <action class="全限定類(lèi)名">
<package name="default" namespace="/" extends="struts-default">
<!-- 底層自動(dòng)從spring容器中通過(guò)名稱(chēng)獲得內(nèi)容琉闪, getBean("userAction") -->
<action name="userAction_*" class="com.itheima.web.action.UserAction" method="{1}">
<result name="success">/messag.jsp</result>
</action>
</package>
- 要求:Action類(lèi)中迹炼,必須提供service名稱(chēng)與 spring配置文件一致。(如果名稱(chēng)一樣,將自動(dòng)注入)
分析:
- struts 配置文件
default.properties ,常量配置文件
struts-default.xml 斯入,默認(rèn)核心配置文件
struts-plugins.xml 砂碉,插件配置文件
struts.xml,自定義核心配置文件
常量的使用刻两,后面配置項(xiàng)增蹭,將覆蓋前面的。
2.default.properties 磅摹,此配置文件中確定 按照【名稱(chēng)】自動(dòng)注入
/org/apache/struts2/default.properties
-
struts-plugins.xml ,struts整合spring
<constant name="struts.objectFactory" value="spring" />
struts的action將有spring創(chuàng)建
總結(jié)滋迈,之后action有spring創(chuàng)建,并按照名稱(chēng)自動(dòng)注入