Spring框架学习

文章40 |   阅读 21323 |   点赞0

来源:https://blog.csdn.net/yerenyuan_pku/category_6457009.html

Spring入门第十二讲——Hibernate-5.0.7+Struts-2.3.24+Spring-4.2.4三大框架纯注解方式的整合

x33g5p2x  于2021-12-18 转载在 其他  
字(18.7k)|赞(0)|评价(0)|浏览(536)

还记得我在《Spring入门第十讲——Hibernate-5.0.7+Struts-2.3.24+Spring-4.2.4三大框架整合开发》这一讲中介绍SSH三大框架整合的时候,说过这三个框架的整合一共有三种方式,其中前两种方式已经讲过了,本讲介绍第三种方式,即纯注解的方式。

搭建SSH注解开发的环境

创建web项目,引入jar包

创建一个web项目,例如ssh3,并引入相关的jar包,那到底引入哪些jar包呢?相信你看过我的这篇文章——《Spring入门第十讲——Hibernate-5.0.7+Struts-2.3.24+Spring-4.2.4三大框架整合开发》的话,就知道应该要导入哪些jar包了。总体来讲,你可能需要导入如下44个jar包。

尤其要注意引入struts2-convention-plugin-2.3.24.jar这个jar包,因为它是Struts2的注解开发包,你要想使用Struts2的注解,必然就要在项目中引入该jar包。

创建数据库和表

