Mybatis

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.longlong.pojo
mybatis.type-handlers-package=com.longlong.typehandler
logging.level.org.mybatis=DEBUG



    
@Repository
public interface MyBatisUserDao {
    User getUser(Long id);
}
@SpringBootApplication(scanBasePackages = {"com.longlong"})
@MapperScan(
      basePackages = "com.longlong.*",
      annotationClass = Repository.class
)
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}
@Component
@Alias(value = "user")
public class User {

    private Long id;

    private String userName;

    private String note;

    private SexEnum sex;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public SexEnum getSex() {
        return sex;
    }

    public void setSex(SexEnum sex) {
        this.sex = sex;
    }
}
@MappedJdbcTypes({JdbcType.INTEGER})
@MappedTypes(value = SexEnum.class)
public class SexTypeHandler extends BaseTypeHandler{
    @Override
    public SexEnum getNullableResult(ResultSet resultSet, String col) throws SQLException {
        int sex = resultSet.getInt(col);
        if (sex != 1 && sex != 2) {
            return null;
        }
        return SexEnum.getEnumById(sex);
    }
    @Override
    public SexEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int sex = resultSet.getInt(i);
        if (sex != 1 && sex != 2) {
            return null;
        }
        return SexEnum.getEnumById(sex);
    }

    @Override
    public SexEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int sex = callableStatement.getInt(i);
        if (sex != 1 && sex != 2) {
            return null;
        }
        return SexEnum.getEnumById(sex);
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, SexEnum sexEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, sexEnum.getId());
    }
}

你可能感兴趣的:(Mybatis)