Spring 4 : 整合 SSH

Posted 阿肯新

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 4 : 整合 SSH相关的知识,希望对你有一定的参考价值。

简介:ssh的整合

 

1       SSH整合

1.1   jar整合

struts:2.3.15.3

hibernate : 3.6.10

spring: 3.2.0

 

1.1.1   struts(jar包导入)

struts-2.3.15.3\\apps\\struts2-blank\\WEB-INF\\lib

 

  模板技术  ,一般用于页面静态化

  freemarker:扩展名:*.ftl

  velocity :扩展名  *.vm

1.1.2   spring(jar包导入)

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

 

 

1.1.3   hibernate(jar包导入)

%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  (存在)

1.1.4   整合包(jar包导入)

l    spring整合hibernate: spring orm

l    struts 整合spring:struts2-spring-plugin-2.3.15.3.jar

删除重复jar包(39个)

 

1.2   spring整合hibernate:有hibernate.cfg.xml

1.2.1   创建表

create table t_user(

  id int primary key auto_increment,

  username varchar(50),

  password varchar(32),

  age int

);
sql

 

1.2.2   PO 类

 

l  javabean

1 public class User {
2     private Integer id;
3     private String username;
4     private String password;
5     private Integer age;
javaBean

 

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>
bean.hbm.xml

 

1.2.3   dao层

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 }
dao

 

1.2.4   service层

 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 }
service

 

1.2.5   hibernate.cfg.xml

 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>
Hibernate.cfg.xml

 

1.2.6   applicationContext.xml

1.2.6.1         添加命名空间

<?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">
applicationContext.xml

 

1.2.6.2         加载hibernate配置文件

 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>
View Code

 

1.2.6.3         dao和service

 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>
View Code

 

1.2.6.4         事务管理

 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>
View Code

 

1.2.7   测试

 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 }
TestApp

 

1.3   spring整合hibernate:没有hibernate.cfg.xml 【】

l    删除hibernate.cfg.xml文件,但需要保存文件内容,将其配置spring中

l    修改dao层,继承HibernateDaoSupport

 

1.3.1   修改spring,配置SessionFactory

 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>
applicationContext.xml

 

1.3.2   修改dao,使用HibernateDaoSupport

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 }
dao

 

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>
applicationContext.xml

 

1.4   struts整合spring:spring创建action

1.编写action类,并将其配置给spring ,spring可以注入service

2.编写struts.xml

3.表单jsp页面

4.web.xml 配置

       1.确定配置文件contextConfigLocation

       2.配置监听器 ContextLoaderListener

       3.配置前端控制器 StrutsPrepareAndExecuteFitler

 

1.4.1   action类

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    }
action

 

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    }
View Code

 

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>
View Code

1.4.3   struts配置

 1 <struts>
 2 
 3     <!-- 开发模式 -->
 4 
 5     <constant name="struts.devMode" value="true" />
 6 
 7  
 8 
 9     <package name="default"spring的使用-ssh整合

ssh笔记整合

Spring---SSH整合

(转)hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合

SSH整合的详细步骤

SSH注解整合