In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "how to use mybatis's typeHandler to stream read and write clob". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use mybatis's typeHandler to stream read and write clob" can help you solve the problem.
TypeHandler performs streaming reading and writing to clob.
There are three steps:
Step 1: write the typehandler file package com.dcits.edps.common.utils; import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.StringReader;import java.io.Writer;import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.MappedJdbcTypes Import org.apache.ibatis.type.MappedTypes;import org.springframework.util.FileCopyUtils; / *
Title: working with Clob field
*
Description: use streams to read and write Clob fields
*
Compiled by: zt
*
Version: 1.0
* / @ MappedTypes ({String.class}) @ MappedJdbcTypes ({JdbcType.CLOB}) public class OscarClobTypeHandler extends BaseTypeHandler {@ Override public void setNonNullParameter (PreparedStatement ps, int I, String parameter, JdbcType jdbcType) throws SQLException {final StringReader sr = new StringReader (parameter); final int length = parameter.getBytes () .length; ps.setCharacterStream (I, sr, length) Sr.close ();} @ Override public String getNullableResult (ResultSet rs, String columnName) throws SQLException {final OutputStream outPutStream = new ByteArrayOutputStream (); final Writer writer = new OutputStreamWriter (outPutStream); try {Reader reader = rs.getCharacterStream (columnName) If (null = = reader) {return null;} else {FileCopyUtils.copy (reader, writer);}} catch (IOException e) {e.printStackTrace () } return outPutStream.toString ();} @ Override public String getNullableResult (ResultSet rs, int columnIndex) throws SQLException {final OutputStream outPutStream = new ByteArrayOutputStream (); final Writer writer = new OutputStreamWriter (outPutStream); try {Reader reader = rs.getCharacterStream (columnIndex) If (null = = reader) {return null;} else {FileCopyUtils.copy (reader, writer);}} catch (IOException e) {e.printStackTrace () } return outPutStream.toString ();} @ Override public String getNullableResult (CallableStatement cs, int columnIndex) throws SQLException {final OutputStream outPutStream = new ByteArrayOutputStream (); final Writer writer = new OutputStreamWriter (outPutStream); try {Reader reader = cs.getCharacterStream (columnIndex) If (null = = reader) {return null;} else {FileCopyUtils.copy (reader, writer);}} catch (IOException e) {e.printStackTrace () } return outPutStream.toString ();}} step 2: add nodes to the configuration file of mybatis
It is important to note that the child nodes in the nodes in the configuration file of mybatisd need to be in order
The order is properties- > settings- > typeAliases- > typeHandlers- > objectFactory- > objectWrapperFactory- > plugins- > environments- > databaseIdProvider- > mappers.
Step three
① needs to add jdbcType to resultMap, which uses a stream when reading, as shown below
② uses when writing data:
Fsfh=# {fsfh,jdbcType=CLOB} mybatis processes the clob field
1. Because the ggnr field in the database is of type clob (stream objects can be stored), the long text passed by the foreground is processed in the background.
two。 After some search, we found the method of mybatis to deal with the clob field, tried to deal with clob, and found that the length of stored data is more than one or two hundred, the field in the database is empty and the storage is short.
3. After consulting and communicating with colleagues in the company, two schemes are summarized: (1). Get the database connection directly and manipulate the precompiled object (the old method, the high concurrency database is easy to get stuck and does not match). (2) .sql uses the begin end method (tested to store data with less than 4000 bytes, which does not meet the requirements)
4. All right, check the official jdbc documentation again. It is mentioned above that there are data restrictions in ojdbc. A higher version is recommended, and then the ojdbc6 problem is solved instead.
After the change, ok. If there is a lazy load, add this cglib-nodep-3.2.4.jar
Mapper processing is attached:
1. Modify Mapper.xml
2.sql
Insert into CQGG (ggnr) values (# {ggnr,jdbcType=CLOB}) about "how to use mybatis's typeHandler to stream read and write clob" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.