MyBatis数据处理

  1. <where></where>标签可以去掉前and
    <sql id="selectall" >
         SELECT * FROM USER
    </sql>
    <select id="selectUserByUsernameAndSex" parameterType="com.company.combine.model.User"
            resultType="com.company.combine.model.User">
       <include refid="selectall"/>
        <where>
            <if test="sex!=null and sex!=''">
                and sex = #{sex}
            </if>
            <if test="username!=null and username!=''">
                and username = #{username}
            </if>
        </where>
    </select>
  1. foreach 在Javabean中传递List
    <!--bean中传List ,在User中有一个名叫idList的List-->
    <select id="selectByPrimaryKeys" parameterType="com.company.combine.model.User"
            resultType="com.company.combine.model.User">
        SELECT *
        FROM USER
        <where>
            <foreach collection="idList" item="id" separator="," open="id in(" close=")">
                #{id}
            </foreach>
        </where>
    </select>
  1. foreach 直接传递数组
  <!--直接传数组-->
    <select id="selectByPrimaryKeys"
            resultType="com.company.combine.model.User">
        SELECT *
        FROM USER
        <where>
            <foreach collection="array" item="id" separator="," open="id in(" close=")">
                #{id}
            </foreach>
        </where>

    </select>
  1. foreach 直接传递List
 <!--直接传list-->
    <select id="selectByPrimaryKeys"
            resultType="com.company.combine.model.User">
        SELECT *
        FROM USER
        <where>
            <foreach collection="list" item="id" separator="," open="id in(" close=")">
                #{id}
            </foreach>
        </where>

    </select>
  1. 模糊查询
    <!--#{}  占位符-->
    <!--${}  拼接符-->
    <select id="selectUserByUsername" parameterType="String"
            resultType="com.company.combine.model.User">
        SELECT * FROM USER WHERE username LIKE "%${value}%"
    </select>
  1. 一对一关联
<resultMap id="result2" type="com.company.combine.model.Orders">
        <id property="id" column="id"/>
        <result property="userId" column="userId"/>
        <result property="number" column="number"/>
        <result property="createtime" column="createtime"/>

        <association property="user" javaType="com.company.combine.model.User">
            <result property="username" column="username"/>
        </association>
    </resultMap>
    <!--一对一-->
    <select id="selectOrdersList"
            resultMap="result2">
        SELECT
        o.id,
        o.user_id as userId,
        o.number,
        o.createtime,
        u.username
        FROM orders o LEFT JOIN user u ON o.user_id=u.id

    </select>
  1. 一对多关联
    <!--一对多-->
    <resultMap id="result3" type="com.company.combine.model.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <collection property="ordersList" ofType="com.company.combine.model.Orders">
            <id property="id" column="id"/>
            <result property="userId" column="userId"/>
            <result property="number" column="number"/>
            <result property="createtime" column="createtime"/>
        </collection>
    </resultMap>
    <select id="selectUserList"
            resultMap="result3">
        SELECT
        o.id,
        o.user_id as userId,
        o.number,
        o.createtime,
        u.username
        FROM user u LEFT JOIN orders o ON u.id=o.user_id
    </select>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,954评论 18 399
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,157评论 0 4
  • 一. Java基础部分.................................................
    wy_sure阅读 9,251评论 0 11
  • 暮色入云阴,轻波逐浪暗, 山染青黛色,云浮叠罗汉。
    平天下之文世界阅读 1,248评论 0 3
  • 我今天在外面跑了一天,快走到家了,路过一个社区的门口,一小孩子 大概7.8岁左右,举起一砖头向我扔了过来!因为太突...
    九五工作室大刚阅读 5,600评论 1 1