MyBatis——[注解方式]一对多查询DEMO
生活随笔
收集整理的這篇文章主要介紹了
MyBatis——[注解方式]一对多查询DEMO
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
API
@Select: 類似于<select>
@Insert: 類似于<insert>
@Update: 類似于<update>
@Delete: 類似于<delete>?
@Results: 類似于<resultMap>
@Result: 類似于<resultMap>的子標簽
@One: 類似于<association>
@Many: 類似于<collection>
源代碼
DEMO ONE?
public interface UserDao {/*** 用戶的持久層接口* @return*/@Select("select * from user ")@Results({@Result(id=true,property = "id",column = "id"),@Result(property = "name" ,column = "name"),@Result(property = "positions",column ="id" ,javaType = List.class,many=@Many(select="cn.itcast.dao.PositionDao.findById",fetchType= FetchType.LAZY))})List<User> findAll();public interface PositionDao {@Select("select * from position p,(select pid from u_p where uid=#{id}) u where p.id in(u.pid)")@Results({@Result(id=true,property = "id",column = "id"),@Result(property = "name" ,column = "name"),@Result(property = "permissions",column ="id" ,javaType = List.class,many=@Many(select="cn.itcast.dao.PermissionDao.findById",fetchType= FetchType.LAZY))})List<Position> findById(); } public interface PermissionDao {@Select("select * from permission p,(select prid from ps_pr where psid=#{id}) u where p.id in (u.prid) ")@Results({@Result(id=true,property = "id",column = "id"),@Result(property = "name" ,column = "name")})List<Permission> findById(); }DEMO TWO
public interface ResourceMapper {@Select("SELECT * FROM resource")@Results(value = {@Result(property = "id",column = "id",id = true),@Result(property = "pattern",column = "pattern"),@Result(property = "roles",column = "id",javaType = List.class,many = @Many(select = "club.zstuca.myzstu.mapper.RoleMapper.loadRolesByResourceId",fetchType = FetchType.LAZY))})List<Resource> getAllResource(); } public interface RoleMapper {@Select("SELECT * FROM role WHERE id=#{id}")Role loadRolesById(String id);@Select("SELECT * FROM resource_role rr LEFT JOIN role r ON rr.rid=r.id WHERE rr.sid=#{resourceId}")List<Role> loadRolesByResourceId(String resourceId); }參考文章
https://blog.csdn.net/desert568/article/details/79079151
http://bbs.itheima.com/thread-440927-1-1.html
https://zhuanlan.zhihu.com/p/66919945
https://blog.csdn.net/qq_42524262/article/details/97664924
總結
以上是生活随笔為你收集整理的MyBatis——[注解方式]一对多查询DEMO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 10——连接鼠标时触摸板
- 下一篇: 播放[.m3u8]链接解决方案