MyBatis

MyBatis的关系
一对一
人和身份证

<?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="users">
    <!--查询用户表中的所有用户信息-->
    <select id="findAllUsers" resultType="Users">
        select * from users;
    </select>

    <!--自定义返回类型 自定义返回结果集-->
    <resultMap id="usersAndCard" type="Users">
        <!--查询用户的同时查询卡的信息-->
        <id property="uid" column="UID"></id><!--主键-->
        <result property="uname" column="UNAME"></result>
        <!--用户对应的卡的信息-->
        <association property="card" javaType="Card">
            <result property="uid" column="UID"></result>
            <result property="number" column="NUMBER"></result>
        </association>

    </resultMap>


    <select id="findUsersAndCard" parameterType="int" resultMap="usersAndCard">
        <!--select u.*,c.* from users u,card c where u.uid=c.uid and u.uid=#{uid};-->
        select * from users u inner join card c on u.uid=c.uid where u.uid=#{uid}
    </select>

</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.sw.dao.ProvinceDao">

    <select id="findAllProvince" parameterType="int" resultType="Province">
        select * from province where pid=#{pid}
    </select>


    <!--自定义返回结果集 包含省份和城市-->
    <resultMap id="provinceAndCity" type="Province">
        <id column="pid" property="pid"></id>
        <result column="pname" property="pname"></result>
        <!--省份对应的城市集合
            ofType:集合中的类型是什么类型
        -->
        <collection property="cities" ofType="City" column="pid">
            <id column="cid" property="cid"></id>
            <result column="cname" property="cname"></result>
        </collection>
    </resultMap>

    <!--查询省份的同时查询对应的城市-->
    <select id="findProvinceAndCity" parameterType="int" resultMap="provinceAndCity">
        select * from province p inner join city c on p.pid=c.pid where p.pid=#{pid}
    </select>



</mapper>

ps:有关系的两个类都要对应配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值