Spring Boot项目中MyBatis连接DB2和MySQL数据库返回结果中一些字符消失——debug笔记
写这篇记录的原因是因为我之前在Spring Boot项目中通过MyBatis连接DB2返回的结果中存在一些字段,
这些字段的元素中缺少了一些符号,所以我现在通过在自己的电脑上通过MyBatis连接DB2和MySQL,
来重现之前碰到的情况。
为了方便分析起见,我这里新建一个test表,并插入一些数据。以下是相关的SQL语句:
drop table test;
create table test (
id int,
name varchar(20),
memo character(50)
);
insert into test (id, name, memo) values (1, 'zifeiy', '床前(明月)光');
insert into test (id, name, memo) values (2, '王()小明', '春眠!@#$%^&**()不觉()《》晓');
然后通过如下SQL语句可以看到返回结果:
SELECT * FROM test
DB2中返回的结果:

MySQL中返回的结果:
mysql> select * from test;
+------+------------+-------------------------------+
| id | name | memo |
+------+------------+-------------------------------+
| 1 | zifeiy | 床前(明月)光 |
| 2 | 王()小明 | 春眠!@#$%^&**()不觉()《》晓 |
+------+------------+-------------------------------+
接下来我们开始编写一个简单的Spring Boot项目,来重现我们之前遇到的问题。
首先去 https://start.spring.io/ 生成一个名为 spring-test 的 spring boot 项目。
以下时application.properties的配置,其中包括连接DB2的信息和连接MySQL的信息。
这里默认连接DB2,而将MySQL连接的属性注释掉了,在连接MySQL的时候需将DB2的连接信息注释掉,
而将MySQL的连接信息取消掉注释。
# DB Configuration for DB2
spring.datasource.url=jdbc:db2://localhost:50000/SAMPLE
spring.datasource.username=zifeiy
spring.datasource.password=password
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
## DB Configuration for MySQL
#spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/zifeiydb?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
#spring.datasource.username=root
#spring.datasource.password=password
# logging
logging.level.com.anbank.tplusone=debug
现在回到我们的 spring-test 项目,他的目录结构大概是这个样子的:

