如果不需要连接池,那么只需要简单的在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. Hibernate查询方式(补)

    -----------------siwuxie095                             Hibernate 查询方式         1.对象导航查询     根据已经加载的对 ...

  2. jQuery对象转换为DOM对象(转)

    jQuery对象转换为dom对象 只有jQuery对象才能调用jQuery类库的各种函数,同样有些dom对象的属性和方法在jQuery上也是无法调用的,不过基本上jQuery类库提供的函数包含了所有的 ...

  3. Python学习记录day7

    目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...

  4. IIS不能下载.apk文件

    IIS服务器不能下载.apk文件的原因:iis的默认MIME类型中没有.apk文件,所以无法下载. 打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 单击MIME类型下的“MIME类 ...

  5. C#中发送邮件,包含Html代码 CDO.Message

    C#代码: /// <summary> /// 发送邮件 /// </summary> /// <param name="context">&l ...

  6. Object类中通用方法之:toString()方法

    1.Java所有的对象都是Object类的实例,都可以直接调用该类中定义的方法,这些方法称为通用方法 2.toString()方法即为Object类中定义的通用方法之一 3.平时我们如果在控制台直接打 ...

  7. 学习GIT 版本控制的好去处 另GDB资料

    廖雪峰的官方网站 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 作者不仅仅是做技 ...

  8. jar 包 的用处 ,dozer、poi、itext 、jxl 、jbarcode 、itextrenderer jquery 效果

    1.dozer 做类型转换的, 新建 xml 文件 描述两个实体的对应关系 ,DozerBeanMapper mapper =new DozerBeanMapper().addMappingFiles ...

  9. 7.25 js 自定义方法 a.b

    调用: $(function(){ Starinput.initiate({name:'qr1_z.startlevel',tar: $("#sitelogo1"), stars: ...

  10. oss browser

    版本问题 https://github.com/aliyun/oss-browser/blob/master/all-releases.md?spm=5176.doc61872.2.7.Ic2az6& ...