MyBatis的一对和多对对和动态SQL

一对多关联:

MyBatis中使用collection标签来解决一对一的关联查询,

collection标签可用的属性如下:property:指的是集合属性的值ofType:指的是集合中元素的类型column:所对应的外键字段名称select:使用另一个查询封装的结果

他有两种解决方式:

第一种:一条SQL的XML配法

<resultMap id="subjectMapper" type="Subjects">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="viewTimes" column="viewTimes"></result>
        <result property="totalVotes" column="totalVotes"></result>
        <collection property="options" ofType="Options">
            <id property="oid" column="oid"></id>
            <result column="sid" property="sid"></result>
            <result property="vote" column="vote"></result>
            <result property="optContent" column="optContent"></result>
        </collection>
    </resultMap>
<select id="selectSubject" resultMap="subjectMapper">
       SELECT * FROM OPTIONS,subjects WHERE options.`sid`=subjects.`id` AND subjects.id=#{0}
    </select>




第二种:二条SQL的XML配法

public Emp getEmpByIdMultiSQL(Integer id);

<!-- many2one  multi sql-->
<select id="getEmpByIdMultiSQL" resultMap="empMapperSQL">
    select empno,empname,deptNo from emp where empno=#{empNo}
</select>

 <resultMap id="empMapperSQL" type="Emp">
      <id column="empNo" property="empNo"></id>
      <result column="empName" property="empName"/>
      <association property="dept" javaType="Dept" select="selectDeptInfoByDeptNo" column="deptno">
      </association>
  </resultMap>
<!--根据员工对应的部门编号作为条件,检索部门的信息-->
<select id="selectDeptInfoByDeptNo" resultType="Dept">
    select * from dept where deptNo=#{deptNo}
</select>
动态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="studentNameSpace">

    <!-- resultMap标签将了工程entity实体类中的对象与数据库中的表对应起来
         resultMap中的id属性是一个唯一的名字
         子标签中的id属性用来指定主键
         result标签用来指定其他的键,其中property属性是指实体中的字段,对应的
         column属性表示的数据库中的响应的字段
     -->
    <resultMap type="com.jpzhutech.entity.Student" id="studentMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sal" column="sal"/>
    </resultMap>

    <!-- 动态查询SQL语句 -->
    <select id="findAll" parameterType="map" resultMap="studentMap">
        select id , name , sal
        from students
        <where>
            <if test="pid!=null" >
                and id = #{pid}    <!-- #{}和之前使用c3p0的时候写的?含义是相同的 -->
            </if>

            <if test="pname!=null" >
                and name = #{pname}
            </if>

            <if test="psal!=null" >
                and sal = #{psal}
            </if>
        </where>
    </select>
</mapper>
动态SQL之插入

<!-- 动态insert -->
    <!-- 定义两个sql片段,第一个对应字段名,id属性值任意并且唯一 -->
    <sql id="key">
      <trim suffixOverrides=",">
        <if test="id!=null">
            id,
        </if>

        <if test="name!=null">
            name,
        </if>

        <if test="sal!=null">
            sal,
        </if>
      </trim>
    </sql>


    <!-- 定义第二个sql片段,第二个对应?,key属性值任意并且唯一 -->
    <sql id="value">
      <trim suffixOverrides=",">
        <if test="id!=null">
            #{id},
        </if>

        <if test="name!=null">
            #{name},
        </if>

        <if test="sal!=null">
            #{sal},
        </if>
      </trim>
    </sql>

    <!-- <include refid="key"/>和<include refid="value"/>表示引用上面sql片段 -->
    <insert id="insertStudent" parameterType="com.jpzhutech.entity.Student">
        insert into students(<include refid="key"/>) values(<include refid="value"/>);
    </insert>
动态SQL之删除

 <!-- 动态删除操作  delete from students where id in(?,?,?);-->
    <delete id="deleteStudent">
        delete from students where id in
        <!-- foreach用于迭代数组元素 
             open表示开始符号
             close表示结束符号
             seprator表示元素间的分割符
             items表示迭代的数组
        -->
        <foreach collection="array" open="(" close=")" separator="," item="ids">
            #{ids}
        </foreach>
 
动态SQL之更新

 <!-- 动态更新SQL语句,update table_name set name=? , sal=? where id=?,
         其中id不能更新,因为id为主键,这个动态更新该怎么写呢? 
         set标签会自动判断后面是否加,
         -->
    <update id="updateStudent" parameterType="map" >
        update students
        <set>
            <if test="pname!=null">
                name = #{pname},
            </if>

            <if test="psal!=null">
                sal = #{psal},
            </if>
        </set>
        where id = #{pid}
    </update>
原文地址:https://www.cnblogs.com/liuzhiw/p/7674637.html