1. Write type parser
scheme 1: implement TypeHandler rewriting method
package com.aliyun.kuxuandemo.typeHandler;
import org.apache.ibatis.type.*;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @author: wb-zcx696752
* @description:
* @data: 2020/10/13 11:19 PM
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListTypeHandler implements TypeHandler<List<String>> {
@Override
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
StringBuffer sb = new StringBuffer();
for (String pa : parameter) {
sb.append(pa).append(",");
}
ps.setString(i, sb.toString().substring(0, sb.toString().length() - 1));
}
@Override
public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
String[] split = resultSet.getString(s).split(",");
return Arrays.asList(split);
}
@Override
public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
String[] split = resultSet.getString(i).split(",");
return Arrays.asList(split);
}
@Override
public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
String[] split = callableStatement.getString(i).split(",");
return Arrays.asList(split);
}
}
scheme 2: inherit BaseTypeHandler class, override method
package com.aliyun.kuxuandemo.typeHandler;
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 java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @author: wb-zcx696752
* @description:
* @data: 2020/10/13 12:56 PM
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListTypeHandler extends BaseTypeHandler<List<String>> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
//1.List集合转字符串
StringBuffer sb = new StringBuffer();
for (String string : strings) {
sb.append(string).append(",");
}
//2.设置给ps
preparedStatement.setString(i, sb.toString().substring(0, sb.toString().length() - 1));
}
@Override
public List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {
String[] split = resultSet.getString(s).split(",");
return Arrays.asList(split);
}
@Override
public List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {
String[] split = resultSet.getString(i).split(",");
return Arrays.asList(split);
}
@Override
public List<String> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String[] split = callableStatement.getString(i).split(",");
return Arrays.asList(split);
}
}
2. Register the parser in mybatis config. XML
<typeHandlers>
<typeHandler handler="com.aliyun.kuxuandemo.typeHandler.ListTypeHandler"/>
</typeHandlers>
3. Use custom type parser
query
@Select("select video_id,corp_id,oss_url,title,category_list,description from video_basic_info " +
"where video_id=#{VideoID} and corp_id=#{CorpID}")
@Results({
@Result(property = "videoId", column = "video_id"),
@Result(property = "corpId", column = "corp_id"),
@Result(property = "ossUrl", column = "oss_url"),
@Result(property = "categoryList", column = "category_list", typeHandler = com.aliyun.kuxuandemo.typeHandler.ListTypeHandler.class)
})
insert modification
@Update({
"<script> ",
"update video_basic_info ",
"<set> ",
"<if test = \"Title != null and Title != '' \"> ",
"title=#{Title}, ",
"</if> ",
"<if test = \"Desc != null and Desc != '' \"> ",
"description=#{Desc}, ",
"</if> ",
"<if test = \"Tag != null and Tag.size() > 0 \"> ",
"category_list=#{Tag,javaType=List,jdbcType=VARCHAR,typeHandler=com.aliyun.kuxuandemo.typeHandler.ArrayTypeHandler} ",
"</if> ",
"</set> ",
"where video_id=#{VideoID} and corp_id=#{CorpID}",
"</script>"
})
int updateVideoBasicInfoTitleDescTag(BaseRequestParameterValidationModel parameter);
div>
Read More:
- Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing a block collection
- Error handling after mybatis custom paging plug-in
- The jar package download of Maven project appears (could not transfer artifact. Org mybatis:mybatis )
- Error: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2
- com.alibaba.druid.sql.parser.ParserException: syntax error
- boot.asm:1:error:parser:instruction expected
- Failed to retrieve plugin descriptor for org.mybatis.generator :mybatis-generator
- Cannot find module ‘body-parser’
- ASP.NET Parser Error Message: Could not load type ‘Web.Global’.
- Oracle error collection solution
- Python random selects elements randomly from a collection
- Java garbage collection
- Mapped Statements collection does not contain value for XXXX
- Struts 2 encapsulates form data into list and map sets
- Related configuration of mybatis project
- Conversion between list and string array
- Design of MQ asynchronous collection report data
- Dataframe groupby custom aggregate function
- How to Add custom middleware for GRPC server
- E11000 duplicate key error collection in mongodb