如果不需要连接池,那么只需要简单的在pom文件中,添加mysql依赖:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>xxx.xxx.xxx</version>
</dependency>

然后在配置文件中添加配置:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://hostname:port/xxxxxx?autoReconnect=true&failOverReadOnly=false&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxx

MySQL datasource 就配置完了。

如果使用连接池,则需要一个数据库配置类,如下是使用 PooledDataSource 的 Java 配置文件:

package cn.liuxingwei.judge.config;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; /**
* 数据源相关配置
* @author liuxingwei
*/
@Configuration
@EnableTransactionManagement
public class DataConfiguration { /**
* mysql driver 变量,取自外挂配置文件
* @author liuxingwei
*/
@Value("${spring.datasource.driver-class-name}")
private String mysqlDriver; /**
* mysql 连接 url,取自外挂配置文件
* @author liuxingwei
*/
@Value("${spring.datasource.url}")
private String mysqlUrl; /**
* mysql 连接用户名,取自外挂配置文件
* @author liuxingwei
*/
@Value("${spring.datasource.username}")
private String mysqlUsername; /**
* mysql 连接密码,取自外挂配置文件
*/
@Value("${spring.datasource.password}")
private String mysqlPassword; /**
* 数据源(dataSource)定义
* @author liuxingwei
* @return DataSource
*/
@Bean
public PooledDataSource dataSource() {
PooledDataSource dataSource = new PooledDataSource();
dataSource.setDriver(mysqlDriver);
dataSource.setUrl(mysqlUrl);
dataSource.setUsername(mysqlUsername);
dataSource.setPassword(mysqlPassword);
return dataSource;
} }

MyBatis 也不需要特殊的配置,只要在pom中加上依赖:

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>xxx.xxx.xxx</version>
</dependency>

然后分别创建实体类、Mapper 接口和 Mapper XML 文件,就可以使用了。附上一个可用于 Eclipse 的 MyBatis Generator 插件的配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://hostname:port/judge?autoReconnect=true&amp;failOverReadOnly=false&amp;useSSL=false&amp;allowMultiQueries=true&amp;rewriteBatchedStatements=true"
userId="root"
password="123456"></jdbcConnection>
<javaModelGenerator targetPackage="cn.liuxingwei.judge.domain"
targetProject="judge/src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="cn.liuxingwei.judge.mapper"
targetProject="judge/src/main/resources"/>
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.liuxingwei.judge.mapper"
targetProject="judge/src/main/java"/>
<table tableName="%">
<generatedKey column="id" sqlStatement="MySql"/>
<domainObjectRenamingRule searchString="^T" replaceString="" />
</table>
</context>
</generatorConfiguration>

顺便提一句,如果数据库的字符编码为 UTF8,可以在连接 url 中添加 characterEncoding=UTF-8 来实现,不必管 MySQL 服务器本身的设置:

spring.datasource.url=jdbc:mysql://hostname:port/xxx?characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false

但是如果数据库编码为 UTF8MB4,是不能简单地把连接串中的 UTF8 换成 UTF8MB4 的,会报错,只能是在 MySQL 的配置文件(my.cnf)中将 MySQL 的默认字符集设置为 utf8mb4,连接串中不再设置 characterEncoding。

Spring Boot 2 实践记录之 MySQL + MyBatis 配置的更多相关文章

  1. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  2. Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock

    在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...

  3. Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性

    在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...

  4. Spring Boot 2 实践记录之 Redis 及 Session Redis 配置

    先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...

  5. Spring Boot 2 实践记录之 Powermock 和 SpringBootTest

    由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...

  6. Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试

    由于注册时,需要对输入的密码进行加密,使用到了 UUID.sha1.md 等算法.在单元测试时,使用到了 Powermock,记录如下. 先看下加密算法: import org.apache.comm ...

  7. Spring Boot 2 实践记录之 条件装配

    实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...

  8. Spring Boot 2 实践记录之 组合注解原理

    Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...

  9. Spring Boot 报错记录

    Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...

随机推荐

  1. 增加路由ip

    C:\Windows\system32>route add  ip地址  -P 操作完成!

  2. Honeycomb

    Honeycomb http://codeforces.com/gym/102028/problem/F time limit per test 4.0 s memory limit per test ...

  3. 如何添加ECSHOP广告位置

    如何添加ECSHOP广告位置 我们都知道ecshop系统默认的广告位置非常的少,但是一个电子商务网站岂能离开广告?庆幸的是,ecshop预留了足够强大的组件让我们能够完全有可能实现任意我们想要广告位置 ...

  4. word2003设置页码不从第一页开始的方法

    问题描述:如果你想设置页码从第三四页开始,前边不要页码,或者前边的页码是不同类型的.那么这个时候就要用到:插入->分隔符模式. 如果你的页面中的各个标题是从样式和格式中选择的,既是你先设置好各种 ...

  5. nginx反向代理部署与演示(二)

    我们把LB01作为负载均衡器,WEB01和WEB02作为两台web服务器.   WEB01与WEB02虚拟主机配置如下:   我们修改nginx下的conf/nginx.conf文件,在http{}中 ...

  6. 一定要 先删除 sc表 中的 某元组 行,,, 再删除 course表中的 元组行

    一定要  先删除 sc表 中的  某元组   行,,, 再删除  course表中的  元组行 course表 SC表 删除  course表中的  元组行,,出现错误 sc    ---->参 ...

  7. BZOJ 2726 [SDOI2012] 任务安排 - 斜率优化dp

    题解 转移方程与我的上一篇题解一样 : $S\times sumC_j  + F_j = sumT_i \times sumC_j + F_i - S \times sumC_N$. 分离成:$S\t ...

  8. Java 8 可重复注解与类型注解

    Java 8 可重复注解与类型注解 Java 8 对注解处理提供了两点改进:可重复的注解及可用于类型的注解. // 首先要提供一个容器,MyAnnotation 才能用于可重复注解 @Target({ ...

  9. c++11多线程学习笔记之二 mutex使用

    // 1111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include ...

  10. 一篇文章Tornado快速入门

    Tornado是一个PythonWeb框架.一个异步网络库.通过使用非阻塞网络I/O,Tornado能够处理数以千计的连接,这意味着对于实时Web服务来说,Tornado是一个理想的Web框架. 作为 ...