Spring Boot 2 实践记录之 MySQL + MyBatis 配置
如果不需要连接池,那么只需要简单的在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&failOverReadOnly=false&useSSL=false&allowMultiQueries=true&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 配置的更多相关文章
- Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题
按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...
- Spring Boot 2 实践记录之 Redis 及 Session Redis 配置
先说 Redis 的配置,在一些网上资料中,Spring Boot 的 Redis 除了添加依赖外,还要使用 XML 或 Java 配置文件做些配置,不过经过实践并不需要. 先在 pom 文件中添加 ...
- Spring Boot 2 实践记录之 Powermock 和 SpringBootTest
由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...
- Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试
由于注册时,需要对输入的密码进行加密,使用到了 UUID.sha1.md 等算法.在单元测试时,使用到了 Powermock,记录如下. 先看下加密算法: import org.apache.comm ...
- Spring Boot 2 实践记录之 条件装配
实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...
- Spring Boot 2 实践记录之 组合注解原理
Spring 的组合注解功能,网上有很多文章介绍,不过都是介绍其使用方法,鲜有其原理解析. 组合注解并非 Java 的原生能力.就是说,想通过用「注解A」来注解「注解B」,再用「注解B」 来注解 C( ...
- Spring Boot 报错记录
Spring Boot 报错记录 由于新建的项目没有配置数据库连接启动报错,可以通过取消自动数据源自动配置来解决 解决方案1: @SpringBootApplication(exclude = Dat ...
随机推荐
- java swing:文本框添加滚动条
有几点要注意: 1.默认的滚动条,仅在输入的文本超过文本框时才会显示..没有超过文本框是不会显示的: 2.设置矩形大小,是在滚动条上设置,而不是在文本框上设置: 示例代码如下: public clas ...
- luoguP1196(带权并查集)
题目链接:https://www.luogu.org/problemnew/show/P1196 思路: 带权并查集.对每个结点,构造表示该结点的头结点,该结点距头结点的距离,该列的大小3个数组. 在 ...
- 关于weblogic报UnsatisfiedLinkError Native Library xxx.so already loaded
一.场景 最近写的一个系统,在Tomcat测试完后说要改使用weblogic,于是在服务器上安装了weblogic,捣鼓了半天,一个个问题冒了出来,其中就有个比较麻烦的报错:UnsatisfiedLi ...
- error: In function ‘void* opencv_showimg(void*)’:
今天这个问题折磨了我一下午,终于知道是为什么了,心酸历程.....赶紧来记录一下 错误: /home/wj/workspace/Loitor_VI_Sensor_SDK_V1./SDK/src/cam ...
- 使用jdbc编程实现对数据库的操作以及jdbc问题总结
1.创建数据库名为mybatis. 2. 在数据库中建立两张表,user与orders表: (1)user表: (2)orders表: 3.创建工程 * 开发环境: * eclipse mars * ...
- JTSL/EL Expression学习
最早的一个学习笔记,时间过去了久了,供java web初学者参考. JTSL/EL Expression学习安排 学习目标:掌握几个常见标签的使用,通晓工作原理,详细到代码层面,遇到问题时能查得出异常 ...
- MySQL主从复制备份
前言 数据库实时备份的需求很常见,MySQL本身提供了 Replication 机制,摘译官方介绍如下: MySQL Replication 可以将一个主数据库中的数据同步到一个或多个从数据库中.并且 ...
- APScheduler 浅析
前言 APScheduler是python下的任务调度框架,全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Linux下的Crontab那样 ...
- Golang之redis
redis是个开源的高性能的key-value的内存数据库,可以把它当成远程的数据结构. 支持的value类型非常多,比如string.list(链表).set(集合). hash表等等 redis性 ...
- 纯净版Windows7系统迅雷下载路径
windows 7 旗舰版64位------------------- Windows 7 Ultimate (x64) - DVD (Chinese-Simplified) 详细信息 文件名 ...