美文网首页
动态sql补

动态sql补

作者: 何以解君愁 | 来源:发表于2022-08-09 15:04 被阅读0次
<!-- 使用foreach批量插入用户-->
        <insert id="insertMultiUsers">
            insert into user(user_name,gender,email,address,dept_id)
            values
            <foreach collection="users" item="user" separator=",">
                (#{user.userName},#{user.gender},#{user.email},#{user.address},#{user.deptId})
            </foreach>
        </insert>
<!--关联表多对一的查询-->
构造函数加一个private Dept dept;
//mapper层传一个Integer类型的id
<select id="getEmpAndDeptByEmpId" resultType="Emp">
    select
    t_emp.*,t_dept.* from t_emp left join t_dept
    on t_emp.dept_id = t_dept.dept_id where t_emp. emp_id = #{empId}
</select>
//级联处理多对一映射问题
<resultMap id="empAndDeptResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <result column="dept_id" property="dept.deptId"></result>
    <result column="dept_name" property="dept.deptName"></result>
</resultMap>
association处理多对一映射问题:
<resultMap id="empAndDeptResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    <association property="dept" javaType="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="depk_name" property="deptName"></result>
    </association>
</resultMap>
<!--关联表一对多的查询-->
构造函数加一个private List<Emp> emp;
//mapper层传一个Integer类型的id
<select id="getEmpAndDeptByEmpId" resultType="Emp">
    select
    t_emp.*,t_dept.* from t_emp right join t_dept
    on t_emp.dept_id = t_dept.dept_id where t_emp. emp_id = #{empId}
</select>
<resultMap id="deptAndEmpResultMap" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <collection property "emp" ofype="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result></collection>
 </resultMap>

相关文章

  • 动态sql补

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第十三章 使用动态SQL(一)

    第十三章 使用动态SQL(一) 动态SQL简介 动态SQL是指在运行时准备并执行的SQL语句。在动态SQL中,准备...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • 关于Mybatis的一些问题讨论

    Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理 动态sql的用途 Mybat...

  • MyBatis5-动态 SQL

    动态 SQL 什么是动态 SQL 就是动态的对 SQL 进行组装 拼接. : 可以自动去...

  • geoserver动态颜色参数样式、动态sql配置实现

    geoserver动态颜色参数样式、动态sql配置实现 动态颜色参数样式、动态sql 访问方式: http://l...

  • 强大的动态SQL

    1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...

  • MyBatis:动态 SQL

    1. 动态 SQL 简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素...

  • 无标题文章

    ### 一、简答题 #### 1、Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行...

网友评论

      本文标题:动态sql补

      本文链接:https://www.haomeiwen.com/subject/bdujwrtx.html