mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add CommaSplitTypeHandler.java
This commit is contained in:
parent
135e0d9a81
commit
7bf7e4d103
@ -15,8 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.handler;
|
package com.mybatisflex.core.handler;
|
||||||
|
|
||||||
|
import org.apache.ibatis.type.BaseTypeHandler;
|
||||||
import org.apache.ibatis.type.JdbcType;
|
import org.apache.ibatis.type.JdbcType;
|
||||||
import org.apache.ibatis.type.TypeHandler;
|
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
import java.sql.CallableStatement;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
@ -24,48 +24,42 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.StringJoiner;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class CommaSplitTypeHandler implements TypeHandler<List<String>> {
|
public class CommaSplitTypeHandler extends BaseTypeHandler<List<String>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
|
||||||
if (parameter != null) {
|
if (parameter != null) {
|
||||||
StringJoiner stringJoiner = new StringJoiner(",");
|
ps.setString(i, String.join(",", parameter));
|
||||||
parameter.forEach(stringJoiner::add);
|
} else {
|
||||||
ps.setString(i, stringJoiner.toString());
|
ps.setNull(i, jdbcType.TYPE_CODE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
|
public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
String columnValueStr = rs.getString(columnName);
|
return splitByComma(rs.getString(columnName));
|
||||||
if (columnValueStr != null) {
|
|
||||||
return Arrays.asList(columnValueStr.split(","));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
|
public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
String columnValueStr = rs.getString(columnIndex);
|
return splitByComma(rs.getString(columnIndex));
|
||||||
if (columnValueStr != null) {
|
|
||||||
return Arrays.asList(columnValueStr.split(","));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
String columnValueStr = cs.getString(columnIndex);
|
return splitByComma(cs.getString(columnIndex));
|
||||||
if (columnValueStr != null) {
|
|
||||||
return Arrays.asList(columnValueStr.split(","));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> splitByComma(String string) {
|
||||||
|
if (string == null || string.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return Arrays.asList(string.split(","));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user