Spring框架学习笔记4:ssh框架的整合

这是黑马视频的学习笔记

一、ssh整合思想


整合思想

二、struts2和spring的整合

把struts2的action交给spring管理

导入jar包=====>配置web.xml文件

web.xml配置struts的过滤器,spring的监听器并指定spring核心配置文件的位置

   <!--配置struts2的过滤器  -->
     <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>
    
    
   <!-- ServletContextListener接口用于在web项目启动的时候加载Spring的IOC容器 
 	就是配置监听器-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	 
     <!--部署applicationContext的xml文件-->  
    <!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,  
    在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。  
    如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:  
    在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。  
    也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,  
    applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。  
    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。-->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:bean.xml</param-value>  
    </context-param>  
    

spring和核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.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 "
	default-autowire="byName">
	
<!-- 配置C3P0连接池 -->
   <bean  id="dataSource" class="com.mchange.v2.c3p0.CombopooledDataSource">
    <!-- 注入属性值 -->
     <property name="driverClass"  value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl"  value="jdbc:mysql:///spring_day03-tx"></property>
    <property name="user"  value="root"></property>
     <property name="password"  value="666666"></property>
   </bean>

<!-- 创建action对象 -->
<bean id="userAction2" class="action.UserAction2" scope="prototype]"></bean>

</beans>

struts2的核心配置文件

<struts>
<!-- 开启动态方法访问 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />

    <package name="demo" extends="struts-default" namespace="/" >
        <!-- class属性值写action对象的id值 -->
        <action name="userAction" class="userAction2" >
        </action>     
    </package>
</struts>

三、spring与hibernate的整合

1.  数据库的信息由spring配置完成,不再用hibernate配置文件配置

2.hibernate里面的sessionfactory的创建也交给spring做,hibernate框架直接使用对象就可以

导入jar包======》创建实体类和映射文件、hibernate的核心配置文件

实体类代码

package cn.itcast.entity;

public class User {
  private Integer uid;
  private String uname;
  private String address;
public Integer getUid() {
	return uid;
}
public void setUid(Integer uid) {
	this.uid = uid;
}
public String getUname() {
	return uname;
}
public void setUname(String uname) {
	this.uname = uname;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
  
}

映射文件代码

<?xml version="1.0" encoding="UTF-8"?>
<!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="cn.itcast.entity.User" table="t_user">
      <id name="uid" column="uid">
         <generator class="native"></generator>
        </id>
      <property name="uname" column="uname"></property>
      <property name="address" column="address"></property>
      
</class>
</hibernate-mapping>

hibernate核心配置文件的代码

<?xml version="1.0" encoding="UTF-8"?>
<!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>
   
   <property name="hibernate.show_sql">true</property>
   <property name="hibernate.format_sql">true</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
   
   <mapping resource="cn/itcast/entity/User.hbm.xml"/>
   
   </session-factory> 
 </hibernate-configuration>
========》spring整合

spring的核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.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 "
	default-autowire="byName">
	
<!-- 配置C3P0连接池 -->
   <bean  id="dataSource" class="com.mchange.v2.c3p0.CombopooledDataSource">
    <!-- 注入属性值 -->
     <property name="driverClass"  value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl"  value="jdbc:mysql:///spring_day03-tx"></property>
    <property name="user"  value="root"></property>
     <property name="password"  value="666666"></property>
   </bean>

<!-- sessionFactory对象的创建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder">
   <!-- 指定hibernate核心配置文件,注入dataSource -->
   <property name="dataSource" ref="dataSource"></property>
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

四、dao使用hibernateTemplate

注;因为没有配置事务,会出现异常

1、action类的代码

package action;

import com.opensymphony.xwork2.ActionSupport;

import cn.itcast.service.UserService;

public class UserAction2 extends ActionSupport {

	private UserService userService;
	
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	@Override
	public String execute() throws Exception {
		return NONE;
	}
	
}

2、service类的代码

package cn.itcast.service;

import cn.itcast.dao.UserDao;

public class UserService {

	private UserDao userDao;

	public void setUserDao(UserDao userDaoImpl) {
		this.userDao = userDao;
	}
	
	
	public void add() {
		System.out.println("service.......");
		userDao.add();
		
	}
	
}

3、dao接口和类的代码

package cn.itcast.dao;

public interface UserDao {

	public void add();
}
package cn.itcast.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;

import cn.itcast.entity.User;



public class UserDaoImpl implements UserDao {

