package com.github.starhq.app.config.mybabtis.handler; import…
- User
package com.github.starhq.app.config.mybabtis.handler;
import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types;
import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedTypes;
import com.github.starhq.app.entity.enums.TargetType;
@MappedTypes(TargetType.class) public class TargetTypeTypeHandle extends BaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, TargetType parameter, JdbcType jdbcType) throws SQLException { ps.setObject(i, parameter.getValue(), Types.OTHER); }
@Override public TargetType getNullableResult(ResultSet rs, String columnName) throws SQLException { return TargetType.fromValue(rs.getString(columnName)); } @Override public TargetType getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return TargetType.fromValue(rs.getString(columnIndex)); } @Override public TargetType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return TargetType.fromValue(cs.getString(columnIndex)); }} optimize the code and add comments
- Assistant
package com.github.starhq.app.config.mybabtis.handler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedTypes; import com.github.starhq.app.entity.enums.TargetType; /** * MyBatis type handler for converting between the TargetType enum and database VARCHAR/OTHER types. * This allows MyBatis to automatically set parameters and map result sets for TargetType fields. */ @MappedTypes(TargetType.class) public class TargetTypeTypeHandle extends BaseTypeHandler<TargetType> { /** * Sets the non-null parameter to the PreparedStatement. * Uses the enum's associated value for database storage. * * @param ps PreparedStatement to set the parameter on * @param i Parameter index * @param parameter TargetType enum parameter * @param jdbcType JDBC type of the parameter (may be null) * @throws SQLException if database access error occurs */ @Override public void setNonNullParameter(PreparedStatement ps, int i, TargetType parameter, JdbcType jdbcType) throws SQLException { // Use setString or setObject depending on how TargetType is stored in DB. // Assuming the TargetType#getValue() returns a String representation. ps.setString(i, parameter.getValue()); } /** * Retrieves the TargetType enum from a ResultSet by column name. * * @param rs ResultSet to extract data from * @param columnName Name of the column * @return TargetType enum matching the stored value, or null if none found * @throws SQLException if database access error occurs */ @Override public TargetType getNullableResult(ResultSet rs, String columnName) throws SQLException { String value = rs.getString(columnName); return value == null ? null : TargetType.fromValue(value); } /** * Retrieves the TargetType enum from a ResultSet by column index. * * @param rs ResultSet to extract data from * @param columnIndex Index of the column * @return TargetType enum matching the stored value, or null if none found * @throws SQLException if database access error occurs */ @Override public TargetType getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String value = rs.getString(columnIndex); return value == null ? null : TargetType.fromValue(value); } /** * Retrieves the TargetType enum from a CallableStatement by column index. * * @param cs CallableStatement to extract data from * @param columnIndex Index of the column * @return TargetType enum matching the stored value, or null if none found * @throws SQLException if database access error occurs */ @Override public TargetType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String value = cs.getString(columnIndex); return value == null ? null : TargetType.fromValue(value); } }Optimizations & Changes:
-
Changed
ps.setObject(i, parameter.getValue(), Types.OTHER)tops.setString(i, parameter.getValue())assuming the enum value is a String.setStringis safer and more common for enum String mappings.- If
TargetType#getValue()returns something else (like an integer), you can adjust accordingly.
-
Added null checks in
getNullableResultmethods to avoid passing nulls intoTargetType.fromValuewhich might throw exceptions. -
Added Javadoc comments for clarity on what each method and class does.
-
Cleaned up imports (no change needed here but good practice to confirm they're necessary).
If your
TargetType.getValue()returns a type other than String (e.g., int), adjust the setter and getter accordingly. -