mybaits级联查询

mybaits级联查询

mybatis怎么多表查询,在一对多的情况下

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.bbs.dao.PostDao">
    <resultMap id="post" type="Post">
        <id property="id" column="id" javaType="Integer"/>
        <result property="username" column="username" javaType="String"/>
        <result property="title" column="title" javaType="String"/>
        <result property="category" column="category" javaType="String"/>
        <result property="content" column="content" javaType="String"/>
        <result property="tag" column="tag" javaType="String"/>
        <result property="createTime" column="create_time" javaType="java.util.Date"/>

        <collection property="comment" column="id" javaType="Integer">
            <id property="id" column="id" javaType="Integer"/>
            <result property="userId" column="user_id" javaType="Integer"/>
            <result property="username" column="username" javaType="String"/>
            <result property="content" column="content" javaType="String"/>
            <result property="createTime" column="create_time" javaType="java.util.Date"/>
            <result property="postId" column="post_id" javaType="Integer"/>
        </collection>
    </resultMap>
     <select id="selectById" parameterType="Integer" resultMap="post">
        select p.*,c.username name,c.content con from post p
        left join comment c on p.id = c.post_id
        where p.id=#{id}
    </select>
</mapper>

在查post表的时候一起把comment表的数据查出来,在post表中定义了一个comment的list集合对象。comment表中有一个post_id的字段对应post表中的id。弄了好久都没搞好,只能来求助老师了。

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿满 2020-05-19 10:06:52

同学你好,报错提示,在结果集中获取“username”列的值时出错,如:

http://img1.sycdn.imooc.com//climg/5ec33c3c0957bea307950306.jpgusername是String类型值,但是获取时,使用getInt()方式。建议同学查看一下获取结果时,是否使用int方法获取username值。

祝:学习愉快~

好帮手慕阿满 2020-05-18 19:28:13

同学你好,同学的sql语句没有问题,问一下同学执行时报错吗?请将具体报错贴一下。

祝:学习愉快~

  • 提问者 cc啊 #1
    Error attempting to get column 'username' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'imooc1' ; Invalid value for getInt() - 'imooc1'; nested exception is java.sql.SQLException: Invalid value for getInt() - 'imooc1' java.sql.SQLException: Invalid value for getInt() - 'imooc1' 这是当时的报错内容
    2020-05-18 19:30:38
  • 提问者 cc啊 #2
    Error attempting to get column 'username' from result set. Cause: java.sql.SQLException: Invalid value for getInt() - 'imooc1' ; Invalid value for getInt() - 'imooc1'; nested exception is java.sql.SQLException: Invalid value for getInt() - 'imooc1' java.sql.SQLException: Invalid value for getInt() - 'imooc1' 这是当时的报错内容
    2020-05-18 19:31:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师