MyBatis学习笔记(二)——标签使用

来源:互联网 发布:ip与mac绑定怎么设置 编辑:程序博客网 时间:2024/06/05 12:47

resultMap手动映射

当数据库表中的字段名称与pojo的实体类的名称不同的时候,使用resultMap:
示例代码:

<mapper namespace="com.zrxjuly.mybatis.mapper.OrderMapper">    <!-- type:为实体类的类名        id的值要与写的sql语句中resultMap的值一致.        colume:数据库中表的字段名称        property:实体类中属性的名称     -->    <resultMap type="Orders" id="orders">        <result column="user_id" property="userId"/>    </resultMap>        <!-- resultMap的值要与resultMap标签中的id的值一致 -->    <select id="selectOrder" resultMap="orders">        select * from orders    </select></mapper>

动态sql

通过MyBatis提供的各种标签方法实现动态拼接sql。

if标签

示例代码:

<select id="selectUserBySexAndUsername" parameterType="User" resultType="User">    select * from user     where 1=1    <if test="sex != null and sex != ''">        sex=#{sex}     </if>    <if test="username != null and username != ''">        and username=#{username}    </if>  </select>

注:where后的1=1的含义:如果sex为空的话,则语句就会变成select * from user where and username=#{username} ,这样就会报sql语法错误。1=1意思是true

where标签

不用where 1=1的方法,直接用where标签:

<select id="selectUserBySexAndUsername" parameterType="User" resultType="User">    select * from user    <!-- where可以去掉第一个前and,也就是说在加and语句的时候要把and放在条件之前 -->    <where>        <if test="sex != null and sex != ''">            sex=#{sex}         </if>        <if test="username != null and username != ''">            and username=#{username}        </if>      </where> </select>

sql片段

若sql语句中多次出现重复部分,可以提取出来公用:

<!-- 提取出来的sql片段,公共部分, -->  <sql id="selectA">    select * from user  </sql>  <select id="selectUserBySexAndUsername" parameterType="User" resultType="User">    <include refid="selectA" /><!-- 此处引用了select * from user -->    <!-- where可以去掉第一个前and,也就是说在加and语句的时候要把and放在条件之前 -->    <where>        <if test="sex != null and sex != ''">            sex=#{sex}         </if>        <if test="username != null and username != ''">            and username=#{username}        </if>      </where>  </select>

foreach标签

当要求根据多个参数进行查询时,用foreach循环遍历多个值:

<!-- collection:传入的参数的名称,        例如:如果接口传过来的参数为Integer[]类型的,则collection="array",此时parameterType属性去掉            如果接口传过来的参数为List<Integer>类型的,则collection="list",此时parameterType属性去掉            item:循环遍历的值            open:遍历开始            close:遍历结束            separator:分隔符            该sql语句相当于:select * from user where id in(1,2,3)       --><select id="selectUserByIds" parameterType="QueryVo" resultType="User">    select * from user    <where>        id in         <foreach collection="idList" item="id" open="(" close=")" separator=",">            #{id}        </foreach>    </where></select>

参数为Integer[]类型:

<select id="selectUserInteger" resultType="User">    select * from user    <where>        id in         <foreach collection="array" item="id" open="(" close=")" separator=",">            #{id}        </foreach>    </where></select>

参数为List<Integer>类型:

<select id="selectUserList" resultType="User">    select * from user    <where>        id in         <foreach collection="list" item="id" open="(" close=")" separator=",">            #{id}        </foreach>    </where></select>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机突然打不开流量怎么办 天猫商家迟迟不发货怎么办 苹果7开不开机了怎么办 苹果机黑屏了开不起来怎么办 美团收银机连不上网怎么办 6s换电池后黑屏怎么办 苹果上的软件打不开也删不掉怎么办 苹果手机打开软件打不开怎么办 苹果电脑下载的软件打不开怎么办 苹果手机下载的软件都打不开怎么办 苹7果屏幕黑屏怎么办 苹果6s手机打不开怎么办 苹果ipad密码输入打不开怎么办 苹果平板黑屏了打不开怎么办 苹果6sp电影商城打不开怎么办 苹果6应用商城打不开怎么办 游戏下载好了安装不上怎么办 苹果5s应用商店不见了怎么办 id被停用手机白板打不开怎么办 苹果6s下载不了软件怎么办 苹果下载的软件打不开怎么办 苹果6s下不了app怎么办 苹果手机因为闪退打不开了怎么办 天猫方糖坏了怎么办 天猫魔盒的遥控器坏了怎么办 天猫盒子没声音怎么办 天猫魔盒播放声音很低怎么办 天猫魔盒3a卡顿怎么办 天猫网络机顶盒打不开了怎么办 天猫机顶盒遥控器丢了怎么办 天猫机顶盒没有遥控器怎么办 天猫机顶盒很卡怎么办 天猫机顶盒没遥控器怎么办 天猫机顶盒看不了怎么办 天猫机顶盒变黑白怎么办 天猫精灵丢了怎么办 咪咕盒子没信号怎么办 猫los灯亮了怎么办 网络猫los闪红灯怎么办 台式电脑二级网页打不开.怎么办 光纤猫los红闪怎么办