MyBatis-plus 配置

mybatis-plus是在mybatis得基础上进行得集成,是的crud操作更加简便,减少了大量得sql语句得编写。

提升:

1. 不用写大量得sql语句

2. 不用每个实体类都需要一个mapper.xml

3. 减少了配置得繁琐性

4. 简化了很多操作,查询分页等比较容易

配置

多路径配置

spring:
  datasource:
    dynamic:
      primary: demo #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        demo:
          url: jdbc:mysql://localhost:3306/fdi-demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: *********
        plan:
          url: jdbc:mysql://localhost:3306/fdi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: *******
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
          enabled: true
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: admin
        allow:
        #deny: 192.168.1.100

使用

1. 使用QueryWrapper进行sql语句编写

 QueryWrapper<DemoComment> demoCommentQueryWrapper = new QueryWrapper<>();
 demoCommentQueryWrapper.eq("comment_id", commentId);
 DemoComment comment  = demoCommentMapper.selectOne(demoCommentQueryWrapper);

2. 使用mybatis得方式编写xml sql语句(用于复杂sql)

注意

多数据源得使用,针对不同得mapper,使用@DB("***")注解以标志所使用得数据库是哪个

 

posted @ 2022-10-11 17:07  100多斤派大星  阅读(762)  评论(0)    收藏  举报