springboot的事务管理的一个完整demo
事务注解@Transactional 可以标注到mapper的dao接口上,或者service的接口上。两者均可。
首先建立一个springboot的项目,
引入pom,需要注意的是,tomcat包,aop包,druid数据源包,jdbc包,mybatis包,servlet,jsp,jstl包
<!-- 手动添加 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <!-- <version>2.4.2</version> --> </dependency> <!--/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!--servlet依赖的jar包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!--jsp依赖的jar包 --> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
书写application.yml
注意配置,配置了jsp后缀后,就可以找到默认index.jsp主页了。
mapper的包含,需要配置目录,否则不会加载mapp.xml 文件。另外还要在启动文件上,标注@MapperScan(basePackages = {"cn.taotao.dao"}),指明mapper找的哪个接口文件。
spring: mvc: view: suffix: .jsp #上面这行配置后,就可以把index.jsp 作为默认首页了。 # prefix: /views/ datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 123456 initial-size: 1 min-idle: 1 max-active: 50 max-wait: 15000 enable: true test-on-borrow: true test-on-return: true test-while-idle: true mybatis: mapper-locations: - classpath:mapper/*Mapper.xml #这个需要指出,否则不会加载mapper.xml文件,或者把生成的文件,写入到和类同级别的目录中(不建议)。 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
编写UserService接口文件,这个接口可以标注@Transactional
package cn.taotao.service; import java.util.List; import org.springframework.transaction.annotation.Transactional; import cn.taotao.bean.User; public interface UserService { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); List<User> queryAll(); }
编写UserServiceImpl文件,需要标注@Service
package cn.taotao.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.taotao.bean.User; import cn.taotao.dao.UserMapper; import cn.taotao.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int deleteByPrimaryKey(Integer id) { // TODO Auto-generated method stub return this.userMapper.deleteByPrimaryKey(id); } @Override public int insert(User record) { // TODO Auto-generated method stub return 0; } @Override public int insertSelective(User record) { // TODO Auto-generated method stub return 0; } @Override public User selectByPrimaryKey(Integer id) { // TODO Auto-generated method stub return null; } @Override public int updateByPrimaryKeySelective(User record) { // TODO Auto-generated method stub return 0; } @Override public int updateByPrimaryKey(User record) { // TODO Auto-generated method stub return 0; } @Override public List<User> queryAll() { // TODO Auto-generated method stub return this.userMapper.queryAll(); //return null; } }
另外,在UserMapper接口文件,也可以标注@Transactional
package cn.taotao.dao; import java.util.List; import org.springframework.transaction.annotation.Transactional; import cn.taotao.bean.User; @Transactional public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); List<User> queryAll(); }
编写controller文件
package cn.taotao.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import cn.taotao.bean.User; import cn.taotao.service.UserService; import cn.taotao.service.impl.UserServiceImpl; @Controller public class IndexController { @Autowired private UserServiceImpl userService; @RequestMapping(value= {"/","/index"}) public ModelAndView getIndex() { ModelAndView mv = new ModelAndView("index"); List<User> queryAll = userService.queryAll(); // User selectByPrimaryKey = this.userService.selectByPrimaryKey(1); mv.addObject("users", queryAll); return mv; } }
建立 src/main/webapp目录,这个目录不会自动创建,需要手工创建
建立index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> index.jsp <a href="/addUser">添加用户</a> <a href="/updateUser">修改用户</a> <a href="/deleteUser">delete</a> <hr/> <c:forEach var="u" items="${users }"> <li>un:${u.username}</li> <li>pw:${u.password }</li> <li>addr:${u.address }</li> <li><fmt:formatDate value="${u.birthday }" pattern="yyyy-MM-dd"/></li> <li>dept: ${u.dept }</li> <hr/> </c:forEach> </body> </html>