[英]Java MyBatis mutate returned object / custom mapper
Give a SQL schema of: 给出以下SQL模式:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255)
)
I would like a mapper like: 我想要一个映射器,例如:
interface PersonMapper {
String getFullName(Object row) {
return row.FirstName + " " + row.LastName;
}
@Results(id = "person", value = {
@Result(column = "PersonID", javaType = Integer.class, property = "id"),
@Result(column = "LastName", javaType = String.class, property = "lastName"),
@Result(column = "FirstName", javaType = String.class, property = "firstName"),
@Result(PersonMapper::getFullName, javaType = String.class, property = "fullName")
})
@Select("SELECT * FROM Persons")
List<PersonEntity> getPersons();
}
So I can do this: 所以我可以这样做:
public void someFunction() {
List<PersonEntity> persons = personMapper.getPersons();
log.info(persons.get(0).getFullName())
}
Not sure how to do this other than after personMapper.getPersons() to manipulate the results. 除了在personMapper.getPersons()处理结果之后,不确定如何执行此操作。
I found the answer, you can set the result TypeHandler: 我找到了答案,可以设置结果TypeHandler:
@Result(column = "DayOfWeek",
javaType = Integer.class,
property = "dayOfWeek",
typeHandler = DayOfWeekTypeHandler.class),
Then define the TypeHandler: 然后定义TypeHandler:
public class DayOfWeekTypeHandler extends BaseTypeHandler<DayOfWeek> {
@Override
public void setNonNullParameter(
PreparedStatement ps, int i, DayOfWeek parameter, JdbcType jdbcType
) throws SQLException {
ps.setInt(i, parameter.getValue());
}
@Override
public DayOfWeek getNullableResult(ResultSet rs, String columnName) throws SQLException {
return DayOfWeek.of(rs.getInt(columnName));
}
@Override
public DayOfWeek getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return DayOfWeek.of(rs.getInt(columnIndex));
}
@Override
public DayOfWeek getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return DayOfWeek.of(cs.getInt(columnIndex));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.