请问各位大神,我的一个对象里面引用了两个相同的User对象,但是mybatis封装出来的像个对象值是一样的,求帮助

咖啡_加碘儿盐 2017-07-11 05:16:15
请问各位大神,我的一个对象里面引用了两个相同的User对象,但是mybatis封装出来的像个对象值是一样的,求帮助
我贴上代码,求各位帮忙修改下

SQL语句
<select id="selectWorksForUser" parameterType="com.zhuoxun.entity.Work" resultMap="workResultMap">
SELECT
c.work_id,
c.work_content,
c.work_demand,
c.work_create_time,
c.work_last_edit_time,
c.work_comp_cycle,
c.deadline,
c.work_leader_user_id,
c.work_join_emp,
c.work_progress,
c.work_percent,
c.notes,
c.user_id,
c.state,
b.user_id,
b.user_name,
b.role_id,
b.dept_id,
b.reg_time,
b.state,
a.user_id,
a.user_name as leader,
a.role_id,
a.dept_id,
a.reg_time,
a.state,
dept.dept_id,
dept.dept_name,
dept.dept_leder_name,
b.user_passwd,
role.role_id,
role.role_name
FROM
work c
INNER JOIN user_info a ON c.work_leader_user_id = a.user_id
INNER JOIN user_info b ON c.user_id = b.user_id
INNER JOIN dept ON b.dept_id = dept.dept_id
INNER JOIN role ON b.role_id = role.role_id
WHERE c.user_id >= #{user.user_id} OR c.work_leader_user_id = #{user_work_leader.user_id} AND c.state = #{state}
ORDER BY c.work_id
</select>


