public class Member {
private String userId;
private String password;
private String userName;
private String gender;
private int age;
private String email;
private String phone;
private String address;
private String[] hobby; // 배열!
private Date enrollDate;
}
<resultMap id="memberMap" type="com.employee.model.vo.Member">
<id property="userId" column="userid"/>
<result property="password" column="password"/>
<result property="userName" column="username"/>
<result property="gender" column="gender"/>
<result property="age" column="age"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
<result property="hobby" column="hobby" typeHandler="strArrType"/> <!-- 타입핸들러 별칭으로 생성 -->
<result property="enrollDate" column="enrolldate"/>
</resultMap>
만드는 방법
- 2 : 인터페이스부분에 Add해서 TypeHandler를 선택한 후
- 3: 해당 T 자료형에 맞는 자료형을 넣는다
package com.employee.common;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
public class StringArrayTypeHandler implements TypeHandler<String[]> {
@Override
public String[] getResult(ResultSet rs, String columnName) throws SQLException {
// TODO Auto-generated method stub
return rs.getString(columnName)!=null?rs.getString(columnName).split(","):null;
}
@Override
public String[] getResult(ResultSet rs, int columnIndex) throws SQLException {
// TODO Auto-generated method stub
return rs.getString(columnIndex)!=null?rs.getString(columnIndex).split(","):null;
}
@Override
public String[] getResult(CallableStatement cs, int columnIndex) throws SQLException {
// TODO Auto-generated method stub
return cs.getString(columnIndex)!=null?cs.getString(columnIndex).split(","):null;
}
@Override
public void setParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
if(parameter!=null) {
ps.setString(i, String.join(",",parameter));
}else {
ps.setString(i, null);
}
}
}