001/* 002 * Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.handler; 017 018import org.apache.ibatis.type.BaseTypeHandler; 019import org.apache.ibatis.type.JdbcType; 020 021import java.sql.CallableStatement; 022import java.sql.PreparedStatement; 023import java.sql.ResultSet; 024import java.sql.SQLException; 025import java.util.Arrays; 026import java.util.List; 027 028 029/** 030 * @author michael 031 */ 032public class CommaSplitTypeHandler extends BaseTypeHandler<List<String>> { 033 034 @Override 035 public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException { 036 if (parameter != null) { 037 ps.setString(i, String.join(",", parameter)); 038 } else { 039 ps.setNull(i, jdbcType.TYPE_CODE); 040 } 041 } 042 043 @Override 044 public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException { 045 return splitByComma(rs.getString(columnName)); 046 } 047 048 @Override 049 public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 050 return splitByComma(rs.getString(columnIndex)); 051 } 052 053 @Override 054 public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 055 return splitByComma(cs.getString(columnIndex)); 056 } 057 058 private List<String> splitByComma(String string) { 059 if (string == null || string.isEmpty()) { 060 return null; 061 } 062 return Arrays.asList(string.split(",")); 063 } 064 065}