在之前的基础上新增修改表book的introduction字段的方法
package lang.java.springbootdemo.dao;
import lang.java.springbootdemo.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BookDao {
List getBookByWriter(String writer);
int updateIntroductionById(int id, String introduction);
}
package lang.java.springbootdemo.service;
import lang.java.springbootdemo.entity.Book;
import java.util.List;
public interface BookService {
List getBookByWriter(String writer);
int updateIntroductionById(int id, String introduction);
}
package lang.java.springbootdemo.service.impl;
import lang.java.springbootdemo.dao.BookDao;
import lang.java.springbootdemo.entity.Book;
import lang.java.springbootdemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public List getBookByWriter(String writer) {
return bookDao.getBookByWriter(writer);
}
@Override
public int updateIntroductionById(int id, String introduction){
return bookDao.updateIntroductionById(id, introduction);
}
}
package lang.java.springbootdemo.controller;
import lang.java.springbootdemo.entity.Book;
import lang.java.springbootdemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@EnableAutoConfiguration
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("/listBooks")
String listBooks(HttpServletRequest request, HttpServletResponse response) {
StringBuilder s = new StringBuilder();
for (Book book : bookService.getBookByWriter(request.getParameter("writer"))) {
s.append(book.getName()).append(";");
}
return s.toString();
}
@RequestMapping("/updateIntroductionById")
int updateIntroductionById(HttpServletRequest request, HttpServletResponse response) {
StringBuilder s = new StringBuilder();
for (Book book : bookService.getBookByWriter(request.getParameter("writer"))) {
s.append(book.getName()).append(";");
}
return bookService.updateIntroductionById(Integer.parseInt(request.getParameter("id")), request.getParameter("introduction"));
}
}
实测可以修改记录,但这时还不支持事务,修改代码,虽然方法内部报错,但是修改依然有效!
package lang.java.springbootdemo.service.impl;
import lang.java.springbootdemo.dao.BookDao;
import lang.java.springbootdemo.entity.Book;
import lang.java.springbootdemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public List getBookByWriter(String writer) {
return bookDao.getBookByWriter(writer);
}
@Override
public int updateIntroductionById(int id, String introduction){
int i = bookDao.updateIntroductionById(id, introduction);
i=1/0;
return i;
}
}
开启事务支持,只需要在对应的方法上添加一行注解,再次测试,记录修改失败,证明事务配置生效。
package lang.java.springbootdemo.service.impl;
import lang.java.springbootdemo.dao.BookDao;
import lang.java.springbootdemo.entity.Book;
import lang.java.springbootdemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public List getBookByWriter(String writer) {
return bookDao.getBookByWriter(writer);
}
@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public int updateIntroductionById(int id, String introduction){
int i = bookDao.updateIntroductionById(id, introduction);
i=1/0;
return i;
}
}