Spring Boot + Mybatis @UpdateProvider注解的使用

@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值,并返回数据

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot + MyBatis可以通过注解和自动配置来简化SSM框架的配置,具体步骤如下: 1. 引入依赖:在pom.xml文件中添加Spring BootMyBatis的依赖,例如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> ``` 2. 配置数据源:在application.properties或application.yml文件中配置数据源,例如: ``` spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root ``` 3. 配置MyBatis:在启动类中添加@MapperScan注解,指定Mapper接口所在的包路径,例如: ``` @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 编写Mapper接口和Mapper XML文件:在Mapper接口中添加@Mapper注解,编写SQL语句,在Mapper XML文件中配置SQL语句和结果映射,例如: ``` @Mapper public interface UserMapper { @Select("select * from user") List<User> findAll(); } ``` ``` <select id="findAll" resultType="com.example.demo.pojo.User"> select * from user </select> ``` 通过以上配置,就可以实现Spring Boot + MyBatis的简化SSM框架。在以上配置中,Spring Boot提供了自动配置和默认配置,可以省去很多繁琐的配置工作,同时还可以提高开发效率和代码可读性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值