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

11

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