S2SH框架整合注解版(十)

简介: S2SH框架整合注解版(十)

一. 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"/>


20190523191044888.png


八. 其余配置


其余的配置,都是不变的。 但是为了方便,将所有的配置信息都列举出来。


八.一 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>


谢谢!!!

相关文章
|
9月前
|
Java 应用服务中间件 Maven
解析Spring Boot中的Profile:配置文件与代码的双重掌控
解析Spring Boot中的Profile:配置文件与代码的双重掌控
|
29天前
|
存储 JSON 前端开发
利用Spring MVC开发程序2
利用Spring MVC开发程序
27 1
|
29天前
|
设计模式 JSON 前端开发
利用Spring MVC开发程序1
利用Spring MVC开发程序
25 0
|
9月前
|
Java 测试技术 Spring
Spring @Profile注解使用和源码解析
在之前的文章中,写了一篇使用Spring @Profile实现开发环境,测试环境,生产环境的切换,之前的文章是使用SpringBoot项目搭建,实现了不同环境数据源的切换,在我们实际开发中,会分为dev,test,prod等环境,他们之间数独立的,今天进来详解介绍Spring @Profile的原理。
60 0
|
XML Java 数据格式
Spring-注入参数详解-[简化配置方式]
Spring-注入参数详解-[简化配置方式]
68 0
ssm-事务--使用配置文件方式进行配置,使用注解进行使用
ssm-事务--使用配置文件方式进行配置,使用注解进行使用
|
Java 测试技术 Spring
Spring系列(十一):@Profile 注解用法介绍
在Spring容器中如果存在同一类型的多个组件,可以使用@Profile注解标识实际要获取的是哪一个bean,这在不同的环境使用不同的变量的场景下非常有用。 最典型的例子:开发环境、测试环境、生产环境会配置不同的数据源,在尽量不修改代码的情况下,可以使用这个注解来动态指定要连接的数据源。
Spring系列(十一):@Profile 注解用法介绍
|
NoSQL Java 数据库连接
聊聊Spring的环境抽象Environment,以及配置@Profile使用详解(介绍profile的6种激活方式)【享学Spring】(下)
聊聊Spring的环境抽象Environment,以及配置@Profile使用详解(介绍profile的6种激活方式)【享学Spring】(下)
聊聊Spring的环境抽象Environment,以及配置@Profile使用详解(介绍profile的6种激活方式)【享学Spring】(下)
|
开发框架 前端开发 Java
Spring MVC开发拦截器
Spring MVC开发拦截器
|
XML 前端开发 Java