整合SSM-事务

新建service接口:ProductService.java

package com.liuxi.service;

import java.util.List;

import com.liuxi.pojo.Product;

public interface ProductService {

	// 查询所有数据
	List<Product> selectAll() throws Exception;

	Product selectOne(int pid) throws Exception;

	Integer update(Product product) throws Exception;

	int deleteOne(int pid) throws Exception;

	Integer insert(Product product) throws Exception;
}

实现类:ProductServiceImpl.java

package com.liuxi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuxi.mapper.ProductMapper;
import com.liuxi.pojo.Product;
import com.liuxi.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {
	// 事务回滚,出现异常
	@Autowired
	private ProductMapper productMapper;

	@Override
	public List<Product> selectAll() throws Exception {
		return productMapper.selectAll();
	}

	@Override
	public Product selectOne(int pid) throws Exception {
		return productMapper.selectOne(pid);
	}

	@Override
	public Integer update(Product product) throws Exception {
		return productMapper.update(product);
	}

	@Override
	public int deleteOne(int pid) throws Exception {
		return productMapper.deleteOne(pid);
	}

	@Override
	public Integer insert(Product product) throws Exception {

		// productMapper.insert(product);
		// productMapper.insert(product);
		// throw new Exception("手动抛出异常");
		// productMapper.insert(product);

		return productMapper.insert(product);
	}

}

controller:

package com.liuxi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liuxi.pojo.Product;
import com.liuxi.service.ProductService;

@Controller
@RequestMapping("/product")
public class IndexController {

	/**
	 * controller 调用service、service层调用mapper
	 * 
	 */

	@Autowired
	private ProductService productService;

	@RequestMapping("list")
	private String list(ModelMap map, String msg) throws Exception {
		List<Product> list = productService.selectAll();
		map.put("list", list);
		map.put("msg", msg);
		return "list";
	}

	@RequestMapping("toInsert")
	private String toInsert() {
		return "edit";
	}

	// 增删改
	@RequestMapping("insert")
	private String insert(Product product, ModelMap map) throws Exception { // spring mvc会自动封装接收参数的对象。
		String msg = "";
		if (productService.insert(product) > 0) {
			msg = "新增成功!";
		} else {
			msg = "新增失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}

	// 修改
	@RequestMapping("toUpdate")
	private String toUpdate(Integer pid, ModelMap map) throws Exception {
		Product product = productService.selectOne(pid);
		map.put("product", product);
		return "edit";
	}

	@RequestMapping("update")
	private String update(Product product, ModelMap map) throws Exception {
		String msg = "";
		if (productService.update(product) > 0) {
			msg = "修改成功!";
		} else {
			msg = "修改失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}

	@RequestMapping("delete")
	private String delete(Integer pid, ModelMap map) throws Exception {
		String msg = "";
		if (productService.deleteOne(pid) > 0) {
			msg = "删除成功!";
		} else {
			msg = "删除失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}

}

spring-mybatis.xml中的事务对象修改包内容:

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
			<tx:method name="batch*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<!-- 事务处理 -->
	<aop:config>
		<aop:pointcut id="pc" expression="execution(* com.liuxi.service..*(..))" />
		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
	</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值