新建名为Test的Java Bean:
package com.zifeiy.springtest.po;
public class Test {
private int id;
private String name;
private String memo;
// getters & setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
}
新建Mapper:
package com.zifeiy.springtest.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.zifeiy.springtest.po.Test;
@Mapper
public interface TestMapper {
@Select("select * from test")
List<Test> select();
}
新建Serivce接口及其实现:
TestService.java:
package com.zifeiy.springtest.service;
import java.util.List;
import com.zifeiy.springtest.po.Test;
public interface TestService {
List<Test> select();
}
TestServiceImpl.java:
package com.zifeiy.springtest.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zifeiy.springtest.mapper.TestMapper;
import com.zifeiy.springtest.po.Test;
import com.zifeiy.springtest.service.TestService;
@Service
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public List<Test> select() {
return this.testMapper.select();
}
}
新建Controller:
package com.zifeiy.springtest.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zifeiy.springtest.po.Test;
import com.zifeiy.springtest.service.TestService;
@RestController
@RequestMapping("/")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/test")
public List<Test> select() {
return this.testService.select();
}
}
然后运行 SpringTestApplication.java 程序,并登陆 http://localhost:8080/test 查看结果如下:
[{"id":1,"name":"zifeiy","memo":"床前(明月)光 "},{"id":2,"name":"王()小明","memo":"春眠!@#$%^&**()不觉()《》晓 "}]
说明在 DB2 Express-C 11 中使用MySQL没有什么问题。
修改 application.properties 文件,使其指向MySQL连接,再次运行。
结果类似,只不过MySQL里面Character类型后面空着的那些空格没有显示出来:
[{"id":1,"name":"zifeiy","memo":"床前(明月)光"},{"id":2,"name":"王()小明","memo":"春眠!@#$%^&**()不觉()《》晓"}]
说明在 MySQL 5.7 中使用MyBatis也灭有什么问题。
想到生产环境和测试环境中使用的是 DB2 9,所以测试一下在 DB2 9 中是否出现这种情况,修改 applications.properties 中的连接信息到测试数据库。
然后结果是这样的:
[{"id":1,"name":"zifeiy","memo":"床前(明月)光 "},{"id":2,"name":"王()小明","memo":"春眠!@#$%^&**()不觉()《》晓 "}]
发现好像没有什么问题。
小结:暂时不能重现之前碰到的问题。后续如果发现问题将在此随笔中继续添加内容。
Spring Boot项目中MyBatis连接DB2和MySQL数据库返回结果中一些字符消失——debug笔记的更多相关文章
- 【Spring Boot】Spring Boot项目设置多个配置文件,并在生产环境中的Tomcat设置对应的配置文件
1.修改Spring Boot项目配置文件 除了主配置文件,另外创建2个配置文件,注意命名要用application-*.properties 主配置文件中写入,使用dev作为开发配置 spring. ...
- Jmeter连接DB2/ORACLE/MYSQL数据库
连接DB2 1.将db2数据库驱动db2java.jar.db2jcc.jar放入jmeter的lib/下,同时也要放入本地jdk目录下例如:C:\Program Files\Java\jdk1.7. ...
- Spring Boot入门(五):使用JDBC访问MySql数据库
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序开发的过程中,操作数据库是必不可少的部分,前面几篇博客中,也一直未涉及到数据库的操作,本篇博客 就 ...
- 在Spring Boot项目中使用Spock框架
转载:https://www.jianshu.com/p/f1e354d382cd Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring B ...
- Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置
0 引言 本文主要在Spring Boot 基础项目的基础上,添加 Mysql .MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作. 1 创建数据表 这个过程就暂时省略了. 2 ...
- Spring Boot项目中使用Mockito
本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...
- 在Spring Boot项目中使用Spock测试框架
本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...
- IDEA连接远程服务器Docker部署Spring Boot项目
开始前的准备工作 拥有一台云服务器,我的是腾讯云服务器(CentOS7) 腾讯云服务器安装Docker,我的版本是Docker 19.03.9,关于安装和配置镜像加速器可以查阅我的另一篇博文:http ...
- 简单的Spring Boot项目——实现连接Mysql数据库
一.创建Spring Boot项目 参考:使用IntelliJ IDEA创建简单的Spring Boot项目 二.数据库.表的创建 三.项目开发 3.1 pom.xml文件配置 <?xml ve ...
随机推荐
- mongodb文件
https://github.com/mongodb/mongo/tree/master 或 https://www.mongodb.com/download-center?jmp=nav#comm ...
- 快速排序Quick_Sort
快排——排序中的明星算法,也几乎是必须掌握的算法,这次我们来领略以下快排为何魅力如此之大. 快排主要有两种思路,分别是挖坑法和交换法,这里我们以挖坑法为例来进行介绍,交换法可以参考这篇博文.值得一提的 ...
- Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
Property or method "openPageOffice" is not defined on the instance but referenced during r ...
- 2019EC-Final参赛总结
本来想发知乎上的,后来发现太长就放这好了23333 没写过这种东西,所以写得比较混乱&流水账 以下内容均为我的个人视角XD 赛前 在车上的时候,红太阳跟我们说他头晕(虽然他好像每场比赛都头 ...
- C# 未能加载项目文件
在使用VS打开从网上下载或者从其他地方复制得来的解决方案时,经常会出现这样一个错误,"在解决方案中的一个或多个项目由于以下原因未能加载项目文件或网站已移动或已重命名,或者不在您的计算机上.& ...
- JQuery实践--Ajax
Ajax概览无需刷新用户页面而发起服务器请求的技术.创建一个XHR实例: var xhr if(window.XMLHttpRequest) { xhr = new XML ...
- SessionState,默認mode應該是"InProc"
在ASP.NET的sessionState的三種屬性 http://www.dotblogs.com.tw/boei/archive/2010/07/06/16414.aspx需要在另外的config ...
- 使用python开发ansible自定义模块的简单案例
安装的版本ansible版本<=2.7,<=2.8是不行的哦 安装模块 pip install ansible==2.7 先导出环境变量 我们自定义模块的目录. 我存放的目录 export ...
- redis之哨兵集群
一.主从复制背景问题 Redis主从复制可将主节点数据同步给从节点,从节点此时有两个作用: 一旦主节点宕机,从节点作为主节点的备份可以随时顶上来. 扩展主节点的读能力,分担主节点读压力. 但是问题是: ...
- opensuse tumbleweed中安装code
在opensuse上启用snaps并安装visual studio code snaps是一个应用程序,运行在很多流行linux发行版上, 其所有依赖项都打包在一个构建中,并且会自动更新并能优雅地回滚 ...