MyBatis 3.5.4 发布于 2020年2月3日,其中最大的特点是:摒弃了繁琐的@Results和@ConstructorArgs注解。

MyBatis官网的说明是这样的:You can now omit unnecessary @Results and @ConstructorArgs annotation.

看似轻描淡写的一句话,却是对程序员双手的极大解放。不妨看下面的例子:

@Results在MyBatis < 3.5.4版本的写法是这样的:

@Results({
      @Result(property = "id", column = "AUTHOR_ID", id = true),
      @Result(property = "username", column = "AUTHOR_USERNAME"),
      @Result(property = "password", column = "AUTHOR_PASSWORD"),
      @Result(property = "email", column = "AUTHOR_EMAIL"),
      @Result(property = "bio", column = "AUTHOR_BIO")
  })
  @Select({
    "SELECT ",
    "  ID as AUTHOR_ID,",
    "  USERNAME as AUTHOR_USERNAME,",
    "  PASSWORD as AUTHOR_PASSWORD,",
    "  EMAIL as AUTHOR_EMAIL,",
    "  BIO as AUTHOR_BIO",
    "FROM AUTHOR WHERE ID = #{id}"})
  Author selectAuthor(int id);

@Results在MyBatis == 3.5.4版本已被取消:

  @Result(property = "id", column = "AUTHOR_ID", id = true)
  @Result(property = "username", column = "AUTHOR_USERNAME")
  @Result(property = "password", column = "AUTHOR_PASSWORD")
  @Result(property = "email", column = "AUTHOR_EMAIL")
  @Result(property = "bio", column = "AUTHOR_BIO")
  @Select({
    "SELECT ",
    "  ID as AUTHOR_ID,",
    "  USERNAME as AUTHOR_USERNAME,",
    "  PASSWORD as AUTHOR_PASSWORD,",
    "  EMAIL as AUTHOR_EMAIL,",
    "  BIO as AUTHOR_BIO",
    "FROM AUTHOR WHERE ID = #{id}"})
  Author selectAuthor(int id);

@ConstructorArgs在MyBatis < 3.5.4版本的写法是这样的:

  @ConstructorArgs({
      @Arg(column = "AUTHOR_ID", javaType = Integer.class, id = true),
      @Arg(column = "AUTHOR_USERNAME", javaType = String.class),
      @Arg(column = "AUTHOR_PASSWORD", javaType = String.class),
      @Arg(column = "AUTHOR_EMAIL", javaType = String.class),
      @Arg(column = "AUTHOR_BIO", javaType = String.class)
  })
  @Select({
    "SELECT ",
    "  ID as AUTHOR_ID,",
    "  USERNAME as AUTHOR_USERNAME,",
    "  PASSWORD as AUTHOR_PASSWORD,",
    "  EMAIL as AUTHOR_EMAIL,",
    "  BIO as AUTHOR_BIO" +
    "FROM AUTHOR WHERE ID = #{id}"})
  Author selectAuthor(int id);

@ConstructorArgs在MyBatis == 3.5.4版本已被取消:

  @Arg(column = "AUTHOR_ID", javaType = Integer.class, id = true)
  @Arg(column = "AUTHOR_USERNAME", javaType = String.class)
  @Arg(column = "AUTHOR_PASSWORD", javaType = String.class)
  @Arg(column = "AUTHOR_EMAIL", javaType = String.class)
  @Arg(column = "AUTHOR_BIO", javaType = String.class)
  @Select({
    "SELECT ",
    "  ID as AUTHOR_ID,",
    "  USERNAME as AUTHOR_USERNAME,",
    "  PASSWORD as AUTHOR_PASSWORD,",
    "  EMAIL as AUTHOR_EMAIL,",
    "  BIO as AUTHOR_BIO" +
    "FROM AUTHOR WHERE ID = #{id}"})
  Author selectAuthor(int id);

标签: none

添加新评论