JAVA代码
public class Work {
private Integer work_id;
private String work_order;
private String work_content;
private String work_demand;
private Timestamp work_create_time;
private Timestamp work_last_edit_time;
private Integer work_comp_cycle;
private Timestamp deadline;
private User user_work_leader = new User(); //任务负责人
private String work_join_emp;
private String work_progress;
private Integer work_percent;
private String notes;
private User user = new User(); //任务发布者
private String image;
private Integer state;


Work work = new Work();
//0为未完成的任务
work.setState(0);
work.getUser_work_leader().setUser_id(5);
work.setUser(user);
PageHelper.offsetPage(startPage, pageSize);
works = workService.selectWorksForUser(work);


查出来的结果两个对象的值是一样的。如截图

...全文
343 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 8 楼 qq_35942223 的回复:
给其中的user字段起别名,例如 select id aId from table这样,你上面字段名都是一样的
我现在改成这种的了,但是还是不行。请大神再帮我看看 这是结果集
<resultMap id="workResultMap" type="com.zhuoxun.entity.Work">
        <id column="work_id" property="work_id"/>
        <result column="work_order" property="work_order"/>
        <result column="work_content" property="work_content"/>
        <result column="work_demand" property="work_demand"/>
        <result column="work_create_time" property="work_create_time"/>
        <result column="work_last_edit_time" property="work_last_edit_time"/>
        <result column="work_comp_cycle" property="work_comp_cycle"/>
        <result column="deadline" property="deadline"/>
        <result column="work_join_emp" property="work_join_emp"/>
        <result column="work_progress" property="work_progress"/>
        <result column="work_percent" property="work_percent"/>
        <result column="notes" property="notes"/>
        <result column="image" property="image"/>
        <result column="state" property="state"/>
        <association property="user_work_leader" javaType="com.zhuoxun.entity.User">
            <id column="a_user_id" property="user_id"/>
            <result column="a_user_name" property="user_name"/>
            <result column="a_user_passwd" property="user_passwd"/>
            <result column="a_reg_time" property="user_reg_time"/>
            <result column="a_state" property="user_state"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="a_role_id" property="role_id"/>
                <result column="a_role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="a_dept_id" property="dept_id"/>
                <result column="a_dept_name" property="dept_name"/>
                <result column="a_dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
        <association property="user" javaType="com.zhuoxun.entity.User">
            <id column="b_user_id" property="user_id"/>
            <result column="b_user_name" property="user_name"/>
            <result column="b_user_passwd" property="user_passwd"/>
            <result column="b_reg_time" property="user_reg_time"/>
            <result column="b_state" property="user_state"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="b_role_id" property="role_id"/>
                <result column="b_role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="b_dept_id" property="dept_id"/>
                <result column="b_dept_name" property="dept_name"/>
                <result column="b_dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
    </resultMap>
这是SQL语句
<select id="selectWorksForUser" parameterType="com.zhuoxun.entity.Work" resultMap="workResultMap">
        SELECT
            c.work_id,
            c.work_content,
            c.work_demand,
            c.work_create_time,
            c.work_last_edit_time,
            c.work_comp_cycle,
            c.deadline,
            c.work_leader_user_id,
            c.work_join_emp,
            c.work_progress,
            c.work_percent,
            c.notes,
            c.user_id,
            c.state,
            b.user_id b_user_id,
            b.user_name b_user_name,
            b.role_id b_role_id,
            b.dept_id b_dept_id,
            b.reg_time b_reg_time,
            b.state b_state,
            a.user_id a_user_id,
            a.user_name a_user_name,
            a.role_id a_role_id,
            a.dept_id a_dept_id,
            a.reg_time a_reg_time,
            a.state a_state,
            dept.dept_id,
            dept.dept_name,
            dept.dept_leder_name,
            b.user_passwd,
            role.role_id,
            role.role_name
        FROM
        work c
            INNER JOIN user_info a ON c.work_leader_user_id = a.user_id
            INNER JOIN user_info b ON c.user_id = b.user_id
            INNER JOIN dept ON b.dept_id = dept.dept_id
            INNER JOIN role ON b.role_id = role.role_id
        WHERE c.user_id >= #{user.user_id} OR c.work_leader_user_id = #{user_work_leader.user_id} AND c.state = #{state}
        ORDER BY c.work_id
    </select>
qq_35942223 2017-07-12
  • 打赏
  • 举报
回复
给其中的user字段起别名,例如 select id aId from table这样,你上面字段名都是一样的
  • 打赏
  • 举报
回复
引用 4 楼 baidu_38818116 的回复:
sql查询第二个user对象的属性的时候给他的字段起别名 例如: <mapper namespace="org.wzk.dao.StudentDao"> <resultMap type="org.wzk.pojo.Student" id="studentMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="hobby" property="hobby"/> <result column="clazz_id" property="clazz.id"/> <result column="clazz_cname" property="clazz.name"/> <collection property="list" ofType="org.wzk.pojo.Course"> <id column="cid" property="cid"/> <result column="course" property="course"/> <result column="ctime" property="ctime"/> </collection> </resultMap> <sql id="studentColumns">s.id,s.name,s.age,s.sex,s.hobby</sql> <sql id="courseColumns">c.cid,c.course,c.ctime</sql> <sql id="clazzColumns">clazz.id clazz_id,clazz.name clazz_cname</sql> 这样查询的的时候可以避免冲突
是直接在属性前面加类似于clazz.这种形式吗
  • 打赏
  • 举报
回复
引用 3 楼 qq_35942223 的回复:
两个user跟字段进行了映射没有
映射过了
<resultMap id="workResultMap" type="com.zhuoxun.entity.Work">
        <id column="work_id" property="work_id"/>
        <result column="work_order" property="work_order"/>
        <result column="work_content" property="work_content"/>
        <result column="work_demand" property="work_demand"/>
        <result column="work_create_time" property="work_create_time"/>
        <result column="work_last_edit_time" property="work_last_edit_time"/>
        <result column="work_comp_cycle" property="work_comp_cycle"/>
        <result column="deadline" property="deadline"/>
        <result column="work_join_emp" property="work_join_emp"/>
        <result column="work_progress" property="work_progress"/>
        <result column="work_percent" property="work_percent"/>
        <result column="notes" property="notes"/>
        <result column="image" property="image"/>
        <result column="state" property="state"/>
        <association property="user_work_leader" javaType="com.zhuoxun.entity.User">
            <id column="user_id" property="user_id"/>
            <result column="user_name" property="user_name"/>
            <result column="user_passwd" property="user_passwd"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="role_id" property="role_id"/>
                <result column="role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="dept_id" property="dept_id"/>
                <result column="dept_name" property="dept_name"/>
                <result column="dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
        <association property="user" javaType="com.zhuoxun.entity.User">
            <id column="user_id" property="user_id"/>
            <result column="user_name" property="user_name"/>
            <result column="user_passwd" property="user_passwd"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="role_id" property="role_id"/>
                <result column="role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="dept_id" property="dept_id"/>
                <result column="dept_name" property="dept_name"/>
                <result column="dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
    </resultMap>
  • 打赏
  • 举报
回复
引用 1 楼 qq_35942223 的回复:
两个对象你有在xml里进行配置
已经映射过了
<resultMap id="workResultMap" type="com.zhuoxun.entity.Work">
        <id column="work_id" property="work_id"/>
        <result column="work_order" property="work_order"/>
        <result column="work_content" property="work_content"/>
        <result column="work_demand" property="work_demand"/>
        <result column="work_create_time" property="work_create_time"/>
        <result column="work_last_edit_time" property="work_last_edit_time"/>
        <result column="work_comp_cycle" property="work_comp_cycle"/>
        <result column="deadline" property="deadline"/>
        <result column="work_join_emp" property="work_join_emp"/>
        <result column="work_progress" property="work_progress"/>
        <result column="work_percent" property="work_percent"/>
        <result column="notes" property="notes"/>
        <result column="image" property="image"/>
        <result column="state" property="state"/>
        <association property="user_work_leader" javaType="com.zhuoxun.entity.User">
            <id column="user_id" property="user_id"/>
            <result column="user_name" property="user_name"/>
            <result column="user_passwd" property="user_passwd"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="role_id" property="role_id"/>
                <result column="role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="dept_id" property="dept_id"/>
                <result column="dept_name" property="dept_name"/>
                <result column="dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
        <association property="user" javaType="com.zhuoxun.entity.User">
            <id column="user_id" property="user_id"/>
            <result column="user_name" property="user_name"/>
            <result column="user_passwd" property="user_passwd"/>
            <association property="role" javaType="com.zhuoxun.entity.Role">
                <id column="role_id" property="role_id"/>
                <result column="role_name" property="role_name"/>
            </association>
            <association property="dept" javaType="com.zhuoxun.entity.Dept">
                <id column="dept_id" property="dept_id"/>
                <result column="dept_name" property="dept_name"/>
                <result column="dept_leder_name" property="dept_leder_name"/>
            </association>
        </association>
    </resultMap>
baidu_38818116 2017-07-11
  • 打赏
  • 举报
回复
sql查询第二个user对象的属性的时候给他的字段起别名 例如: <mapper namespace="org.wzk.dao.StudentDao"> <resultMap type="org.wzk.pojo.Student" id="studentMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="hobby" property="hobby"/> <result column="clazz_id" property="clazz.id"/> <result column="clazz_cname" property="clazz.name"/> <collection property="list" ofType="org.wzk.pojo.Course"> <id column="cid" property="cid"/> <result column="course" property="course"/> <result column="ctime" property="ctime"/> </collection> </resultMap> <sql id="studentColumns">s.id,s.name,s.age,s.sex,s.hobby</sql> <sql id="courseColumns">c.cid,c.course,c.ctime</sql> <sql id="clazzColumns">clazz.id clazz_id,clazz.name clazz_cname</sql> 这样查询的的时候可以避免冲突
qq_35942223 2017-07-11
  • 打赏
  • 举报
回复
两个user跟字段进行了映射没有
nikyotensai 2017-07-11
  • 打赏
  • 举报
回复
你查的难道不是同一个东西?
qq_35942223 2017-07-11
  • 打赏
  • 举报
回复
两个对象你有在xml里进行配置

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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