Код: Выделить всё
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
public class ExpiryDate {
@Max(9999)
private int year;
@Max(12)
private int month;
public ExpiryDate() {
......
}
public ExpiryDate(final Integer year, final Integer month) {
.....
}
... getter/setter
@Override
public boolean equals(final Object obj) {
.....
}
@Override
public int hashCode() {
.....
}
}
Код: Выделить всё
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
@MappedTypes(value = ExpiryDate.class)
public class ExpiredDateHandler extends BaseTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, ExpiryDate parameter, JdbcType jdbcType)
throws SQLException {
ps.setDate(i, this.convert(parameter));
}
@Override
public ExpiryDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.convert(rs.getDate(columnName));
}
@Override
public ExpiryDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.convert(rs.getDate(columnIndex));
}
@Override
public ExpiryDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.convert(cs.getDate(columnIndex));
}
private java.sql.Date convert(final ExpiryDate expiryDate) {
return java.sql.Date.valueOf(expiryDate.getYear() + "-" + expiryDate.getMonth() + "-01");
}
private ExpiryDate convert(final java.sql.Date value) {
if (value == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(value);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
return new ExpiryDate(year, month);
}
}
Код: Выделить всё
UPDATE users
SET expiry_date = #{pwdExpireDate, typeHandler=com.sql.ExpiredDateHandler},
WHERE ID = 123;
Подробнее здесь: https://stackoverflow.com/questions/790 ... -hibernate