创建一个数据库,例如s2sh_crm,并在该数据库下新建一张客户表,这里笔者使用的数据库是MySQL。

  1. create database s2sh_crm;
  2. use s2sh_crm;
  3. CREATE TABLE `cst_customer` (
  4. `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  5. `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  6. `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  7. `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  8. `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  9. `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  10. `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  11. PRIMARY KEY (`cust_id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

引入相关的配置文件

首先,既然是使用纯注解的方式来整合这三个框架,那么Struts2框架所须的配置文件就没必要有了。虽说struts.xml文件是可以不用写了,但是我们还是应在项目的web.xml文件中配置Struts2的核心过滤器。

然后,Struts2框架所须的配置文件没了,不代表Spring框架所须的配置文件就没必要创建了。之前讲Spring整合Hibernate框架的时候,我们可以不必创建Hibernate的核心配置文件,其里面应有的数据库连接的配置信息(包括数据库驱动类的全名称、要连接的数据库、用户名以及密码等)可以抽取到一个属性文件(例如jdbc.properties)当中,而且建议将该属性文件置于项目的src目录下。

  1. jdbc.driverClass=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql:///s2sh_crm
  3. jdbc.username=root
  4. jdbc.password=liayun

接着,在Spring配置文件(例如applicationContext.xml)中引入外部的jdbc.properties属性文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!-- 引入外部属性文件 -->
  4. <context:property-placeholder location="classpath:jdbc.properties" />
  5. <!-- 配置C3P0连接池 -->
  6. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  7. <property name="driverClass" value="${jdbc.driverClass}" />
  8. <property name="jdbcUrl" value="${jdbc.url}" />
  9. <property name="user" value="${jdbc.username}" />
  10. <property name="password" value="${jdbc.password}" />
  11. </bean>
  12. </beans>

在以上applicationContext.xml文件中,可以看到我们的项目使用的是C3P0连接池,连接池对象中所使用到的配置信息都来自于src目录下的jdbc.properties属性文件当中。
紧接着,就要使用Spring中的核心监听器(即ContextLoaderListener)来整合web项目了。咋整合呢?在web.xml文件中配置Spring中的核心监听器(即ContextLoaderListener),这样在服务器启动的时候,它就会加载Spring的配置文件了,并且还要手动配置让其加载类路径下的配置文件。

最后,还要记得在src目录下引入Log4j的配置文件(log4j.properties)哟!也就是日志记录文件,该文件内容如下:

  1. ### direct log messages to stdout ###
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.err
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  6. ### direct messages to file mylog.log ###
  7. log4j.appender.file=org.apache.log4j.FileAppender
  8. log4j.appender.file.File=c\:mylog.log
  9. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  11. ### set log levels - for more verbose logging change 'info' to 'debug' ###
  12. # error warn info debug trace
  13. log4j.rootLogger= info, stdout

创建相关的包结构

引入相关的页面

该项目所用到的相关后台页面,大家也没必要费心费力地写了,直接点击我这里给的百度网盘链接进行下载即可。

将下载下来的所有东东拷贝到我们的项目中之后,发布我们的项目,试着访问一下项目的首页,点击新增客户超链接,看能不能跳转到客户添加页面。

至此,SSH纯注解开发的环境算是搭建好了,接下来,就要编写代码实现保存客户这一功能了。

编写代码实现保存客户的功能

创建Customer实体类

在com.meimeixia.ssh.domain包下创建一个Customer实体类。

  1. package com.meimeixia.ssh.domain;
  2. /** * 客户管理的实体 * @author liayun * */
  3. public class Customer {
  4. private Long cust_id;
  5. private String cust_name;
  6. private String cust_source;
  7. private String cust_industry;
  8. private String cust_level;
  9. private String cust_phone;
  10. private String cust_mobile;
  11. public Long getCust_id() {
  12. return cust_id;
  13. }
  14. public void setCust_id(Long cust_id) {
  15. this.cust_id = cust_id;
  16. }
  17. public String getCust_name() {
  18. return cust_name;
  19. }
  20. public void setCust_name(String cust_name) {
  21. this.cust_name = cust_name;
  22. }
  23. public String getCust_source() {
  24. return cust_source;
  25. }
  26. public void setCust_source(String cust_source) {
  27. this.cust_source = cust_source;
  28. }
  29. public String getCust_industry() {
  30. return cust_industry;
  31. }
  32. public void setCust_industry(String cust_industry) {
  33. this.cust_industry = cust_industry;
  34. }
  35. public String getCust_level() {
  36. return cust_level;
  37. }
  38. public void setCust_level(String cust_level) {
  39. this.cust_level = cust_level;
  40. }
  41. public String getCust_phone() {
  42. return cust_phone;
  43. }
  44. public void setCust_phone(String cust_phone) {
  45. this.cust_phone = cust_phone;
  46. }
  47. public String getCust_mobile() {
  48. return cust_mobile;
  49. }
  50. public void setCust_mobile(String cust_mobile) {
  51. this.cust_mobile = cust_mobile;
  52. }
  53. }

由于是SSH三大框架纯注解方式的整合开发,所以不必创建该实体类相对应的映射配置文件,没了映射配置文件,那到底如何建立表和类之间的映射关系呢?别急,后面会使用Hibernate的注解来实现。

Spring框架整合Struts2框架

首先,在com.meimeixia.ssh.web.action包下创建一个CustomerAction类,并在该类中编写如下的一个保存客户的方法(save方法)。

  1. package com.meimeixia.ssh.web.action;
  2. import com.meimeixia.ssh.domain.Customer;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import com.opensymphony.xwork2.ModelDriven;
  5. /** * 客户管理的Action类 * @author liayun * */
  6. public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
  7. //模型驱动使用的对象
  8. private Customer customer = new Customer();
  9. @Override
  10. public Customer getModel() {
  11. return customer;
  12. }
  13. //保存客户的方法
  14. public String save() {
  15. System.out.println("CustomerAction中的save方法执行了......");
  16. return NONE;
  17. }
  18. }

然后,在Spring中配置以上Action类。之前,我们都是这样配置的:将Action类交给Spring来管理,于是在Spring核心配置文件中就要添加如下的配置。

  1. <bean id="customerAction" class="com.meimeixia.ssh.web.action.CustomerAction" scope="prototype">
  2. </bean>

但我们现在使用的是纯注解方式,所以就不能像上面那样配置了,得在Action类上使用@Controller注解和@Scope注解。大家一定要记得在类上使用注解时,要开启组件扫描,即在Spring核心配置文件中添加如下的配置。

  1. <!-- 开启组件扫描,即将我们的类交给Spring去管理,直接扫描父包 -->
  2. <context:component-scan base-package="com.meimeixia.ssh" />

