Mybatis-Plus配置Oracle和MySQL

准备工作

1、导包

    <dependencies>
        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis-plus-->
        <!--mybatis-plus是自己开发的,并非官方的-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <!--非官方: mybatis-spring-boot-starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        

        <!--Druid会使用log4j的日志记录,使用导入依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>

        <!--JDBC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--MySQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!--oracle-->
<!--        <dependency>-->
<!--            <groupId>com.oracle</groupId>-->
<!--            <artifactId>ojdbc6</artifactId>-->
<!--            <version>11.2.0.1.0</version>-->
<!--        </dependency>-->

        <!--mybatis-plus-generator 生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.2</version>
        </dependency>

        <!-- freemarker,作为代码生成器mapper文件的模板引擎使用(当然也可以使用velocity,二选一即可) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!--  约定大于配置,我们不要让Spring启动的时候过滤掉xml文件    -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>项目名</finalName>
    </build>

2、全部的配置文件(配置JDBC、Druid、MP)

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=true&allowMultiQueries=true

    # Oracle配置
    #url: jdbc:oracle:thin:@127.0.0.1:1521:test
    #username: root
    #password: 123456
    #driver-class-name: oracle.jdbc.driver.OracleDriver

    #    配置druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    # druid 配置
    druid:
      #    打开我们的内置监控页面,打开后我们才可以有相应的web界面
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: user
        login-password: 123456
      #        打开我们的内置监控功能,打开之后我们才可以对我们的操作进行统计
      filter:
        stat:
          enabled: true
          slow-sql-millis: 2000
        #      配置防火墙,防止sql语句注入,我们可以设置我们想要那些语句可以执行,哪些不行
        wall:
          enabled: true
          config:
            delete-allow: true
      #            select-all-column-allow: false,如果我们设置了拒绝查询,那么我们查询时就会报错,返回一个whitePage
      #            selelct-allow: false
      #        打开我们的Web关联监控配置,即我们对某些路径进行精确统计
      web-stat-filter:
        enabled: true
      # 初始化时建立的物理连接数。初始化发生在显式调用init方法,或者第一次getConnection时.
      initial-size: 5
      # 连接池最大物理连接数量。
      max-active: 50
      # 连接池最小物理连接数量。
      min-idle: 5
      # 获取连接时最大等待时间,单位为毫秒。
      # 配置之后,缺省启用公平锁,并发效率会有所下降,若需要可以通过配置useUnfairLock属性为true使用非公平锁。
      max-wait: 6000
      # 是否缓存preparedStatement,也就是PSCache。
      # PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
      pool-prepared-statements: true
      # 要启用PSCache,其值必须大于0,当大于0时,poolPreparedStatements自动触发修改为true。
      # 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100。
      max-pool-prepared-statement-per-connection-size: 20
      # 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
      # 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
      validation-query: select 1 from dual
      # 检测连接是否有效的超时时间,单位为秒。
      # 底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法。
      #      validation-query-timeout: 30
      # 有两个含义:
      #  1) Destroy线程会检测连接的间隔时间,若连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。
      #  2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明。
      time-between-eviction-runs-millis: 60000
      # 连接保持空闲而不被驱逐的最长时间。
      min-evictable-idle-time-millis: 300000
      # 建议配置为true,不影响性能,并且保证安全性。
      # 申请连接的时候检测,若空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
      test-while-idle: true
      # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
      test-on-borrow: false
      # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
      test-on-return: false
      # 类型是字符串,通过别名的方式配置扩展的拦截器插件,常用的拦截器插件有:
      # 监控统计用的filter:stat,日志用的filter:log4j,防御sql注入攻击的filter:wall,三个同时配置的化,用逗号隔开。
      # 注意,Druid中的filter-class-names配置项是不起作用的,必须采用filters配置项才可以。
      filters: stat,wall,log4j2
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录。
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true


# 整合mybatis 下面使用MP也可以配置映射的文件
# mybatis:
  # mapper-locations:
    # - classpath:com/zhao/mapper/*.xml

mybatis-plus:
  configuration:
    # 更新字段的时候设置为null,忽略实体null判断之后,解决Oracle无效的列异常, oracle数据库必须配置
    jdbc-type-for-null: 'null'  # 注意要有单引号
    # mybatis-plus驼峰映射
    map-underscore-to-camel-case: true
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # mybatis的xml文件地址,如果启动文件找不到xml文件,如下去修改pom.xml文件
  mapper-locations: classpath:mybatis/mapper/*.xml
  global-config:
    # 禁用mybatis-plus的LOGO
    banner: false
    db-config:
      # 逻辑未删除值,(逻辑删除下有效)
      logic-delete-value: 1
      # 逻辑未删除值,(逻辑删除下有效)需要注入逻辑策略LogicSqlInjector,以@Bean方式注入
      logic-not-delete-value: 0
      # 对应实体类的字段,写了这个在实体类中就不需要写注解了
      logic-delete-field: deleted

使用YAML配置MP

OracleMySQL通用

# 整合mybatis 下面使用MP也可以配置映射的文件
# mybatis:
  # mapper-locations:
    # - classpath:com/zhao/mapper/*.xml

mybatis-plus:
  configuration:
    # 更新字段的时候设置为null,忽略实体null判断之后,解决Oracle无效的列异常, oracle数据库必须配置
    jdbc-type-for-null: 'null'  # 注意要有单引号
    # mybatis-plus驼峰映射
    map-underscore-to-camel-case: true
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # mybatis的xml文件地址,如果启动文件找不到xml文件,如下去修改pom.xml文件
  mapper-locations: classpath:mybatis/mapper/*.xml
  global-config:
    # 禁用mybatis-plus的LOGO
    banner: false
    db-config:
      # 逻辑未删除值,(逻辑删除下有效)
      logic-delete-value: 1
      # 逻辑未删除值,(逻辑删除下有效)需要注入逻辑策略LogicSqlInjector,以@Bean方式注入
      logic-not-delete-value: 0
      # 对应实体类的字段,写了这个在实体类中就不需要写注解了
      logic-delete-field: deleted

使用Properties配置MP

  # mybatis的xml文件地址,如果启动文件找不到xml文件,如下去修改pom.xml文件
mybatis-plus.mapper-locations= classpath:mybatis/mapper/*.xml
    # 禁用mybatis-plus的LOGO
mybatis-plus.global-config.banner=false
    # mybatis-plus驼峰映射
mybatis-plus.configuration.map-underscore-to-camel-case = true
    # 更新字段的时候设置为null,忽略实体null判断之后,解决Oracle无效的列异常, oracle数据库必须配置,没有引号
mybatis-plus.configuration.jdbc-type-for-null=null
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

pom.xml解决找不到xml文件的配置

<build>
        <!--  约定大于配置,我们不要让Spring启动的时候过滤掉xml文件    -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>

猜你喜欢

转载自blog.csdn.net/qq_57581439/article/details/130767476