SpringBot连接Mysql

一、SpringBoot集成Mybatis连接Mysql数据库配置

1、引入依赖

<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

properties版

2、增加配置

###连接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/kang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true 
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
spring.jpa.database=MYSQL

##配置Mapper*.xml位置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
##配置实体类位置,设置别名
mybatis.type-aliases-package=com.kang.entity
##驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true


###配置分页插件
pagehelper.helperDialec=mysql
pagehelper.reasonable: true
pagehelper.supportMethodsArguments: true
pagehelper.params: count=countSql

###打印SQL
logging.level.[com.kang.dao]=debug   --[包名]

###日志输出位置     
response.handler.enabled=true
logging.file=/usr/local/tomcat/logs/user.info
logging.level.org.springframework.web=debug

yml版

2、增加配置

###连接数据库
spring: 
    datasource: 
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/kang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true 
        username: root
        password: root
    jpa:
        properties:
          hibernate:
            hbm2ddl: 
              auto: update
        show-sql: true
        database: mysql

##Mybatis配置    
mybatis:
  type-aliases-package: com.kang.entity                   ##定义所有操作类的别名所在包
  mapper-locations:                                     ##所有的mapper映射文件
    - classpath:mybatis/mapper/*.xml
  configuration: 
    map-underscore-to-camel-case: true                    ##驼峰命名

###配置分页插件
pagehelper: 
    helperDialec: mysql
    reasonable: true
    supportMethodsArguments: true
    params: 
        count: countSql

###打印SQL
logging:
  level:
     com.kang.dao : debug

###日志输出位置     
response: 
    handler: 
        enabled: true
logging: 
    file: /usr/local/tomcat/logs/user.info
    level: 
        org.springframework.web: debug

二、SpringBoot集成Mybatis-plus连接Mysql数据库配置

1、引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

properties版

2、增加配置

###连接数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/kang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true 
spring.datasource.username=root
spring.datasource.password=root

###mybatis-plus相关配置
##项目Mapper.xml存放位置,classpath:*****,表示项目resources文件夹下
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml
##实体类存放位置
mybatis-plus.type-aliases-package=com.kang.entity
##驼峰下划线转换
mybatis-plus.global-config.db-column-underline=true
##刷新mapper 调试神器
mybatis-plus.global-config.refresh-mapper=true
##数据库大写下划线转换
##mybatis-plus.global-config.capital-mode=true
##序列接口实现类配置
##mybatis-plus.global-config.key-generator=com.baomidou.springboot.xxx
##逻辑删除配置
mybatis-plus.global-config.logic-delete-value=0
mybatis-plus.global-config.logic-not-delete-value=1
mybatis-plus.global-config.sql-injector=com.baomidou.mybatisplus.mapper.LogicSqlInjector
##驼峰命名
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
## 数据库类型设置
mybatis-plus.global-config.db-config.db-type=mysql
mybatis-plus.global-config.db-config.column-underline=true
mybatis-plus.global-config.db-config.logic-delete-value=true
mybatis-plus.global-config.db-config.logic-not-delete-value=false

yml版

2、增加配置

###连接数据库
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/activiti?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
  jpa:
    properties:
      hibernate:
        hbm2ddl: 
          auto: update
    show-sql: true
    database: mysql

###mybatis-plus相关配置
##配置Mapper*.xml位置
mybatis-plus:
    type-aliases-package: com.kang.entity
    mapper-locations: classpath:mapper/*.xml
    #此项设置均为默认值[可不设置]
    global-config: 
        db-config: 
            ##主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
            id-type: auto
            ##字段策略 IGNORED:"忽略判断"  NOT_NULL:"非 NULL 判断")  NOT_EMPTY:"非空判断"
            field-strategy: NOT_EMPTY
            ##数据库类型
            db-type: MYSQL
    configuration: 
        ##是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
        map-underscore-to-camel-case: true
        ##如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
        call-setters-on-nulls: true
        ##这个配置会将执行的sql打印出来,在开发或测试的时候可以用
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

###日志输出位置     
response: 
    handler: 
        enabled: true
logging: 
    file: /usr/local/tomcat/logs/user.info
    level: 
        org.springframework.web: debug

三、配置阿里数据库连接池

1、引入依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

Mybatis版

2、增加配置

###阿里连接池配置
spring:
  datasource:
    druid:
      username: root
      password: 123456
      url: jdbc:mysql://127.0.0.1:3306/kang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true 
      driver-class-name: com.mysql.jdbc.Driver
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

3、添加配置类
mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
            <setting name="useGeneratedKeys" value="true"/>
            <setting name="useColumnLabel" value="true"/>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

Mybatis-plus版

2、增加配置

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/kang?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true 
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,slf4j
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.152.12
spring.datasource.druid.stat-view-servlet.deny=192.168.1.73
spring.datasource.druid.stat-view-servlet.reset-enable=false
##Druid 管理账号
spring.datasource.druid.stat-view-servlet.login-username=kang
##Druid 管理密码
spring.datasource.druid.stat-view-servlet.login-password=kang
##com.simple.spring.boot.mapper 该包打印DEBUG级别日志
logging.level.com.kang.mapper=debug

##mybatis plus mapper文件路径
mybatis-plus.mapperLocations=classpath:/mybatis/mapper/*.xml
##mybaits plus 实体类路径
mybatis-plus.typeAliasesPackage=com.kang.entity
mybatis-plus.typeEnumsPackage=
##数据库相关配置
##主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
mybatis-plus.global-config.db-config.id-type=UUID
##字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
mybatis-plus.global-config.db-config.field-strategy=not_empty
#驼峰下划线转换
mybatis-plus.global-config.db-config.column-underline=true
##数据库大写下划线转换
##capital-mode: true
##逻辑删除配置
mybatis-plus.global-config.db-config.logic-delete-value=0
mybatis-plus.global-config.db-config.logic-not-delete-value= 1
##mybatis-plus.global-config.db-config.db-type= sqlserver
##刷新mapper 调试神器
mybatis-plus.global-config.refresh=true
##原生配置
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
mybatis-plus.configuration.call-setters-on-nulls =true

   转载规则


《SpringBot连接Mysql》 kang 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录