标签:tap 通过 lis constant sub har spring容器 1.5 整合
简介:ssh的整合
struts:2.3.15.3
hibernate : 3.6.10
spring: 3.2.0
struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib
模板技术 ,一般用于页面静态化
freemarker:扩展名:*.ftl
velocity :扩展名 *.vm
l 基础:4+1 , beans、core、context、expression , commons-logging (struts已经导入)
l AOP:aop联盟、spring aop 、aspect规范、spring aspect
l db:jdbc、tx
l 测试:test
l web开发:spring web
l 驱动:mysql
l 连接池:c3p0
l 整合hibernate:spring orm
%h%\hibernate3.jar 核心
%h%\lib\required 必须
%h%\lib\jpa jpa规范 (java persistent api 持久api),hibernate注解开发 @Entity @Id 等
l 整合log4j
导入 log4j...jar (struts已经导入)
整合(过渡):slf4j-log4j12-1.7.5.jar
l 二级缓存
核心:ehcache-1.5.0.jar
依赖:
backport-util-concurrent-2.1.jar
commons-logging (存在)
l spring整合hibernate: spring orm
l struts 整合spring:struts2-spring-plugin-2.3.15.3.jar
删除重复jar包(39个)
create table t_user( id int primary key auto_increment, username varchar(50), password varchar(32), age int );
l javabean
1 public class User { 2 private Integer id; 3 private String username; 4 private String password; 5 private Integer age;
l 映射文件
<!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>
l spring提供 HibernateTemplate 用于操作PO对象,类似Hibernate Session对象。
1 public class UserDaoImpl implements UserDao { 2 3 4 5 //需要spring注入模板 6 7 private HibernateTemplate hibernateTemplate; 8 9 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { 10 11 this.hibernateTemplate = hibernateTemplate; 12 13 } 14 15 16 17 @Override 18 19 public void save(User user) { 20 21 this.hibernateTemplate.save(user); 22 23 } 24 25 26 27 }
1 public class UserServiceImpl implements UserService { 2 3 4 5 private UserDao userDao; 6 7 public void setUserDao(UserDao userDao) { 8 9 this.userDao = userDao; 10 11 } 12 13 @Override 14 15 public void register(User user) { 16 17 userDao.save(user); 18 19 } 20 21 22 23 }
1 <session-factory> 2 3 <!-- 1基本4项 --> 4 5 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 6 7 <property name="hibernate.connection.url">jdbc:mysql:///ee19_spring_day03</property> 8 9 <property name="hibernate.connection.username">root</property> 10 11 <property name="hibernate.connection.password">1234</property> 12 13 14 15 <!-- 2 配置方言 --> 16 17 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 18 19 20 21 <!-- 3 sql语句 --> 22 23 <property name="hibernate.show_sql">true</property> 24 25 <property name="hibernate.format_sql">true</property> 26 27 28 29 <!-- 4 自动生成表(一般没用) --> 30 31 <property name="hibernate.hbm2ddl.auto">update</property> 32 33 34 35 <!-- 5本地线程绑定 --> 36 37 <property name="hibernate.current_session_context_class">thread</property> 38 39 40 41 <!-- 导入映射文件 --> 42 43 <mapping resource="com/itheima/domain/User.hbm.xml"/> 44 45 46 47 </session-factory>
<?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">
1 <!-- 1 加载hibenrate.cfg.xml 获得SessionFactory 2 3 * configLocation确定配置文件位置 4 5 --> 6 7 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 8 9 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 10 11 </bean> 12 13 14 15 <!-- 2创建模板 16 17 * 底层使用session,session 有sessionFactory获得 18 19 --> 20 21 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 22 23 <property name="sessionFactory" ref="sessionFactory"></property> 24 25 </bean>
1 <!-- 3 dao --> 2 3 <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> 4 5 <property name="hibernateTemplate" ref="hibernateTemplate"></property> 6 7 </bean> 8 9 10 11 <!-- 4 service --> 12 13 <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"> 14 15 <property name="userDao" ref="userDao"></property> 16 17 </bean>
1 <!-- 5 事务管理 --> 2 3 <!-- 5.1 事务管理器 :HibernateTransactionManager --> 4 5 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > 6 7 <property name="sessionFactory" ref="sessionFactory"></property> 8 9 </bean> 10 11 <!-- 5.2 事务详情 ,给ABC进行具体事务设置 --> 12 13 <tx:advice id="txAdvice" transaction-manager="txManager"> 14 15 <tx:attributes> 16 17 <tx:method name="register"/> 18 19 </tx:attributes> 20 21 </tx:advice> 22 23 <!-- 5.3 AOP编程,ABCD 筛选 ABC --> 24 25 <aop:config> 26 27 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/> 28 29 </aop:config>
1 @RunWith(SpringJUnit4ClassRunner.class) 2 3 @ContextConfiguration(locations="classpath:applicationContext.xml") 4 5 public class TestApp { 6 7 8 9 @Autowired 10 11 private UserService userService; 12 13 14 15 @Test 16 17 public void demo01(){ 18 19 User user = new User(); 20 21 user.setUsername("jack"); 22 23 user.setPassword("1234"); 24 25 user.setAge(18); 26 27 28 29 userService.register(user); 30 31 } 32 33 }
l 删除hibernate.cfg.xml文件,但需要保存文件内容,将其配置spring中
l 修改dao层,继承HibernateDaoSupport
1 <!-- 1.1加载properties文件 --> 2 <!-- 1.2 配置数据源 --> 3 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 4 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 5 <property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property> 6 <property name="user" value="root"></property> 7 <property name="password" value="1234"></property> 8 </bean> 9 10 <!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory 11 * configLocation确定配置文件位置 12 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 13 1)dataSource 数据源 14 2)hibernateProperties hibernate其他配置项 15 3) 导入映射文件 16 mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】 17 <property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property> 18 <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> 19 mappingResources ,加载执行映射文件,从src下开始 。不支持通配符* 20 <property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property> 21 mappingDirectoryLocations ,加载指定目录下的,所有配置文件 22 <property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property> 23 mappingJarLocations , 从jar包中获得映射文件 24 --> 25 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 26 <property name="dataSource" ref="dataSource"></property> 27 <property name="hibernateProperties"> 28 <props> 29 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 30 <prop key="hibernate.show_sql">true</prop> 31 <prop key="hibernate.format_sql">true</prop> 32 <prop key="hibernate.hbm2ddl.auto">update</prop> 33 <prop key="hibernate.current_session_context_class">thread</prop> 34 </props> 35 </property> 36 <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> 37 </bean>
l 继承HibernateDaoSupport
1 // 底层需要SessionFactory,自动创建HibernateTemplate模板 2 3 public class UserDaoImpl extends HibernateDaoSupport implements UserDao { 4 5 6 7 @Override 8 9 public void save(User user) { 10 11 this.getHibernateTemplate().save(user); 12 13 } 14 15 16 17 }
l spring 删除模板,给dao注入SessionFactory
1 <!-- 3 dao --> 2 3 <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> 4 5 <property name="sessionFactory" ref="sessionFactory"></property> 6 7 </bean>
1.编写action类,并将其配置给spring ,spring可以注入service
2.编写struts.xml
3.表单jsp页面
4.web.xml 配置
1.确定配置文件contextConfigLocation
2.配置监听器 ContextLoaderListener
3.配置前端控制器 StrutsPrepareAndExecuteFitler
l 通用
1 public class UserAction extends ActionSupport implements ModelDriven<User> { 2 3 4 5 //1 封装数据 6 7 private User user = new User(); 8 9 10 11 @Override 12 13 public User getModel() { 14 15 return user; 16 17 } 18 19 20 21 //2 service 22 23 private UserService userService; 24 25 public void setUserService(UserService userService) { 26 27 this.userService = userService; 28 29 }
l 功能
1 /** 2 3 * 注册 4 5 * @return 6 7 */ 8 9 public String register(){ 10 11 userService.register(user); 12 13 return "success"; 14 15 }
1.4.2 spring配置
1 <!-- 6 配置action --> 2 3 <bean id="userAction" class="com.itheima.web.action.UserAction" scope="prototype"> 4 5 <property name="userService" ref="userService"></property> 6 7 </bean>
1 <struts> 2 3 <!-- 开发模式 --> 4 5 <constant name="struts.devMode" value="true" /> 6 7 8 9 <package name="default" namespace="/" extends="struts-default"> 10 11 <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> 12 13 <action name="userAction_*" class="userAction" method="{1}"> 14 15 <result name="success">/messag.jsp</result> 16 17 </action> 18 19 </package> 20 21 </struts>
1 <form action="${pageContext.request.contextPath}/userAction_register" method="post"> 2 3 用户名:<input type="text" name="username"/> <br/> 4 5 密码:<input type="password" name="password"/> <br/> 6 7 年龄:<input type="text" name="age"/> <br/> 8 9 <input type="submit" /> 10 11 </form>
1 <!-- 1 确定spring xml位置 --> 2 3 <context-param> 4 5 <param-name>contextConfigLocation</param-name> 6 7 <param-value>classpath:applicationContext.xml</param-value> 8 9 </context-param> 10 11 <!-- 2 spring监听器 --> 12 13 <listener> 14 15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16 17 </listener> 18 19 <!-- 3 struts 前端控制器 --> 20 21 <filter> 22 23 <filter-name>struts2</filter-name> 24 25 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 26 27 </filter> 28 29 <filter-mapping> 30 31 <filter-name>struts2</filter-name> 32 33 <url-pattern>/*</url-pattern> 34 35 </filter-mapping>
l 删除spring action配置
l struts <action class="全限定类名">
<package name="default" namespace="/" extends="struts-default"> <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> <action name="userAction_*" class="com.itheima.web.action.UserAction" method="{1}"> <result name="success">/messag.jsp</result> </action> </package> |
l 要求:Action类中,必须提供service名称与 spring配置文件一致。(如果名称一样,将自动注入)
分析:
1. struts 配置文件
default.properties ,常量配置文件
struts-default.xml ,默认核心配置文件
struts-plugins.xml ,插件配置文件
struts.xml,自定义核心配置文件
常量的使用,后面配置项,将覆盖前面的。
2.default.properties ,此配置文件中确定 按照【名称】自动注入
/org/apache/struts2/default.properties
3. struts-plugins.xml ,struts整合spring
<constant name="struts.objectFactory" value="spring" />
struts的action将有spring创建
总结,之后action有spring创建,并按照名称自动注入
标签:tap 通过 lis constant sub har spring容器 1.5 整合
原文地址:http://www.cnblogs.com/soficircle/p/7113539.html