爱一个人是一回事,拥有一个人是另外一回事,爱上并拥有一个人,更是另外一回事。 蝴蝶,是什么,是希望,是真正的希望。 上一章简单介绍了S2SH框架整合XML版(九),如果没有看过,请观看上一章。
 
一. S2SH 注解版
 
将bean 的创建,与事务管理进行注解形式的开发。 与上一章有着非常紧密的联系,如果没有看过上一章,请观看上一章。
 
二. 开启注解扫描
 
<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.yjl"></context:component-scan>
 
三.将bean 的xml 形式创建,事务控制进行删除。
 
<!--全部进行移除-->
<!-- 设置userDao类 -->
	<bean id="userDao" class="com.yjl.dao.impl.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<!-- 设置userService -->
	<bean id="userService" class="com.yjl.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- 设置userAction -->
	<bean id="userAction" class="com.yjl.web.action.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
<!--移除事务-->
<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>
    </tx:advice>
    <!-- 配置aop -->
    <aop:config>
    	<aop:pointcut expression="execution( * com.yjl.service.impl.*.*(..))" id="pointCut1"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut1"/>
    </aop:config>
 
四. UserDaoImpl 的写法
 
package com.yjl.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.yjl.dao.UserDao;
import com.yjl.pojo.User;
/**
 @author:yuejl
 @date: 2019年5月22日 下午8:04:43
 @Description 类的相关描述
*/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll() {
		System.out.println("执行了查询全部的方法");
		List<User> userList=new ArrayList<User>();
		String hql="from User";
		System.out.println("输出值为:"+hibernateTemplate);
		userList=(List<User>) hibernateTemplate.find(hql);
		return userList;
	}
}
 
五. UserServiceImpl 的写法
 
package com.yjl.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yjl.dao.UserDao;
import com.yjl.pojo.User;
import com.yjl.service.UserService;
/**
 @author:yuejl
 @date: 2019年5月22日 下午8:05:15
 @Description 类的相关描述
*/
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	@Resource(name="userDao")
	private UserDao userDao;
	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}
}
 
六. UserAction 的写法
 
package com.yjl.web.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.yjl.pojo.User;
import com.yjl.service.UserService;
/**
 @author:yuejl
 @date: 2019年5月22日 下午8:05:24
 @Description 类的相关描述
*/
@Controller
@Scope("prototype")
public class UserAction {
	@Resource(name="userService")
	private UserService userService;
	public String list(){
		List<User> userList=userService.findAll();
		ActionContext.getContext().getValueStack().
		set("userList", userList);
		return "list";
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
 
这样,就替换了bean 的创建,但还没有替换事务控制
 
七.事务控制注解
 
<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
	<!-- 开启注解事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
 
 
八. 其余配置
 
其余的配置,都是不变的。 但是为了方便,将所有的配置信息都列举出来。
 
八.一 applicationContext.xml
 
<?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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 采用注解的方式进行 -->
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.yjl"></context:component-scan>
	<!-- 引入配置文件  前面要加上classpath-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
	<!-- 创建sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
	</bean>
	<!-- 创建Hibernate模板 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
	<!-- 开启注解事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
 
八.二 hibernate.cfg.xml
 
<?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>
		<!-- 关于Hibernate的相应配置 -->
		<!-- 引入方言 -->
		<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.format_sql">true</property>
		<!-- 引入相应的约束文件  ctrl点击时可以正确进入-->
	 	<mapping resource="com/yjl/pojo/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
 
八.三 struts.xml
 
<?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.i18n.encoding" value="UTF-8"></constant>
	<!--修改是否为开发者模式 -->
	<constant name="struts.enable.devMode" value="true"></constant>
	<package name="user" extends="struts-default" namespace="/">
	<action name="User_*" class="userAction" method="{1}">
			<result name="list">list.jsp</result>
		</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整合</display-name>
  <!-- 配置spring 启动时的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置启动参数 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置全局编码格式 -->
   <filter> 
		<filter-name>CharacterEncodingFilter</filter-name> 
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
		<init-param> 
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value> 
		</init-param> 
		<init-param> 
			<param-name>forceEncoding</param-name> 
			<param-value>true</param-value> 
		</init-param> 
	</filter> 
	<filter-mapping> 
		<filter-name>CharacterEncodingFilter</filter-name> 
		<url-pattern>/*</url-pattern> 
	</filter-mapping>
  
   <!-- 配置struts2的过滤器 -->
  <filter>
  		<filter-name>struts</filter-name>
  		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  		<filter-name>struts</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>  
    <init-param>  
      <param-name>singleSession</param-name>  
      <param-value>true</param-value>  
    </init-param>  
  </filter>  
    
   <filter-mapping>  
    <filter-name>openSessionInViewFilter</filter-name>  
    <url-pattern>*.action</url-pattern>  
  </filter-mapping>  
  <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>
 
谢谢!!!