本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置。

1. pom.xml文件配置

需要在dependencies节点添加:

<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!-- MySQL end --> <!-- Connection Pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- Connection Pool end --> <!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MyBatis end -->

2. 因为是单数据源,为了方便起见,我们把数据源和连接池全部放到application.yml里,当然数据源的配置也可以另起一个新文件。

 server:
port: spring:
datasource:
name: devutility-test-database-mybatis-springboot
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${DB_URL:jdbc:mysql://127.0.0.1:3306/Test}?useUnicode=true&useSSL=false
username: ${DB_UID:tester}
password: ${DB_PWD:tester}
initial-size:
min-idle:
max-active:
max-wait:
time-between-eviction-runs-millis:
min-evictable-idle-time-millis:
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size:
filters: stat
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: devutility.test.database.mybatis.springboot.entities

第27行定义了mapper的xml文件的地址;

第28行定义了实体类的包,在mapper的xml文件中可以只写实体类名而不必写全实体类的命名空间。

3. MyBatis相关配置

MyBatis的配置主要涉及到mapper的xml文件,mapper的接口文件,以及数据库相关的实体类的定义。

(1) 首先定义一个客户实体类:

package devutility.test.database.mybatis.springboot.entities;

public class Customer extends BaseEntity {
private long id;
private String name1;
private String address1;
private String city;
private int state;
private int zip;
private String phone;
private String email;

(2) 定义mapper的xml文件,也叫XML映射文件,存放于src/main/resources/mappers目录下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="devutility.test.database.mybatis.springboot.mappers.CustomerMapper">
<select id="get" resultType="Customer">
select * from Customer where id = #{id};
</select>
</mapper>

XML映射文件的定义规则可以参考MyBatis官网,此处我们仅实现一个根据主键查询Customer的功能。

(3) 定义Mapper接口:

package devutility.test.database.mybatis.springboot.mappers;

import org.apache.ibatis.annotations.Mapper;

import devutility.test.database.mybatis.springboot.entities.Customer;

@Mapper
public interface CustomerMapper {
Customer get(long id);
}

4. 应用

package devutility.test.database.mybatis.springboot.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import devutility.test.database.mybatis.springboot.entities.Customer;
import devutility.test.database.mybatis.springboot.mappers.CustomerMapper; @RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerMapper customerMapper; @RequestMapping("/get")
public Customer get(long id) {
return customerMapper.get(id);
}
}

Demo地址

Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid的更多相关文章

  1. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  2. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  3. Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!

    前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...

  4. Spring Boot 入门系列(二十三)整合Mybatis,实现多数据源配置!

    d之前介绍了Spring Boot 整合mybatis 使用注解方式配置的方式实现增删改查以及一些复杂自定义的sql 语句 .想必大家对spring boot 项目中,如何使用mybatis 有了一定 ...

  5. Spring Data JPA系列4——Spring声明式数事务处理与多数据源支持

    大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第四篇了,先来回顾下前面三篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...

  6. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  7. Spring Boot 应用系列 4 -- Spring Boot 2 整合log4j2

    一.背景 1. log4j2传承于log4j和logback,它是目前性能最好的日志处理工具,有关它们的性能对比请看: 2. 除了性能好之外,log4j2有这么几个重要的新features: (1) ...

  8. Spring Boot 应用系列 3 -- Spring Boot 2 整合MyBatis和Druid,多数据源

    本文演示多数据源(MySQL+SQL Server)的配置,并且我引入了分页插件pagehelper. 1. 项目结构 (1)db.properties存储数据源和连接池配置. (2)两个数据源的ma ...

  9. Spring Boot 2.x整合mybatis及druid数据源及逆向工程

    1逆向工程 1)db.properties #============================# #===== Database sttings =====# #=============== ...

  10. Spring Boot2 系列教程(二十二)整合 MyBatis 多数据源

    关于多数据源的配置,前面和大伙介绍过 JdbcTemplate 多数据源配置,那个比较简单,本文来和大伙说说 MyBatis 多数据源的配置. 其实关于多数据源,我的态度还是和之前一样,复杂的就直接上 ...

随机推荐

  1. sublimetext 2 编译文件带input时 提示 EOFError: EOF when reading a line

    昨天在网下下载了个什么sublimetxt 2 的破解版,然后让我折腾了半天,没错 ,就是因为这个 EOFError: EOF when reading a line错误让我搞的半死.怨自己,贪图中文 ...

  2. 常用修图工具的一些使用技巧及问题解决方法——ai

    一.ai如何修改画布大小 一. ai如何修改画布大小: 1. 左上角菜单中的文件——文档设置(也可以直接点菜单栏下边的控制栏中的文档设置) 2. 文档设置界面中,点击右上角“编辑画板“ 3. 此时面板 ...

  3. 12-ssm中的description The request sent by the client was syntactically incorrect.

    此问题一般是在前端的数据传回是封装成对象失败的情况: 1.对象名不一致: 2.对象的数据类型不一致: 特别注意日期类型的: 如果前端是date数据类型的话: 传入的日期有问题 在pojo类中限定 @D ...

  4. WPF之数据触发器 改变控件背景色或闪烁

    需求,很多矩形表示桶,其中:空桶=红色,满桶=绿色,使用中=红绿闪烁. <Window x:Class="FlickerDemo.MainWindow" xmlns=&quo ...

  5. 品味性能之道<二>:性能工程师可以具备的专业素养

          性能工程师可以具备的专业素养 程序语言原理,包括:C.C++.java及jvm.ASP,因为建站大部分外围应用和中间件都是JAVA编写,大部分的电商平台采用的ASP编写,底层核心系统是C/ ...

  6. python性能监控初试

    标 题: python性能监控初试作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990765.html 欢迎转帖 请保持文本完整并注明出处 之前性能统计 ...

  7. com.opensymphony.xwork2.config.ConfigurationManager.addConfigurationProvider

    一月 31, 2016 5:06:31 下午 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting f ...

  8. 482. License Key Formatting

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 2018.09.28 牛客网contest/197/B面积并(二分+简单计算几何)

    传送门 比赛的时候把题目看成求面积交了,一直没调出来. 下来发现是面积并气的吐血. 码了一波发现要开long double. 然而直接用现成的三角函数会挂. 因此需要自己手写二分求角度. 大致思路就是 ...

  10. 2018.09.22 atcoder Integers on a Tree(构造)

    传送门 先考虑什么时候不合法. 第一是考虑任意两个特殊点的权值的奇偶性是否满足条件. 第二是考虑每个点的取值范围是否合法. 如果上述条件都满足的话就可以随便构造出一组解. 代码: #include&l ...