一旦开启了组件扫描,就可以在我们所创建的CustomerAction类上添加@Controller注解和@Scope注解了。

  1. package com.meimeixia.ssh.web.action;
  2. import org.springframework.context.annotation.Scope;
  3. import org.springframework.stereotype.Controller;
  4. import com.meimeixia.ssh.domain.Customer;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. import com.opensymphony.xwork2.ModelDriven;
  7. /** * 客户管理的Action类 * @author liayun */
  8. @Controller("customerAction")
  9. @Scope("prototype")
  10. public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
  11. //模型驱动使用的对象
  12. private Customer customer = new Customer();
  13. @Override
  14. public Customer getModel() {
  15. return customer;
  16. }
  17. //保存客户的方法
  18. public String save() {
  19. System.out.println("CustomerAction中的save方法执行了......");
  20. return NONE;
  21. }
  22. }

接着,还要在Struts2中配置以上Action类,用于负责处理请求和页面跳转。之前,我们都是在Struts2配置文件中添加像下面这样的配置。

  1. <package name="demo" extends="struts-default" namespace="/">
  2. <action name="customer_save" class="customerAction" method="save">
  3. </action>
  4. <action name="customer_update" class="customerAction" method="update">
  5. </action>
  6. </package>

但我们现在使用的是纯注解方式,所以就不能像上面那样配置了,得使用注解了。

这时,你可以做一个测试,发布咱们的项目到Tomcat服务器中,访问项目的首页之后,点击新增客户超链接,在跳转到客户添加页面后,直接点击保存按钮,那么你就会看到Eclipse控制台打印了如下信息,这就说明了程序并没有任何问题。

在Action类中调用service层中的服务

现在,咱就需要在Action类中调用service层中的服务了。哎呀!service层中的接口和实现类咱也没有写啊!所以,接下来,我们还要编写service层中的接口和实现类。首先,在com.meimeixia.ssh.service包下编写如下一个名为CustomerService的接口。

  1. package com.meimeixia.ssh.service;
  2. import com.meimeixia.ssh.domain.Customer;
  3. /** * 客户管理的业务层的接口 * @author liayun * */
  4. public interface CustomerService {
  5. void save(Customer customer);
  6. }

然后,再在com.meimeixia.ssh.service.impl包下编写一个以上接口的实现类,即CustomerServiceImpl.java。

  1. package com.meimeixia.ssh.service.impl;
  2. import com.meimeixia.ssh.domain.Customer;
  3. import com.meimeixia.ssh.service.CustomerService;
  4. /** * 客户管理的service的实现类 * @author liayun */
  5. public class CustomerServiceImpl implements CustomerService {
  6. @Override
  7. public void save(Customer customer) {
  8. System.out.println("CustomerServiceImpl类中的save方法执行了......");
  9. }
  10. }

接着,便要将以上CustomerServiceImpl实现类交给Spring来管理了。之前,我们都是在Spring核心配置文件中添加像下面这样的配置。

  1. <bean id="customerService" class="com.meimeixia.ssh.service.impl.CustomerServiceImpl">
  2. </bean>

但我们现在使用的是纯注解方式,所以就不能像上面那样配置了,得在类上使用@Service注解。

  1. package com.meimeixia.ssh.service.impl;
  2. import org.springframework.stereotype.Service;
  3. import com.meimeixia.ssh.domain.Customer;
  4. import com.meimeixia.ssh.service.CustomerService;
  5. /** * 客户管理的service的实现类 * @author liayun */
  6. @Service("customerService")
  7. public class CustomerServiceImpl implements CustomerService {
  8. @Override
  9. public void save(Customer customer) {
  10. System.out.println("CustomerServiceImpl类中的save方法执行了......");
  11. }
  12. }

