注解@Results用法总结.pdfVIP

  • 19
  • 0
  • 约3.62千字
  • 约 2页
  • 2023-08-10 发布于上海
  • 举报
注解@Results⽤法总结 MyBatis中使⽤@Results注解来映射查询结果集到实体类属性。 (1)@Results的基本⽤法。当数据库字段名与实体类对应的属性名不⼀致时,可以使⽤@Results映射来将其对应起来。column为数据 库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。 @Select(select * from orders where id=#{id}) @Results({ @Result(id=true,column = id,property = id), @Result(column = orderNum,property = orderNum), @Result(column = orderTime,property = orderTime), @Result(column = orderStatus,property = orderStatus), @Result(column = peopleCount,property = peopleCount), @Result(column = payType,property = payType), @Result(column = orderDesc,property = orderDesc) }) Orders findById(String id) throws Exception; (2)@ResultMap的⽤法。当这段@Results代码需要在多个⽅法⽤到时,为了提⾼代码复⽤性,我们可以为这个@Results注解设置id, 然后使⽤@ResultMap注解来复⽤这段代码。 @Select({select id, name, class_id from my_student}) @Results(id=studentMap, value={ @Result(column=id, property=id, jdbcType=JdbcType.INTEGER, id=true), @Result(column=name, property=name, jdbcType=JdbcType.VARCHAR), @Result(column=class_id , property=classId, jdbcType=JdbcType.INTEGER) }) ListStudent selectAll(); @Select({select id, name, class_id from my_student where id = #{id}}) @ResultMap(value=studentMap) Student selectById(integer id); (3)@One的⽤法。当我们需要通过查询到的⼀个字段值作为参数,去执⾏另外⼀个⽅法来查询关联的内容,⽽且两者是⼀对⼀关系时, 可以使⽤@One注解来便捷的实现。其中column对应的值在传⼊select时,不管是设置为memberId还是id都可以传⼊使⽤,建议设置为 memberId,提⾼可读性。 @Select(select * from orders where id=#{id}) @Results({ @Result(id=true,column = id,property = id), @Result(column = orderNum,property = orderNum), @Result(column = orderTime,property = orderTime), @Result(column = orderStatus,property = orderStatus), @Result(column = peopleCount,property = peopleCount), @Result(column = payType,property = payType), @Result(column =

文档评论(0)

1亿VIP精品文档

相关文档