MyBatis中使用@Results注解来映射查询结果集到实体类属性。
(1)@Results的基本用法。当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。
(2)连表查询时使用Results注解可以查询连接的表的数据
@Select("select menu.id, menu.parent_id,menu.name,menu.url,menu.icon,menu.sort,menu.description,\n" +
"menu_vs_role.role_id,role.name,user_vs_role.user_id \n" +
"from menu \n" +
"left join menu_vs_role on menu.id=menu_vs_role.menu_id \n" +
"left join role on menu_vs_role.role_id=role.id \n" +
"left join user_vs_role on role.id=user_vs_role.role_id where user_vs_role.user_id=#{user_id}")
@Results({
@Result(column = "id",property = "id", jdbcType= JdbcType.INTEGER, id=true),
@Result(column = "parent_id",property = "parent_id"),
@Result(column = "name",property = "name"),
@Result(column = "url",property = "url"),
@Result(column = "sort",property = "sort"),
@Result(column = "description",property = "description"),
@Result(column = "role_id",property = "menu_vs_role.role_id"), // 可以实现连表查询menu_vs_role的数据
@Result(column = "name",property = "role.name"),
@Result(column = "user_id",property = "user_vs_role.user_id")
})
List<Menu> findMenuListByUserId(int user_id);