mybaits中查询中对象中存在对象(可能是单一对象,可能是集合)

User和Department数据为多对一

实体对象:
User
属性:
id Integer,name String,Department department
Department
属性
id Integer,name String,sn String


mapper.xml中配置问题件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tabchanj.mybatis.domain.UserMapper">

    <resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap">
        <!-- User的基本属性 -->
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <!--
           property:表示本类中关联的对象属性 
        并将关联对象的列名的别名与关联对象的属性做对应
        -->
        <association property="dept" javaType="Department">
            <id column="dept_id" property="id" />
            <result column="dept_name" property="name" />
            <result column="dept_sn" property="sn" />
        </association>
    </resultMap>
    <select id="get" parameterType="Long" resultMap="UserMap">
        <!-- 此时先将department的数据一并查询出来 -->
        select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id where u.id = #{cc}
    </select>

    <select id="list" resultMap="UserMap">
        select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id
    </select>
</mapper>



-----------Department的mapper映射配置文件----------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tabchanj.mybatis.domain.DepartmentMapper">
    <resultMap type="Department" id="DepartmentMap"/>
    <select id="get" parameterType="Long" resultMap="DepartmentMap">
        select * from t_department where id = #{id}
    </select>
    <select id="list" resultMap="DepartmentMap">
        select * from t_department
    </select>
</mapper>

改进的User的Mapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tabchanj.mybatis.domain.UserMapper">

    <resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap">
        <!-- User的基本属性 -->
        <id property="id" column="id" />
        <result property="name" column="name" />
        <!-- 
             property:表示本类中关联的对象属性
             columnPrefix:表示关联对象的列名前都有
             dept_ resultMap:指明返回的类型时department类型,之一resultMap路径department的mapper映射配置文件中的resultMap的路径
          -->
        <association property="dept" javaType="Department"
            columnPrefix="dept_"
            resultMap="com.tabchanj.mybatis.domain.DepartmentMapper.DepartmentMap">
            <!-- <id column="dept_id" property="id" /> -->
            <!-- <result column="dept_name" property="name" /> -->
            <!-- <result column="dept_sn" property="sn" /> -->
        </association>
    </resultMap>
    <select id="get" parameterType="Long" resultMap="UserMap">
        <!-- 此时先将department的数据一并查询出来 -->
        select u.*,d.name dept_name,d.sn dept_sn from t_user u LEFT JOIN
        t_department d on u.dept_id=d.id where u.id = #{cc}
    </select>

    <select id="list" resultMap="UserMap">
        select u.*,d.name dept_name,d.sn
        dept_sn from t_user u LEFT JOIN t_department d on u.dept_id=d.id
    </select>
</mapper>



Department对User是一对多


-----------------department的mapper映射配置文件-----------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tabchanj.mybatis.domain.DepartmentMapper">
    <resultMap type="Department" id="DepartmentMap">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="sn" property="sn" />
        <!-- 
        property:指明本类users属性
        ofType:指明users集合中装的时User类型的数据
        select:该查询语句还是使用的是user的mapper映射配置文件中的查询语句,从路径就可以看出
        column:指明该查询语句中的参数时本类中的id列的值
         -->
        <collection property="users" ofType="User"
            select="com.tabchanj.mybatis.domain.UserMapper.getByDeptId" column="id">
        </collection>
    </resultMap>
    <select id="get" parameterType="Long" resultMap="DepartmentMap">
        select * from
        t_department where id = #{id}
    </select>
    <select id="list" resultMap="DepartmentMap">
        select * from t_department
    </select>
</mapper>


-------User的mapper映射配置文件(只保留了id="getByDeptId"的sql查询语句)--------------------

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tabchanj.mybatis.domain.UserMapper">

    <resultMap type="com.tabchanj.mybatis.domain.User" id="UserMap">
        <!-- User的基本属性 -->
        <id property="id" column="id" />
        <result property="name" column="name" />
       </resultMap>
<!-- 该语句在department的mapper映射配置文件中被引用-->
 <select id="getByDeptId" parameterType="Long" resultMap="UserMap">
        select * from t_user where dept_id=#{id}
    </select>
</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值