insert blob类型的数据

本文介绍了一种在特定数据库中插入和更新OFD文件的方法。首先检查文件名是否已存在,若不存在则插入新的记录并获取序列键,然后更新记录中的文件内容为提供的字节数据。

相关连接1:http://blog.youkuaiyun.com/cdl2008sky/article/details/3335428

相关连接2:http://wenda.so.com/q/1367453137067044 

 

  public int saveBill(String name, String typeid, String modelidStr, byte[] buffer) {
        String sql3 = "select count(*) c  from  MOFENI_T_SYS_BILLMODEL where  OFDMODELID=?";//OFDMODELID表示ofd文件的名字,重复的名字不能再添加了
        int count = super.queryForInt(sql3, name);
        if (count <= 0) {
            String sql1 = "begin  insert into  MOFENI_T_SYS_BILLMODEL values(?,?,?,'',?,''); commit;end ;";//先insert
            long modelid = getSequenceKey("SEQ_MOFENI_T_SYS_BILLMODEL");//得到序列

            int maxModelorder = super.queryForInt("select nvl(max(modelorder),0)+1 as morder from MOFENI_T_SYS_BILLMODEL where typeid=?", typeid);
            super.update(sql1, modelid, typeid, maxModelorder, name);//先执行insert。模板id,票据类型id,模板序号,模板名称
            String sql2 = " update MOFENI_T_SYS_BILLMODEL set OFDMODEL= ? where id=?";//后update
            super.update(sql2, buffer, modelid);//这里必须用占位符才能解析blob。blob的这里是个字节数组。
            return 1;
        }
        return 2;
    }

要使用 MyBatis 向 TDengine 的 BLOB 类型字段插入数据,可以按照以下步骤操作: ### 1. 定义实体类 首先,定义一个与数据库表对应的实体类,其中包含 BLOB 类型字段。 ```java public class ReportView { private Long id; private String rptname; private byte[] xmldata; // BLOB 类型字段 // 构造函数、Getter 和 Setter 方法 public ReportView() {} public ReportView(Long id, String rptname, byte[] xmldata) { this.id = id; this.rptname = rptname; this.xmldata = xmldata; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getRptname() { return rptname; } public void setRptname(String rptname) { this.rptname = rptname; } public byte[] getXmldata() { return xmldata; } public void setXmldata(byte[] xmldata) { this.xmldata = xmldata; } } ``` ### 2. 定义 Mapper 接口 使用 MyBatis-Plus 的 `BaseMapper` 接口(如果使用 MyBatis-Plus)或者自定义 Mapper 接口来定义插入方法。 ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface ReportViewMapper extends BaseMapper<ReportView> { int insertReportView(ReportView reportView); } ``` ### 3. 编写 SQL 映射文件 创建对应的 XML 映射文件,编写插入 SQL 语句。 ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.ReportViewMapper"> <insert id="insertReportView" parameterType="com.example.entity.ReportView"> INSERT INTO reportview (id, rptname, xmldata) VALUES (#{id}, #{rptname}, #{xmldata}) </insert> </mapper> ``` ### 4. 使用 Mapper 插入数据 在服务层或者其他业务逻辑中调用 Mapper 方法插入数据。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ReportViewService { @Autowired private ReportViewMapper reportViewMapper; public void insertReportViewData() { Long id = 1L; String rptname = "Test Report"; byte[] xmldata = "This is XML data".getBytes(); ReportView reportView = new ReportView(id, rptname, xmldata); reportViewMapper.insertReportView(reportView); } } ``` ### 注意事项 - 确保数据库连接配置正确,能够正常连接到 TDengine 数据库。 - 处理可能出现的异常,如数据库连接异常、SQL 执行异常等。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值