Commit 3b6af779 authored by 李振振's avatar 李振振

11

parent cb3b0f8e
...@@ -31,11 +31,13 @@ public class Vccinfo implements Serializable { ...@@ -31,11 +31,13 @@ public class Vccinfo implements Serializable {
private String updateKey; private String updateKey;
@TableField(value = "tclob") @TableField(value = "tclob")
private String tClob; private String tClob;
@TableField(value = "tbytea")
private byte[] tBytea;
/** /**
* 如果是pg等数据库,字段声明为bytea,则不需要BlobTypeHandler转换,如果声明为blob,则需要转换 * 如果是pg等数据库,字段声明为bytea,则不需要BlobTypeHandler转换,如果声明为blob,则需要转换
*/ */
// @TableField(value = "tblob", typeHandler = org.apache.ibatis.type.BlobTypeHandler.class) @TableField(value = "tblob", typeHandler = org.apache.ibatis.type.BlobTypeHandler.class)
@TableField(value = "tblob") // @TableField(value = "tblob")
private byte[] tBlob; private byte[] tBlob;
} }
...@@ -3,6 +3,7 @@ package com.beagle.informix.mapper; ...@@ -3,6 +3,7 @@ package com.beagle.informix.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.beagle.informix.entity.mybatis.Vccinfo; import com.beagle.informix.entity.mybatis.Vccinfo;
import org.apache.ibatis.annotations.*; import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
...@@ -19,4 +20,6 @@ public interface VccinfoMapper extends BaseMapper<Vccinfo> { ...@@ -19,4 +20,6 @@ public interface VccinfoMapper extends BaseMapper<Vccinfo> {
* @param vccinfo * @param vccinfo
*/ */
void insertData(@Param("vccinfo") Vccinfo vccinfo); void insertData(@Param("vccinfo") Vccinfo vccinfo);
int selectTest();
} }
...@@ -11,6 +11,10 @@ import java.io.*; ...@@ -11,6 +11,10 @@ import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @author lzz * @author lzz
...@@ -28,6 +32,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -28,6 +32,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
} }
public static void main(String[] args) {
try {
new InformixJdbcProxyServiceImpl(null).doConnectJob();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/** /**
* informix.url=jdbc:informix-sqli://localhost:9088/sysadmin:INFORMIXSERVER=informix * informix.url=jdbc:informix-sqli://localhost:9088/sysadmin:INFORMIXSERVER=informix
* informix.driverClassName=com.informix.jdbc.IfxDriver * informix.driverClassName=com.informix.jdbc.IfxDriver
...@@ -40,17 +51,38 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -40,17 +51,38 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
*/ */
@Override @Override
public void doConnectJob() throws Exception{ public void doConnectJob() throws Exception{
// todo 使用jdbc连接proxy,对应的query(clob、blob字段)存在jdbc 类型转换mysql异常的问题 Driver driver = getDriver();
// Unknown exception: Can not find JDBC type `2005` in column type Connection connection = getConnection(driver);
insert(connection);
closeConnection(connection);
deregisterDriver(driver);
}
private void testSameConnectionJob(Connection connection) throws Exception{
Driver driver = getDriver(); Driver driver = getDriver();
Connection conn = getConnection(driver); Connection conn = getConnection(driver);
insert(conn); // 连接一直开启的话,发送的sql在proxy中都是使用一个同一个会话
// update(conn); for (int i = 0; i < 100; i++) {
// query(conn); String querySQL = "SELECT 1";
// delete(conn); Statement stat = connection.createStatement();
ResultSet resultSet = stat.executeQuery(querySQL);
}
closeConnection(conn); closeConnection(conn);
deregisterDriver(driver); deregisterDriver(driver);
} }
private void testAloneConnectionJob() throws Exception{
Driver driver = getDriver();
for (int i = 0; i < 100; i++) {
// 这种情况下的话,每次新建一个连接,在proxy中都会对应新增一个连接
Connection conn = getConnection(driver);
String querySQL = "SELECT 1";
Statement stat = conn.createStatement();
ResultSet resultSet = stat.executeQuery(querySQL);
closeConnection(conn);
}
deregisterDriver(driver);
}
private Driver getDriver() throws Exception{ private Driver getDriver() throws Exception{
// 定义informix的驱动信息 // 定义informix的驱动信息
String driverUrl = "com.mysql.cj.jdbc.Driver"; String driverUrl = "com.mysql.cj.jdbc.Driver";
...@@ -66,7 +98,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -66,7 +98,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
// 创建testdb数据库、my_test_create_table表 // 创建testdb数据库、my_test_create_table表
// 现在模拟插入、更新、删除、查询等sql // 现在模拟插入、更新、删除、查询等sql
// 插入 // 插入
String insertSql = "insert into cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey,tclob,tblob) values (?, ? , ?, ?, ? , ?,?,? )"; String insertSql = "insert into SYSDBA.cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey,tclob,tbytea,tblob) values (?, ? , ?, ?, ? , ?,?,?,? )";
PreparedStatement stat = conn.prepareStatement(insertSql); PreparedStatement stat = conn.prepareStatement(insertSql);
stat.setObject(1, "123456"); stat.setObject(1, "123456");
stat.setObject(2, "vname"); stat.setObject(2, "vname");
...@@ -79,11 +111,16 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -79,11 +111,16 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
StringReader reader = new StringReader(clobContent); StringReader reader = new StringReader(clobContent);
stat.setClob(7, reader, clobContent.length()); stat.setClob(7, reader, clobContent.length());
// 处理blob字段 // 处理blob字段
Resource resource = resourceLoader.getResource("classpath:EnableLoopback.exe"); Resource resource = resourceLoader.getResource("classpath:test.txt");
File inputStream = resource.getFile(); File inputStream = resource.getFile();
FileInputStream fis = new FileInputStream(inputStream); FileInputStream fis = new FileInputStream(inputStream);
stat.setBlob(8, fis); stat.setBlob(8, fis);
resource = resourceLoader.getResource("classpath:test.txt");
inputStream = resource.getFile();
fis = new FileInputStream(inputStream);
stat.setBlob(9, fis);
stat.executeUpdate(); stat.executeUpdate();
} }
private void update(Connection conn) throws Exception{ private void update(Connection conn) throws Exception{
// 创建testdb数据库、my_test_create_table表 // 创建testdb数据库、my_test_create_table表
...@@ -94,28 +131,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -94,28 +131,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
stat.executeUpdate(); stat.executeUpdate();
} }
private void query(Connection conn) throws Exception { private void query(Connection conn) throws Exception {
String querySQL = "select * from cti_vccinfo_new where vccid = '123456' "; String querySQL = "SELECT id FROM sc_test";
PreparedStatement stat = conn.prepareStatement(querySQL); Statement stat = conn.createStatement();
ResultSet resultSet = stat.executeQuery(); ResultSet resultSet = stat.executeQuery(querySQL);
if (resultSet != null) {
while (resultSet.next()) {
log.info("vccid =>" + resultSet.getString("vccid"));
Clob clob = resultSet.getClob("tclob");
BufferedReader reader = new BufferedReader(clob.getCharacterStream());
String clobStr = "";
String finalClobStr = "";
while ((clobStr = reader.readLine())!= null) {
finalClobStr += clobStr;
}
log.info("tclob =>" + finalClobStr);
Blob blob = resultSet.getBlob("tblob");
BufferedInputStream bins= new BufferedInputStream(blob.getBinaryStream());
byte [] bytes = bins.readAllBytes();
String blobStr= new String(bytes, StandardCharsets.UTF_8);
log.info("tblob =>" + blobStr);
}
}
} }
private void delete(Connection conn) throws Exception{ private void delete(Connection conn) throws Exception{
String deleteSql = "delete from cti_vccinfo_new where vccid = '123456' "; String deleteSql = "delete from cti_vccinfo_new where vccid = '123456' ";
conn.createStatement().executeUpdate(deleteSql); conn.createStatement().executeUpdate(deleteSql);
...@@ -125,9 +147,8 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -125,9 +147,8 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
Properties properties = new Properties(); Properties properties = new Properties();
// 组装连接数据库信息 // 组装连接数据库信息
// jdbc:informix-sqli://<server>:<port1526>/<database>:informixserver=<dbservername> // jdbc:informix-sqli://<server>:<port1526>/<database>:informixserver=<dbservername>
StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://localhost:3307") StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://localhost:3307/test?useServerPrepStmts=true");
.append("/") //;.append("?useServerPrepStmts=true");
.append("testdb").append("?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true");
DriverManager.setLoginTimeout(10); DriverManager.setLoginTimeout(10);
properties.put("user", "root"); properties.put("user", "root");
properties.put("password", "Apurelove9014"); properties.put("password", "Apurelove9014");
...@@ -136,4 +157,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService { ...@@ -136,4 +157,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService {
private void closeConnection(Connection connection) throws Exception { private void closeConnection(Connection connection) throws Exception {
connection.close(); connection.close();
} }
} }
...@@ -2,22 +2,20 @@ package com.beagle.informix.service.impl; ...@@ -2,22 +2,20 @@ package com.beagle.informix.service.impl;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beagle.informix.entity.mybatis.Vccinfo; import com.beagle.informix.entity.mybatis.Vccinfo;
import com.beagle.informix.mapper.VccinfoMapper; import com.beagle.informix.mapper.VccinfoMapper;
import com.beagle.informix.service.InformixService; import com.beagle.informix.service.InformixService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** /**
* @author lzz * @author lzz
...@@ -30,15 +28,9 @@ import java.util.List; ...@@ -30,15 +28,9 @@ import java.util.List;
public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccinfo> implements InformixService { public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccinfo> implements InformixService {
@Resource @Resource
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
@Resource
private VccinfoMapper vccinfoMapper;
@Override @Override
@Transactional
public void doConnectJob() throws Exception{ public void doConnectJob() throws Exception{
insertMethod(); insertMethod();
// updateMethod();
// queryMethod();
// delete();
} }
private void insertMethod() throws Exception{ private void insertMethod() throws Exception{
Vccinfo vccinfo = new Vccinfo(); Vccinfo vccinfo = new Vccinfo();
...@@ -57,10 +49,16 @@ public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccin ...@@ -57,10 +49,16 @@ public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccin
try (FileInputStream fis = new FileInputStream(inputStream)){ try (FileInputStream fis = new FileInputStream(inputStream)){
vccinfo.setTBlob(fis.readAllBytes()); vccinfo.setTBlob(fis.readAllBytes());
} }
resource = resourceLoader.getResource("classpath:test.txt.docx");
inputStream = resource.getFile();
try (FileInputStream fis = new FileInputStream(inputStream)){
vccinfo.setTBytea(fis.readAllBytes());
}
// insert // insert
// 如果使用实体保存,则需要在blob字段上新增BlobTypeHandler转换 // 如果使用实体保存,则需要在blob字段上新增BlobTypeHandler转换
// 如果使用xml的insert保存,也需要在属性上设置BlobTypeHandler转换 // 如果使用xml的insert保存,也需要在属性上设置BlobTypeHandler转换
baseMapper.insertData(vccinfo); baseMapper.insertData(vccinfo);
// this.save(vccinfo);
} }
private void updateMethod() throws Exception { private void updateMethod() throws Exception {
...@@ -72,7 +70,7 @@ public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccin ...@@ -72,7 +70,7 @@ public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccin
} }
private void queryMethod() { private void queryMethod() {
List<Vccinfo> vccinfoList = vccinfoMapper.queryList(1); List<Vccinfo> vccinfoList = this.baseMapper.queryList(1);
log.info("queryAll:{}", JSONUtil.toJsonStr(vccinfoList)); log.info("queryAll:{}", JSONUtil.toJsonStr(vccinfoList));
} }
private void delete() { private void delete() {
......
spring: spring:
datasource: datasource:
druid:
# url: jdbc:informix-sqli://localhost:9088/testdb:INFORMIXSERVER=informix
#&useServerPrepStmts=true
# url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai # url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai # url: jdbc:mysql://localhost:3307/test.txt
url: jdbc:mysql://localhost:3307/test?useServerPrepStmts=true
# url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true # url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true
username: root username: root
# username: informix
# password: in4mix
password: Apurelove9014 password: Apurelove9014
# max-active: 20 # type: com.alibaba.druid.pool.DruidDataSource
# max-wait: 60000 # druid:
# initial-size: 5 # max-active: 500
# connect-timeout: 30 # max-wait: 60000
# min-idle: 5 # initial-size: 500
# min-evictable-idle-time-millis: 300000 # connect-timeout: 30
# validation-query: select 1 # min-idle: 20
# test-on-borrow: false # min-evictable-idle-time-millis: 300000
# test-on-return: false # validation-query: select 1
# test-while-idle: false # test.txt-on-borrow: false
# time-between-eviction-runs-millis: 60000 # test.txt-on-return: false
# driver-class-name: com.informix.jdbc.IfxDriver # test.txt-while-idle: false
# time-between-eviction-runs-millis: 60000
# driver-class-name: com.informix.jdbc.IfxDriver
driver-class-name: com.mysql.jdbc.Driver # com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver # com.mysql.cj.jdbc.Driver
jpa: jpa:
properties: properties:
......
-- serial 代表自增,本质上也是个int类型 -- serial 代表自增,本质上也是个int类型
create table cti_vccinfo_new create table cti_vccinfo_new
( (
vccid serial not null, vccid int not null primary key auto_increment,
vccname VARCHAR(255), vccname VARCHAR(255),
effective INTEGER default 0 not null, effective INTEGER default 0 not null,
agentmax INTEGER default 0 not null, agentmax INTEGER default 0 not null,
ivrmax INTEGER default 0 not null, ivrmax INTEGER default 0 not null,
updatekey VARCHAR(30), updatekey VARCHAR(30),
tclob clob null, info longtext,
tblob blob null, tclob longtext null,
primary key (vccid) constraint PK_CTI_VI_new tblob blob null
); );
-- 对接proxy,现在 dml 的 crud的sql都能正常识别并运行 -- 对接proxy,现在 dml 的 crud的sql都能正常识别并运行
......
this is test text
\ No newline at end of file
...@@ -13,12 +13,12 @@ import javax.annotation.Resource; ...@@ -13,12 +13,12 @@ import javax.annotation.Resource;
@SpringBootTest(classes = BaseApplication.class) @SpringBootTest(classes = BaseApplication.class)
class InformixServiceTest { class InformixServiceTest {
// @Resource(name = "informixJdbcService") @Resource(name = "informixJdbcService")
// private InformixService informixJdbcService; private InformixService informixJdbcService;
@Resource(name = "informixJdbcProxyService") @Resource(name = "informixJdbcProxyService")
private InformixService informixJdbcProxyService; private InformixService informixJdbcProxyService;
@Resource(name = "informixHibernateService") // @Resource(name = "informixHibernateService")
private InformixService informixHibernateService; private InformixService informixHibernateService;
// @Resource(name = "informixIbatisService") // @Resource(name = "informixIbatisService")
// private InformixService informixIbatisService; // private InformixService informixIbatisService;
...@@ -57,4 +57,14 @@ class InformixServiceTest { ...@@ -57,4 +57,14 @@ class InformixServiceTest {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test
void testJdbcInfo() {
try {
informixJdbcService.doConnectJob();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment