SpringBoot

java MyBatis <-> mssql bulk 삽입 (테이블 반환 매개 변수 사용 SQLServerDataTable )

Xmobile 2023. 1. 23. 08:11
BaseTypeHandler를 확장하여  사용자 정의 테이블에 대응되는 객체를 만들어 줍니다.
 
Handler
public class TypeUpDataVibHandler extends BaseTypeHandler<List<TypeUpDataVibVo>>{ 
	  @Override 
	  public void setNonNullParameter(PreparedStatement ps, 
	      int i, List<TypeUpDataVibVo> parameter, JdbcType jdbcType) 
	      throws SQLException { 
	    SQLServerDataTable tvp = new SQLServerDataTable(); 

        tvp.addColumnMetadata("C1" ,java.sql.Types.NVARCHAR); 
        tvp.addColumnMetadata("C2" ,java.sql.Types.NVARCHAR); 
        tvp.addColumnMetadata("C3" ,java.sql.Types.NVARCHAR); 
        tvp.addColumnMetadata("C4" ,java.sql.Types.NVARCHAR); 
        tvp.addColumnMetadata("C5" ,java.sql.Types.NVARCHAR); 
        tvp.addColumnMetadata("C6" ,java.sql.Types.NVARCHAR); 

	    for (TypeUpDataVibVo param : parameter) { 
	    	tvp.addRow( 

	    			param.getC1(), 
	    			param.getC2(), 
	    			param.getC3(), 
	    			param.getC4(), 
	    			param.getC5(), 
	    			param.getC6()
				); 
	    } 
	    ps.unwrap(SQLServerPreparedStatement.class).setStructured(i, "TYPE_UP_DATA_VIB_BULK", tvp); 
	  } 
	  @Override 
	  public List<TypeUpDataVibVo> getNullableResult(ResultSet rs, String columnName) throws SQLException { 
	    return null; 
	  } 
	  @Override 
	  public List<TypeUpDataVibVo> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 
	    return null; 
	  } 
	  @Override 
	  public List<TypeUpDataVibVo> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 
	    return null; 
	  } 
}
 

VO

public class TypeUpDataVibVo { 
	String C1; 
	String C2; 
	String C3; 
	String C4; 
	String C5; 
	String C6; 


	public String getC1() { 
		return C1; 
	} 
	public void setC1(String c1) { 
		C1 = c1; 
	} 
	public String getC2() { 
		return C2; 
	} 
	public void setC2(String c2) { 
		C2 = c2; 
	} 
	public String getC3() { 
		return C3; 
	} 
	public void setC3(String c3) { 
		C3 = c3; 
	} 
	public String getC4() { 
		return C4; 
	} 
	public void setC4(String c4) { 
		C4 = c4; 
	} 
	public String getC5() { 
		return C5; 
	} 
	public void setC5(String c5) { 
		C5 = c5; 
	} 
	public String getC6() { 
		return C6; 
	} 
	public void setC6(String c6) { 
		C6 = c6; 
	} 
}

 

xml 

<select id="saveUpload_bulk" statementType="CALLABLE" resultType="hashMap"> 
{ 
    call P_BULK(#{list, typeHandler=kr.common.data.TypeUpDataVibHandler}) 
} 
</select>
 
service 
@Override 
public void saveUpload_bulk(List<TypeUpDataVibVo> list) throws SQLServerException { 
    tsMapper.saveUpload_bulk(list); 
}