新建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>