标签:day09 name 查询 mybatis student Mybatis id select
多对一处理
一、查询嵌套查询
<!-- 1、查询所有的学生信息 2、根据查询出来的学生的tid,查询对应的老师 --> <select id="getStudent" resultMap="StudentTeacher"> select * from mybatis.student </select> <resultMap id="StudentTeacher" type="stu"> <result property="id" column="id"/> <result property="name" column="name"/> <!-- 复杂的部分需要单独处理 对象:association 集合:collection --> <!-- peoperty是实体类中的属性;tid是对应的数据库中的属性; javaType是查询结果的实体类;select是嵌套查询的sql语句的名称--> <association property="teacher" column="tid" javaType="tea" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="tea"> select * from mybatis.teacher where id = #{id} </select>
二、按照查询结果查询
<!--按照查询结构嵌套--> <select id="getStudent2" resultMap="StudentTea"> select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id; </select> <resultMap id="StudentTea" type="stu"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="tea"> <result property="id" column="id"/> <result property="name" column="tname"/> </association> </resultMap>
标签:day09,name,查询,mybatis,student,Mybatis,id,select 来源: https://www.cnblogs.com/nlyide/p/15844378.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。