紧接着,在CustomerAction类中使用注解注入CustomerService类型的属性,并在save方法中调用service层中的服务。

  1. package com.meimeixia.ssh.web.action;
  2. import javax.annotation.Resource;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Namespace;
  5. import org.apache.struts2.convention.annotation.ParentPackage;
  6. import org.springframework.context.annotation.Scope;
  7. import org.springframework.stereotype.Controller;
  8. import com.meimeixia.ssh.domain.Customer;
  9. import com.meimeixia.ssh.service.CustomerService;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import com.opensymphony.xwork2.ModelDriven;
  12. /** * 客户管理的Action类 * @author liayun * */
  13. @Controller("customerAction")
  14. @Scope("prototype")
  15. @ParentPackage("struts-default")
  16. @Namespace("/")
  17. public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
  18. //模型驱动使用的对象
  19. private Customer customer = new Customer();
  20. @Override
  21. public Customer getModel() {
  22. return customer;
  23. }
  24. //注入service
  25. @Resource(name="customerService")
  26. private CustomerService customerService;
  27. //保存客户的方法
  28. @Action(value="customer_save")
  29. public String save() {
  30. System.out.println("CustomerAction中的save方法执行了......");
  31. customerService.save(customer);
  32. return NONE;
  33. }
  34. }

这时,不妨做个测试,发布咱们的项目到Tomcat服务器中,访问项目的首页之后,点击新增客户超链接,在跳转到客户添加页面后,直接点击保存按钮,那么你就会看到Eclipse控制台打印了如下信息,这就说明了程序并没有任何问题。

在service层调用dao层对外提供的方法

现在,咱就需要在service层中调用dao层中对外提供的方法了。哎呀!dao层中的接口和实现类咱也没有写啊!所以,接下来,我们还要编写dao层中的接口和实现类。首先,在com.meimeixia.ssh.dao包下编写如下一个名为CustomerDao的接口。

  1. package com.meimeixia.ssh.dao;
  2. import com.meimeixia.ssh.domain.Customer;
  3. /** * 客户管理的dao的接口 * @author liayun * */
  4. public interface CustomerDao {
  5. void save(Customer customer);
  6. }

然后,再在com.meimeixia.ssh.dao.impl包下编写一个以上接口的实现类,即CustomerDaoImpl.java。

  1. package com.meimeixia.ssh.dao.impl;
  2. import com.meimeixia.ssh.dao.CustomerDao;
  3. import com.meimeixia.ssh.domain.Customer;
  4. /** * 客户管理的dao的实现类 * @author liayun */
  5. public class CustomerDaoImpl implements CustomerDao {
  6. @Override
  7. public void save(Customer customer) {
  8. System.out.println("CustomerDaoImpl类中的save方法执行了......");
  9. }
  10. }

接着,便要将以上CustomerDaoImpl实现类交给Spring来管理了。之前,我们都是在Spring核心配置文件中添加像下面这样的配置。

  1. <bean id="customerDao" class="com.meimeixia.ssh.dao.impl.CustomerDaoImpl">
  2. </bean>

但我们现在使用的是纯注解方式,所以就不能像上面那样配置了,得在类上使用@Repository注解。

  1. package com.meimeixia.ssh.dao.impl;
  2. import org.springframework.stereotype.Repository;
  3. import com.meimeixia.ssh.dao.CustomerDao;
  4. import com.meimeixia.ssh.domain.Customer;
  5. /** * 客户管理的dao的实现类 * @author liayun */
  6. @Repository("customerDao")
  7. public class CustomerDaoImpl implements CustomerDao {
  8. @Override
  9. public void save(Customer customer) {
  10. System.out.println("CustomerDaoImpl类中的save方法执行了......");
  11. }
  12. }

紧接着,在CustomerServiceImpl实现类中使用注解注入CustomerDao类型的属性,并在save方法中调用dao层中的对外提供的方法。

  1. package com.meimeixia.ssh.service.impl;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Service;
  4. import com.meimeixia.ssh.dao.CustomerDao;
  5. import com.meimeixia.ssh.domain.Customer;
  6. import com.meimeixia.ssh.service.CustomerService;
  7. /** * 客户管理的service的实现类 * @author liayun * 现在要将Service交给Spring去管理,原来得写成下面这样: */
  8. @Service("customerService")
  9. public class CustomerServiceImpl implements CustomerService {
  10. //注入dao
  11. @Resource(name="customerDao")
  12. private CustomerDao customerDao;
  13. @Override
  14. public void save(Customer customer) {
  15. System.out.println("CustomerServiceImpl类中的save方法执行了......");
  16. customerDao.save(customer);
  17. }
  18. }

