SpringBoot2.0学习笔记:(九) Spring Boot中集成Mybatis与Druid

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/liujun03/article/details/82829385

一、项目的搭建

Druid对Spring boot做了很好的适配,所有的工作都只需要在配置文件中完成。

具体的Druid在Spring Boot中的配置可以看:GitHub文档

首先看一下项目引入的jar包:

<dependencies>
    <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>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

在Spring Boot项目使用Druid需要导入的jar包是druid-spring-boot-starter

很简单,项目只需要导入

mybatis-spring-boot-starter

mysql-connector-java

druid-spring-boot-starter

spring-boot-starter-web

这四个jar包就可以了。

之后看一下项目目录结构:

在这里插入图片描述

我使用的是yml格式的配置文件,因为配置太多了,这样写简单一点=-=。

controller、model、dao、这三个包内的Java文件勿用多说,说一下resoucres目录下的mybatis文件夹,这是我定义的Mybatis的映射文件所处的位置,这个路径需要在配置文件中配置一下。

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

现在具体看一下application.yml文件:

spring:
  datasource:
    druid:
      # JDBC配置
      url: jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=UTF-8
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver

      # 连接池配置
      # 配置初始化大小、最小、最大
      initial-size: 5
      max-active: 20
      min-idle: 5
      # 获取连接等待超时时间
      max-wait: 60000
      pool-prepared-statements: true
      #
      validation-query: SELECT 1
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      # 间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      # 一个连接在连接池中最小的生存时间
      min-evictable-idle-time-millis: 300000
      max-pool-prepared-statement-per-connection-size: 20
      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true
      filters: stat
      filter:
      # 慢SQL记录(sql语句执行时间超过某个界限就记录下来)
        stat:
          slow-sql-millis: 200
          log-slow-sql: true

      # 监控配置
      # Druid WebStatFilter配置
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: /druid/*,*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico
      # Druid StatViewServlet配置
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        # 登录监控界面的用户名以及密码,登录url为localhost:8080/druid
        login-username: admin
        login-password: admin
        allow:
        deny:
mybatis:
  # 指定mybatis映射文件的位置
  mapper-locations: classpath:mybatis/*.xml
  configuration:
  	# 开启驼峰命名转换
    map-underscore-to-camel-case: true

这就是全部的配置了,其中有关于Druid的配置可以参照上面给出的超链接去GitHub上详细了解。

最后还不能忘了的是,在启动文件SpringBootDruidApplication中加入@MapperScan注解,指定要扫描的Mapper类的路径。


@MapperScan(value = "com.baiding.springboot.dao")
@SpringBootApplication
public class SpringBootDruidApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDruidApplication.class, args);
	}
}

以上就是SpringBoot2.0.x中关于Mybatis与Druid的全部配置了,当然这只是很简陋的配置,具体的扩展可以参看官方文档。下面看一下效果:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这里的Druid监控界面访问:localhost:8080/druid就可以

下面是Druid的SQL监控:

在这里插入图片描述

下面是Druid的URI监控:

在这里插入图片描述

问题解惑

1. Mybatis查出了数据,但无法完成赋值

比如,你在数据库中查出了一条数据赋值给对象,对象的id成功赋值了,但是他的userName是个空值。这里就是驼峰命名 的坑了,当你在Mybatis映射文件中select语句直接使用resultType而不是用resultMap返回数据,并且对象中采用驼峰命名(userName)数据库中使用user_name时,就会出现这个 情况:

<select id="getUser" parameterType="long" 				 resultType="com.baiding.springboot.model.User">
    SELECT * FROM user WHERE id = #{id}
</select>

所以解决办法就是在配置文件中开启驼峰命名:

mybatis:
  configuration:
    map-underscore-to-camel-case: true

2. Druid连接Mysql数据库一直提示密码错误

我的mysql数据库的密码一开始是一串纯数字,例如:033453,但是启动项目的时候一直报密码错误,之后我跟踪了一下配置文件中数据库连接属性的赋值过程,跟踪到DruidAbstractDataSourcesetPassword方法,

public void setPassword(String password) {
    if (StringUtils.equals(this.password, password)) {
        return;
    }

    if (inited) {
        LOG.info("password changed");
    }

    this.password = password;
}

发现在这里得到的password已经变成了另外一串数字了。很疑惑,但不明白具体原因,希望有大手子可以告知。

之后,我改成了连接虚拟机上另外的 一个数据库,密码也改为了字符串,这样启动就没问题了。

最后附上项目地址:spring-boot-druid

猜你喜欢

转载自blog.csdn.net/liujun03/article/details/82829385