1. 程式人生 > >Spring Boot項目中MyBatis連接DB2和MySQL數據庫返回結果中一些字符消失——debug筆記

Spring Boot項目中MyBatis連接DB2和MySQL數據庫返回結果中一些字符消失——debug筆記

select() 再次 batis ons tor sta nba spring 測試

寫這篇記錄的原因是因為我之前在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筆記