这时,不妨做个测试,发布咱们的项目到Tomcat服务器中,访问项目的首页之后,点击新增客户超链接,在跳转到客户添加页面后,直接点击保存按钮,那么你就会看到Eclipse控制台打印了如下信息,这就说明了程序并没有任何问题。

Spring框架整合Hibernate框架

在创建Customer实体类时,我们并没有创建该实体类相对应的映射配置文件,那到底如何建立表和类之间的映射关系呢?之前,我们使用的是映射配置文件,现在得使用Hibernate的注解来建立表中的字段和类中的属性之间的关系了。所以,我们得重新修改Customer实体类。

你又要说了,那配置一对多关联映射和多对多关联映射,又该用啥子注解呢?这得需要你自己去网上搜索学习了,我在这里并不会讲解,Hibernate的注解了解一下就行!因为维护的时候会稍微麻烦一点。接下来,我要重点讲解Spring是怎样来整合Hibernate框架了。
首先,在Spring整合Hibernate框架的时候,是可以不用创建Hibernate的核心配置文件的,但我们还是要把Hibernate核心配置文件中的基本信息配置和映射文件的引入放到Spring配置文件中进行配置。所以,这时Spring配置文件的内容就应修改为下面这个样子。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!-- 引入外部属性文件 -->
  4. <context:property-placeholder location="classpath:jdbc.properties" />
  5. <!-- 配置C3P0连接池 -->
  6. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  7. <property name="driverClass" value="${jdbc.driverClass}" />
  8. <property name="jdbcUrl" value="${jdbc.url}" />
  9. <property name="user" value="${jdbc.username}" />
  10. <property name="password" value="${jdbc.password}" />
  11. </bean>
  12. <!-- 开启组件扫描,即将我们的类交给Spring去管理,直接扫描父包 -->
  13. <context:component-scan base-package="com.meimeixia.ssh" />
  14. <!-- Spring整合Hibernate -->
  15. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  16. <!-- 自己得注入一个连接池 -->
  17. <property name="dataSource" ref="dataSource" />
  18. <!-- 配置Hibernate的相关属性 -->
  19. <property name="hibernateProperties">
  20. <!-- Properties类型的属性,我们要怎么注入呢? -->
  21. <props>
  22. <!-- 配置Hibernate的方言 -->
  23. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  24. <!-- 打印sql语句 -->
  25. <prop key="hibernate.show_sql">true</prop>
  26. <!-- 格式化sql语句 -->
  27. <prop key="hibernate.format_sql">true</prop>
  28. <!-- 自动创建表 -->
  29. <prop key="hibernate.hbm2ddl.auto">update</prop>
  30. </props>
  31. </property>
  32. <!-- 加载映射(扫描的是包) -->
  33. <property name="packagesToScan" value="com.meimeixia.ssh.domain" />
  34. </bean>
  35. </beans>

然后,我们就要在dao层的实现类中使用Hibernate模板类来完成保存客户的操作了。这里一定要注意dao层中的实现类是不能继承Hibernate模板类的,因为不能使用注解的方式来完成属性注入。但是,我们又要在dao层的实现类中注入Hibernate模板类,这该怎么办呢?只能用老办法了,先在Spring配置文件中配置Hibernate模板类。

  1. <!-- 自己定义一个Hibernate的模板类 -->
  2. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
  3. <property name="sessionFactory" ref="sessionFactory"></property>
  4. </bean>

