MyBtais学习(三)-动态SQL

1.if和where标签

if标签:用来判断条件

    <select id="selectByIf" resultType="com.work.pojo.User">
        select * from user where
                <if test="username != null and username !=''">
                               username like #{username}
                </if>
                <if test="sex != null and sex != ''">
                               and sex = #{sex};
                </if>
    </select>

if标签内的为true时,拼接SQL语句,当if内为false时,就不拼接SQL语句

所以,如上例,当查询条件有一项或多项不满足的时候。就会出现:

 select * from user where and sex = #{sex}
 select * from user where 

SQL语句就会出错,因此我们一般在使用if时都搭配where,这样可以避免SQL拼接错误的出现。

    <select id="selectByIf" resultType="com.work.pojo.User">
        select * from user where
        <where>
                <if test="username != null and username !=''">
                               username like #{username}
                </if>
                <if test="sex != null and sex != ''">
                               and sex = #{sex};
                </if>
        </where>
    </select>

where有条件时作为关键词,能去掉多余的and和or

没有查询条件时自动消失

2.set标签

当只有if条件判断语句的话,当最后一个参数没有输入会出现多余的,从而导致SQL语句出错。

<update id="updateByIF" >
     update user
        <if test="username != null and username !=''">
            username =#{username},
        </if>
        <if test="birthday != null">
            birthday = #{birthday},
        </if>
        <if test="sex != null and sex != ''">
            sex =#{sex},
        </if>
        <if test="address != null and address!=''">
            address = #{address}
        </if>
     where id = #{id};
    </update>

出错的SQL语句:

使用set标签可以去掉多余的,

 <update id="updateByIF" >
     update user
     <set>
        <if test="username != null and username !=''">
            username =#{username},
        </if>
        <if test="birthday != null">
            birthday = #{birthday},
        </if>
        <if test="sex != null and sex != ''">
            sex =#{sex},
        </if>
        <if test="address != null and address!=''">
            address = #{address}
        </if>
        </set>
     where id = #{id};
    </update>

3.foreach标签

如果用户要批量删除,只用id=#{id}是不够的

foreach标签:可以遍历数组和集合

foreach标签的属性:

collection:被遍历的数组或集合的名字

item:表示遍历之后,每一个参数的变量名

separator:遍历的每一个参数之间的间隔符号

open:遍历前添加的内容

close:遍历后添加的内容

#{变量名}:item的值

xml:

<delete id="deleteByArray" >
        delete from user where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

接口:

// 批量删除  框架默认的数组变量名为array,如果不想使用可以自己设置注解
    void deleteByArray(@Param("ids") int[] ids);

测试:

 @Test
    public void test09()throws IOException{
        String resource = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new 

        SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        userMapper.deleteByArray(new int[]{1,2});

        sqlSession.commit();

        sqlSession.close();
    }

4.choose标签

choose标签类似于Java里的switch-case。

choose标签的属性:

when:匹配一个条件

otherwise:所有条件不匹配时执行

5.sql和include标签

我们发现在接口映射文件中会出现很多相同的sql语句,每个地方都写一遍就很麻烦,我们可以把相同的sql语句抽出来,在需要的地方引入即可

1.sql标签:定义一段SQL语句,起个名字可以重复使用

2.include标签:引入上面定义的SQL代码段

 <sql id="userInfo">
        id,username,birthday,sex,address
    </sql>
    <select id="selectBySQL" resultType="com.work.pojo.User">
        select 
            <include refid="userInfo">
                from user
            </include>
    </select>

6.解决字段名映射问题

MySQL中命名通常参照下划线命名法

Java中命名通常参照驼峰命名法

数据库字段名和类的成员变量名不一致的时候,查询结果无法封装成功

解决方案一:

给不一致的字段取别名

select o_id oId,user_id userId from `order` 

解决方案二:

在核心配置文件mybatis-config.xml中的setting中开启全局驼峰命名映射

<setting name="mapUnderscoreToCamelCase" value="true"/>

mapUnderscoreToCamelCase默认是false,设置为true即可

解决方案三:

使用resultMap解决

resultMap可以建立查询的列与对象属性的对应关系

1.定义resultMap标签,id属性:唯一标识,type属性:要封装的类型

2.id标签:对主键字段进行映射

3.result标签:对普通字段进行映射

4.column代表数据库字段名,property代表类的成员变量名

5.在查询结果中使用resultMap

 <resultMap id="OrderMap" type="Order" >
        <id column="o_id" property="oId"/>
        <result column="user_id" property="userId" />
    </resultMap>

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值