1223JAVA学习

本文详细介绍了MyBatis中对多个数据进行操作的方法,包括使用`foreach`标签删除记录,以及处理表多对一和一对多关系的映射方式。对于多对一场景,展示了两种不同的映射实现,而一对多情况则通过`collection`属性和子查询实现了部门与员工的关系映射。这些内容有助于深入理解MyBatis的动态SQL和复杂关联映射。
摘要由CSDN通过智能技术生成

总结

Mybatis增强

  • 对多个数据进行操作

    <!--
    	mybatis会把集合或者数组放入map中
    	如果是集合,key默认是list
    	如果是数组,key默认就是array
    
    	foreach:遍历标签
    	collection:要遍历的对象(key)
    	separator:每遍历一次的分隔符
    	open:遍历开始前加的符号
    	close:遍历后加的符号
    	
    -->
    <!--void delete(List<Long> ids);-->
        <delete id="delete">
            DELETE  FROM product where id in
            <foreach collection="list" item="d" open="(" close=")" separator=",">
                #{d}
            </foreach>
        </delete>
    
  • 表多对一的情况

    <!--方式一-->
    <resultMap id="empResultMap" type="cn.itsource._02_many2one.domain.Employee">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        <!--
    		这个字段是对象
    	-->
            <association property="dept" javaType="cn.itsource._02_many2one.domain.Department">
                <id column="did" property="id"></id>
                <result column="dname" property="name"></result>
            </association>
        </resultMap>
        <!--List<Employee> findAll();-->
        <select id="findAll" resultMap="empResultMap">
            SELECT e.*,d.id did,d.name dname from employee e,department d
            WHERE e.dept_id=d.id
        </select>
    
    <!--方式二-->
    <resultMap id="empResultMap" type="cn.itsource._02_many2one.domain.Employee">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
            <association property="dept" column="dept_id" select="deptByDeptId" javaType="cn.itsource._02_many2one.domain.Department">
    
            </association>
        </resultMap>
    
        <select id="deptByDeptId" resultType="cn.itsource._02_many2one.domain.Department">
            SELECT * FROM department WHERE id=#{id}
        </select>
    
        <!--List<Employee> findAll();-->
        <select id="findAll" resultMap="empResultMap">
            SELECT * from employee
        </select>
    
  • 表一对多的情况

    <!--方式一-->
    <resultMap id="deptResultMap" type="cn.itsource._03_.one2many.domain.Department">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
    		<!--
    			这里的字段是集合类型
    		-->
            <collection property="emp" ofType="cn.itsource._03_.one2many.domain.Employee">
                <id column="eid" property="id"></id>
                <result column="ename" property="name"></result>
            </collection>
        </resultMap>
    
        <!--List<Department> findAll();-->
        <select id="findAll" resultMap="deptResultMap">
            SELECT d.*,e.id eid,e.name ename FROM department d,employee e
            WHERE d.id=e.dept_id
        </select>
    
    <!--方式二-->
    <resultMap id="deptResultMap" type="cn.itsource._03_.one2many.domain.Department">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
            <collection property="emp" column="id" select="empByDeptId" ofType="cn.itsource._03_.one2many.domain.Employee">
    
            </collection>
        </resultMap>
    
        <select id="empByDeptId" resultType="cn.itsource._03_.one2many.domain.Employee">
            SELECT  * from employee where dept_id = #{id}
        </select>
    
        <!--List<Department> findAll();-->
        <select id="findAll" resultMap="deptResultMap">
            SELECT * FROM department
        </select>
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值