Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

package com.github.starhq.app.config.mybabtis.handler; import…

  1. 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

  2. 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:

    1. Changed ps.setObject(i, parameter.getValue(), Types.OTHER) to ps.setString(i, parameter.getValue()) assuming the enum value is a String.

      • setString is safer and more common for enum String mappings.
      • If TargetType#getValue() returns something else (like an integer), you can adjust accordingly.
    2. Added null checks in getNullableResult methods to avoid passing nulls into TargetType.fromValue which might throw exceptions.

    3. Added Javadoc comments for clarity on what each method and class does.

    4. 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.