mybatis怎么连表查询?新手!!

yang19921994 2016-03-19 10:57:02
mybatis怎么连表查询?两个表中的字段是不是要写在一个实体里面?还是分开写?Mapper.xml中怎么写?用collection?还是其他的?
...全文
703 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yaphets_VJ 2016-11-04
  • 打赏
  • 举报
回复
引用 18 楼 x18094 的回复:
[quote=引用 14 楼 yang19921994 的回复:] 谢谢大家,问题已解决,和我之前的思路差不多,嘿嘿
楼主,怎么写的贴出来? 自己解决了却藏着,哼,宝宝不开心[/quo 真可爱
编程周记 2016-11-04
  • 打赏
  • 举报
回复
引用 14 楼 yang19921994 的回复:
谢谢大家,问题已解决,和我之前的思路差不多,嘿嘿
楼主,怎么写的贴出来? 自己解决了却藏着,哼,宝宝不开心
wl89119221 2016-03-29
  • 打赏
  • 举报
回复
<select id="selectBlogDetails" parameterType="int" resultMap="detailedBlogResultMap"> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id, P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id, T.name as tag_name from Blog B left outer join Author A on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = #{id} </select> 你可能想要把它映射到一个智能的对象模型,包括由一个作者写的一个博客,由多项交互,由0个或者多个评论和标签。下面是一个复杂ResultMap 的完整例子,(假定作者、博客、评论和标签都是别名)。 仔细看看这个例子,但是不用太担心,我们会一步步地来。跃然乍看让人沮丧,但是实际上是很简单的。 <!-- Very Complex Result Map --> <resultMap id="detailedBlogResultMap" type="Blog"> <constructor> <idArg column="blog_id" javaType="int"/> </constructor> <result property="title" column="blog_title"/> <association property="author" column="blog_author_id" javaType="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> <result property="favouriteSection" column="author_favourite_section"/> </association> <collection property="posts" ofType="Post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <association property="author" column="post_author_id" javaType="Author"/> <collection property="comments" column="post_id" ofType=" Comment"> <id property="id" column="comment_id"/> </collection> <collection property="tags" column="post_id" ofType=" Tag" > <id property="id" column="tag_id"/> </collection> <discriminator javaType="int" column="draft"> <case value="1" resultType="DraftPost"/> </discriminator> </collection> </resultMap>
回首已无路 2016-03-28
  • 打赏
  • 举报
回复
貌似我用很少写语句,都是关联查询!
yang19921994 2016-03-27
  • 打赏
  • 举报
回复
谢谢大家,问题已解决,和我之前的思路差不多,嘿嘿
「已注销」 2016-03-23
  • 打赏
  • 举报
回复
sql语句该怎么写就怎么写,不需要返回实体类,返回map就行了
yang19921994 2016-03-23
  • 打赏
  • 举报
回复
引用 10 楼 hash_set 的回复:

<select id=" 和调用接口文件中的方法名一样" resultMap="BaseResultMap" >
    A.age,B.name
    from tablea A
    inner join tableb B  on A.name(A表关联条件)   =  B.name(B表关联条件)
    <where>
    <if test="address != null">
  	 .....      //判断条件
    </if>
  </where>
  </select>
xml中resultMap要把两个表关联起来吗?
hash_set 2016-03-23
  • 打赏
  • 举报
回复
引用 11 楼 yang19921994 的回复:
[quote=引用 10 楼 hash_set 的回复:]

<select id=" 和调用接口文件中的方法名一样" resultMap="BaseResultMap" >
    A.age,B.name
    from tablea A
    inner join tableb B  on A.name(A表关联条件)   =  B.name(B表关联条件)
    <where>
    <if test="address != null">
  	 .....      //判断条件
    </if>
  </where>
  </select>
xml中resultMap要把两个表关联起来吗?[/quote] 严格来说resultMap中定义的是返回的结果集,使用resultMap方式返回时你想查询返回什么内容就要在这个结果集里定义,拿本例来说,我需要链表查询获取a表中的age信息和b表中的name信息,那么:

   <resultMap id="BaseResultMap" type="待封入对象model的完整路径名称" >
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="DECIMAL" />
1.column对应数据库表字段名称,property对应java属性名称,jdbcType对应数据类型。 2.定义中不区分该数据来自a表还是b表,只根据column指定的数据名来进行匹配,所以链表查询的时候要防止获取的数据重名
hash_set 2016-03-21
  • 打赏
  • 举报
回复

<select id=" 和调用接口文件中的方法名一样" resultMap="BaseResultMap" >
    A.age,B.name
    from tablea A
    inner join tableb B  on A.name(A表关联条件)   =  B.name(B表关联条件)
    <where>
    <if test="address != null">
  	 .....      //判断条件
    </if>
  </where>
  </select>
supaipai 2016-03-21
  • 打赏
  • 举报
回复
<select id="find" resultType="map"> select * from a join b; </select> 大概这样?
yang19921994 2016-03-21
  • 打赏
  • 举报
回复
引用 6 楼 fengspg 的回复:
返回map不就行了嘛
求实例。。。大神
yang19921994 2016-03-21
  • 打赏
  • 举报
回复
引用 5 楼 rentian1 的回复:
直接让拼写连接sql语句,在mapping.xml文件里面的selsect语句设置返回的值到一个Map集合就行,就可以不写实体类。
求实例啊。。。新手不会,谢谢!!
Intboy 2016-03-21
  • 打赏
  • 举报
回复
返回map不就行了嘛
修炼到救赎 2016-03-21
  • 打赏
  • 举报
回复
直接让拼写连接sql语句,在mapping.xml文件里面的selsect语句设置返回的值到一个Map集合就行,就可以不写实体类。
yang19921994 2016-03-21
  • 打赏
  • 举报
回复
引用 1 楼 rumlee 的回复:
mybatis只是做了o/r转换,sql语句该怎么写还怎么写啊。
求解释什么是o/r转换啊?谢谢大神
yang19921994 2016-03-21
  • 打赏
  • 举报
回复
引用 2 楼 Baoge_leopard 的回复:
面向对象,实体bean映射就行
把两个表映射在一个xml里面吗?
默伊清风 2016-03-20
  • 打赏
  • 举报
回复
面向对象,实体bean映射就行
rumlee 2016-03-19
  • 打赏
  • 举报
回复
mybatis只是做了o/r转换,sql语句该怎么写还怎么写啊。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