增加List类型转换成Varchar工具类(有bug)
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -50,18 +50,23 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 阿里云相关依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
<version>3.17.4</version>
|
<version>3.17.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun</groupId>
|
<groupId>com.aliyun</groupId>
|
||||||
<artifactId>green20220302</artifactId>
|
<artifactId>green20220302</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.bao.dating.handler;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||||
|
import org.apache.ibatis.type.MappedTypes;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List类型转换成Varchar类型
|
||||||
|
* @author KilLze
|
||||||
|
*/
|
||||||
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||||
|
@MappedTypes(List.class)
|
||||||
|
public class ListToVarcharTypeHandler implements TypeHandler<List<String>> {
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
|
||||||
|
// 遍历List类型的入参,拼装为String类型,使用Statement对象插入数据库
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int j = 0; j < strings.size(); j++) {
|
||||||
|
if (j == strings.size() - 1) {
|
||||||
|
sb.append(strings.get(j));
|
||||||
|
} else {
|
||||||
|
sb.append(strings.get(j)).append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preparedStatement.setString(i, sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = resultSet.getString(s);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = resultSet.getString(i);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
|
||||||
|
// 获取String类型的结果,使用","分割为List后返回
|
||||||
|
String resultString = callableStatement.getString(i);
|
||||||
|
if (StringUtils.isNotEmpty(resultString)) {
|
||||||
|
return Arrays.asList(resultString.split(","));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
url: jdbc:mysql://114.55.250.24:3306/dating?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
|
url: jdbc:mysql://114.55.250.24:3306/dating?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
|
||||||
username: root
|
username: root
|
||||||
password: root
|
password: rJ6DBTYrFCpjdsxy2sBV
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
# MyBatis 配置
|
# MyBatis 配置
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
|
||||||
|
|
||||||
|
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
||||||
<!-- 动态添加 -->
|
<!-- 动态添加 -->
|
||||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||||
INSERT INTO post
|
INSERT INTO post
|
||||||
@@ -16,7 +16,10 @@
|
|||||||
</if>
|
</if>
|
||||||
is_public, like_count, favorite_count, created_at, updated_at)
|
is_public, like_count, favorite_count, created_at, updated_at)
|
||||||
VALUES
|
VALUES
|
||||||
(#{userId}, #{content}, #{mediaOssKeys}, #{tags}, #{location}, #{isPublic}, 0, 0, #{created_at}, #{updated_at})
|
(#{userId}, #{content},
|
||||||
|
#{mediaOssKeys, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
|
#{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
|
#{location}, #{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user