再使用注解注入HibernateTemplate类型的属性。

  1. package com.meimeixia.ssh.dao.impl;
  2. import javax.annotation.Resource;
  3. import org.springframework.orm.hibernate5.HibernateTemplate;
  4. import org.springframework.stereotype.Repository;
  5. import com.meimeixia.ssh.dao.CustomerDao;
  6. import com.meimeixia.ssh.domain.Customer;
  7. /** * 客户管理的dao的实现类 * @author liayun */
  8. @Repository("customerDao")
  9. public class CustomerDaoImpl /*extends HibernateDaoSupport*/ implements CustomerDao {
  10. //注入Hibernate模板
  11. @Resource(name="hibernateTemplate")
  12. private HibernateTemplate hibernateTemplate;
  13. @Override
  14. public void save(Customer customer) {
  15. System.out.println("CustomerDaoImpl类中的save方法执行了......");
  16. }
  17. }

接着,就可以在dao层的实现类中使用Hibernate模板类来完成保存客户的操作了。

  1. package com.meimeixia.ssh.dao.impl;
  2. import javax.annotation.Resource;
  3. import org.springframework.orm.hibernate5.HibernateTemplate;
  4. import org.springframework.stereotype.Repository;
  5. import com.meimeixia.ssh.dao.CustomerDao;
  6. import com.meimeixia.ssh.domain.Customer;
  7. /** * 客户管理的dao的实现类 * @author liayun */
  8. @Repository("customerDao")
  9. public class CustomerDaoImpl /*extends HibernateDaoSupport*/ implements CustomerDao {
  10. //注入Hibernate模板
  11. @Resource(name="hibernateTemplate")
  12. private HibernateTemplate hibernateTemplate;
  13. @Override
  14. public void save(Customer customer) {
  15. System.out.println("CustomerDaoImpl类中的save方法执行了......");
  16. //使用Hibernate的模板完成保存客户的操作
  17. this.hibernateTemplate.save(customer);
  18. }
  19. }

配置Spring的事务管理

这里,我们会使用Spring基于注解方式的声明式事务管理,因为写起来更加简单。

  • 第一步,配置事务管理器。
  1. <!-- 配置事务管理器 -->
  2. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  3. <!-- 它里面想要管理事务,还得获得连接,获得连接就需要SessionFactory -->
  4. <property name="sessionFactory" ref="sessionFactory" />
  5. </bean>
  • 第二步,开启事务注解。
  1. <!-- 开启注解(式)事务 -->
  2. <tx:annotation-driven transaction-manager="transactionManager" />
  • 第三步,在业务层中的实现类上添加@Transactional注解。
  1. package com.meimeixia.ssh.service.impl;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import com.meimeixia.ssh.dao.CustomerDao;
  6. import com.meimeixia.ssh.domain.Customer;
  7. import com.meimeixia.ssh.service.CustomerService;
  8. /** * 客户管理的service的实现类 * @author liayun */
  9. @Service("customerService")
  10. @Transactional
  11. public class CustomerServiceImpl implements CustomerService {
  12. //注入dao
  13. @Resource(name="customerDao")
  14. private CustomerDao customerDao;
  15. @Override
  16. public void save(Customer customer) {
  17. System.out.println("CustomerServiceImpl类中的save方法执行了......");
  18. customerDao.save(customer);
  19. }
  20. }

至此,SSH框架整合的第三种方式,算是讲完了,真不容易!为了证明这一点,你可以做一个测试,发布咱们的项目到Tomcat服务器中,访问项目的首页之后,点击新增客户超链接,这时会跳转到客户添加页面,填入客户的相关信息之后,点击保存按钮,那么你就会看到咱的客户表中添加了一条新的记录。

配置页面的跳转

你有没有想过一个问题,那就是当我们保存客户成功之后,应该是要跳转到客户列表显示页面的,但现在显然不是这样,保存客户就完了。所以,我们还需要配置一下页面的跳转,就像下面这样。

  1. //保存客户的方法 //这儿是页面跳转
  2. @Action(value="customer_save", results={@Result(name="success", location="/login.jsp")})
  3. public String save() {
  4. System.out.println("CustomerAction中的save方法执行了......");
  5. customerService.save(customer);
  6. return SUCCESS;
  7. }

一旦像上面这样修改了Action中的save方法后,成功保存客户之后,就会跳转到用户登录页面了。

相关文章