MyBatis注解对应关系
Posted HC_Manage&
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis注解对应关系相关的知识,希望对你有一定的参考价值。
MyBatis注解对应关系
# crud
# 名称不一致
@Select("select * from book where id = #id")
@Results(
@Result(property="id",column="id",id=true),
@Result(property="name",column="b_name"),
@Result(property="createTime",column="b_create_time"),
@Result(property="price",column="price")
)
public Book findById(int id);
# 一对一
@Select("select * from chapter")
@Results(
@Result(property = "id", column = "id",id=true),
@Result(property = "name", column = "name"),
@Result(property = "bookId", column = "book_id"),
@Result(property = "book",column="book_id",one = @One(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.BookMapper.findById"))
)
public List<Chapter> findAll();
# 一对多
@Select("select * from book")
//解决数据库和java属性不一致
//property:java属性
//column:数据库属性
//id:判断是否是主键
@Results(
@Result(property="id",column="id",id=true),
@Result(property="name",column="b_name"),
@Result(property="createTime",column="b_create_time"),
@Result(property="price",column="price"),
@Result(property="chapters",column="id",many=@Many(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.ChapterMapper.findByBookId"))
)
public List<Book> findAll();
# 多对多
@Select("select * from user")
@Results(
@Result(property="id",column="id",id=true),
@Result(property="roles",column="id",many=@Many(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.RoleMapper.findByUserId"))
)
public List<User> findUserAndRole();
@Select("SELECT * from role r INNER JOIN user_role ur ON r.id = ur.role_id where ur.user_id = #userId")
public List<Role> findByUserId(int userId);
以上是关于MyBatis注解对应关系的主要内容,如果未能解决你的问题,请参考以下文章