	//得到hibernateTemplate对象
	private HibernateTemplate hibernateTemplate;
		
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public void add() {
           User user=new User();
            user.setUname("tom");
            user.setAddress("tiantang");
		   hibernateTemplate.save(user);
	}

}

4、spring核心配置文件的代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.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 "
	default-autowire="byName">
	
<!-- 配置C3P0连接池 -->
   <bean  id="dataSource" class="com.mchange.v2.c3p0.CombopooledDataSource">
    <!-- 注入属性值 -->
     <property name="driverClass"  value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl"  value="jdbc:mysql:///spring_day03-tx"></property>
    <property name="user"  value="root"></property>
     <property name="password"  value="666666"></property>
   </bean>

<!-- sessionFactory对象的创建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder">
   <!-- 指定hibernate核心配置文件,注入dataSource -->
   <property name="dataSource" ref="dataSource"></property>
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 创建action对象 -->
<bean id="userAction2" class="action.UserAction2" scope="prototype]">
<property name="userService" ref="userService"></property>
</bean>

<!--创建service对象  -->
<bean id="userService" class="cn.itcast.service.UserService">
<property name="userDao" ref="userDaoImpl"></property>
</bean>

<!--创建dao对象,zhuru  -->
<bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>


<!-- 创建hibernateTemplate对象 -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionfactory" ref="sessionFactory"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
hibernate的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!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>
   
   <property name="hibernate.show_sql">true</property>
   <property name="hibernate.format_sql">true</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
   
   
   <mapping resource="cn/itcast/entity/User.hbm.xml"/>
   
   </session-factory> 
 </hibernate-configuration>

struts2的配置文件

<?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>
<!-- 开启动态方法访问 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />

    <package name="bookdemo2" extends="struts-default" namespace="/" >

        <action name="userAction*" class="userAction" >
        </action>  
    </package>
<include file="cn/itcast/action/hello.xml"></include>


</struts>

   
   

五、配置事务的代码

<!-- sessionFactory对象的创建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder">
   <!-- 指定hibernate核心配置文件,注入dataSource -->
   <property name="dataSource" ref="dataSource"></property>
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 开启扫描注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 创建action对象 -->
<bean id="userAction2" class="action.UserAction2" scope="prototype]">
<property name="userService" ref="userService"></property>
</bean>

service类的代码

@Transactional
public class UserService {

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	

六、常用的hibernateTemplate的方法

HibernateTemplate 提供了非常多的常用方法来完成基本的操作,比如增加、删除、修改及查询等操作,Spring 2.0 更增加对命名 SQL 查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate 的常规用法,就可完成大多数DAO对象的 CRUD操作。

    下面是 HibernateTemplate的常用方法。

    delete(Object entity): 删除指定持久化实例。

    deleteAll(Collection entities): 删除集合内全部持久化类实例。

    find(String queryString): 根据 HQL 查询字符串来返回实例集合。查询所有

    findByNamedQuery(String queryName): 根据命名查询返回实例集合。

    get(Classentity Class,Serializable id): 根据主键加载特定持久化类的实例。根据id查询

    save(Object entity): 保存新的实例。

    saveOrUpdate(Object entity): 根据实例状态,选择保存或者更新。

    update(Object entity): 更新实例的状态,要求entity 是持久状态。

    setMaxResults(intmax Results): 设置分页的大小。


七、没有hibernate核心配置文件的整合方式

spring整合hibernate时,可以不写hibernate核心配置文件,把原来在hibernate核心配置文件里的配置写在spring的核心配置文件中

<!-- sessionFactory对象的创建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder">
   <!-- 指定hibernate核心配置文件,注入dataSource -->
   <property name="dataSource" ref="dataSource"></property>
<!--    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->

<!--配置hibernate  -->
<property name="hibernateProperties">
   <props>
   <prop key="hibernate.show_sql">true</prop>
   <prop key="hibernate.format_sql">true</prop>
   <prop key="hibernate.hbm2ddl.auto">update</prop>
   <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
   </props>
</property>
<!--配置映射文件  -->
<property name="mappingRespurces">
<list>
<value>cn/itcast/entity/User.hbm.xml</value>
</list>
</property>

</bean>

八、spring的分模块开发

<import resource="classpath:"/>

猜你喜欢

转载自blog.csdn.net/shanshuisheng/article/details/81004110