spring+mybatis+Blob

这两天遇到一个问题,其中项目是将一个老的项目迁移成微服务架构的,用mybatis进行持久层开发;原本是查一个表,只不过其中一个字段执行了一个存储过程,以后要到全业务数据中心后,考虑不跑存储过程了,然后就将存储过程对字段的处理转移到了java代码中,难点在于这是一个blob类型的大字段,在oracle数据库中直接用dbms_lob_substr()函数直接截取需要的字段就可以查处相关的数据,展示的也是字符串,但是到java中通过断点则是[B@69df6c1d,明显这是一个内存地址,我暂时还不太明白为什么显示一个内存地址。

寻求解决办法:使用晚上说的直接读取blob大字段,然后通过流的形式将blob转成二进制数组

private static byte[] blobToBytes(Blob blob) {
        BufferedInputStream is = null;
        byte[] bytes = null;
        try {
            is = new BufferedInputStream(blob.getBinaryStream());
            bytes = new byte[(int) blob.length()];
            int len = bytes.length;
            int offset = 0;
            int read = 0;
 
            while (offset < len
                    && (read = is.read(bytes, offset, len - offset)) >= 0) {
                offset += read;
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
 
    }

 或者将blob大字段转String 

String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");//blob 转 String

上面这两种方式都会出现全文乱码(不是中文乱码)

继续尝试网上一种通过mybatis配置直接返回二进制数组的mybatis中加入

这里放下原网址:https://blog.csdn.net/lqzkcx3/article/details/80969105

<!-- 映射文件-->
<resultMap type="map" id="blobResultMap"> 
    <!--为了以示区分, 特意将property配置为和column不一样 -->
    <result property="AX_D" column="AX_DATA" jdbcType="BLOB" javaType = "_byte[]"/> 
</resultMap>

注意:查询的SQL语句中, 是针对两个字段; 但在resultMap的映射配置中, 我们只设置了一个,也就是只配置了特殊情况——BLOB映射,其他字段的匹配最终还是交给了Mybatis自主完成。
<select> 标签中的属性设置中,我们使用的是resultMap来引用我们配置的<resultMap>,而不是通常的resultType。
<resultMap>的type属性, 我们设置的依然是map,这样可以保证最大的兼容性。

这样是通过数据库查询的blob字段放入我们自己配置的map,进入的是blob,出来的是byte[],按照理想状态是可以的,结果debug出来的是非正常的二进制数组,例如【-1,40,-1,127,-1,0,0,0】

后来想,通过dbms_lob_substr()方法查出来的是字符串,只是不知到为什么显示是内存地址,就想查询结果往map放一边,就改动成

<!-- 映射文件-->
<resultMap type="map" id="blobResultMap"> 
    <!--为了以示区分, 特意将property配置为和column不一样 -->
    <result property="AX_D" column="AX_DATA" jdbcType="BLOB" javaType = "String"/> 
</resultMap>

这样在查询结果就能正常显示了,例如e8ddd787222;

至此,忙活了一整天的问题得到了解决(其实在处理存储过程的时候也费了好大劲,在sql中实在解决不了才转到java代码中的)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Spring Boot + Vue.js + MyBatis-Plus实现以表格形式显示书籍ID对应的照片的代码实现: 1. 创建Book实体类 ```java @Entity @Table(name = "book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String author; private String photoUrl; // 省略getter和setter方法 } ``` 2. 创建BookMapper接口 ```java @Mapper public interface BookMapper extends BaseMapper<Book> { @Select("SELECT photo_url FROM book WHERE id = #{bookId}") String getPhotoUrlById(Long bookId); } ``` 3. 创建BookService接口和实现类 ```java public interface BookService { List<Book> getAllBooks(); String getPhotoUrlById(Long bookId); } @Service public class BookServiceImpl implements BookService { @Autowired private BookMapper bookMapper; @Override public List<Book> getAllBooks() { return bookMapper.selectList(null); } @Override public String getPhotoUrlById(Long bookId) { return bookMapper.getPhotoUrlById(bookId); } } ``` 4. 创建BookController类 ```java @RestController @RequestMapping("/api/books") public class BookController { @Autowired private BookService bookService; @GetMapping("") public List<Book> getAllBooks() { return bookService.getAllBooks(); } @GetMapping("/{bookId}/photo") public String getPhotoUrlById(@PathVariable Long bookId) { return bookService.getPhotoUrlById(bookId); } } ``` 5. 创建BookTable.vue组件 ```vue <template> <div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Author</th> <th>Photo</th> </tr> </thead> <tbody> <tr v-for="book in books" :key="book.id"> <td>{{ book.id }}</td> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td> <img :src="'data:image/png;base64,' + photos[book.id]" /> </td> </tr> </tbody> </table> </div> </template> <script> import axios from "axios"; export default { data() { return { books: [], photos: {}, }; }, created() { this.getBooks(); }, methods: { getBooks() { axios.get("/api/books").then((response) => { this.books = response.data; this.getPhotos(); }); }, getPhotos() { this.books.forEach((book) => { axios .get(`/api/books/${book.id}/photo`, { responseType: "blob" }) .then((response) => { const reader = new FileReader(); reader.readAsDataURL(response.data); reader.onload = () => { this.photos[book.id] = reader.result.split(",")[1]; }; }); }); }, }, }; </script> ``` 以上就是Spring Boot + Vue.js + MyBatis-Plus实现以表格形式显示书籍ID对应的照片的代码实现。需要注意的是,在获取照片时需要指定响应类型为blob,然后使用FileReader将blob转换成Base64编码的字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值