@UpdateProvider实现对数据库的更新
@UpdateProvider(type = BookController.class,method = "update1")
public int update1(Book book);
type:要写SQL语句的类,method:方法名
实现类中的方法:
public String update1(Book book) {
return new SQL() {
{
UPDATE("book");
if (book.getNumber() != null) {
SET("number=#{number}");
}
if (book.getBookName() != null) {
SET("bookName=#{bookName}");
}
if (book.getAuthorName() != null) {
SET("authorName=#{authorName}");
}
if (book.getType() != null) {
SET("type=#{type}");
}
if (book.getPrice() != null) {
SET("price=#{price}");
}
WHERE("id=#{id}");
}
}.toString();
}
方法的简单使用如下:
//调用动态更新的方法(bookMapper.update1(book);)
@ResponseBody
@PatchMapping("/up/{id}")
public Book showUpdate(Book book, @PathVariable("id") Integer id) {
bookMapper.update1(book);
return bookMapper.getBookById(id);
}
结果如下:
只更新此内容的number值,并返回数据