SpringBoot2入门必读(4):Spring boot集成Mybatis(二)

SpringBoot2入门必读(4):Spring boot集成Mybatis(二)

Mybatis的相关配置

Mybatis查询

1、查询单个实体

    //获取单个实体,@Param("eid")也可以不用
    Emp getEmp(@Param("eid")Integer eid);
    <select id="getEmp" resultType="Emp">
        select * from emp where eid=#{eid}
    </select>

2、查询多个实体

    public List<Emp> getEmps();
    <select id="getEmps" resultType="Emp">
        select * from emp;
    </select>

3、查询单个字段

    //查询行数
    int getCount();
    <select id="getCount" resultType="Integer">
        select count(*) from emp
    </select>

4、查询一行数据的多个字段(map)

    //返回一条数据的多个字段
    Map<String,String> getEmpToMap(Integer eid);

    <!--这里直接用*,也可以只查你想要返回的字段-->
    <select id="getEmpToMap" resultType="map">
        select * from emp where eid=#{eid}
    </select>

5、查询多行数据的多个字段(ListMap)

    //返回多条数据的多个字段
    List<Map<String,String>>getEmpToListMap();
    <!--这里直接用*,也可以只查你想要返回的字段-->
    <select id="getEmpToMap" resultType="map">
        select * from emp
    </select>

6、模糊查询

    //模糊查询
    List<Emp>dim(String email);
    <select id="dim" resultType="Emp">
        select * from emp where email "%"#{email}"%"
    </select>

动态sql

1、if

  • if 标签通过test属性里面的表达式进行判断,表达式为true则执行里面的内容,否则不执行
  • 这里需要注意加上:where 1=1,因为如果不加就会变成 select * from emp where and empName=#{empName},where后面直接跟and会报错
<!--if 标签通过test属性里面的表达式进行判断,表达式为true则执行里面的内容,否则不执行
    这里需要注意加上:where 1=1,因为如果不加就会变成  select * from emp where and empName=#{empName},where后面直接跟and会报错-->
    <select id="getEmp2" resultType="Emp">
        select * from emp where 1=1
        <if test="empName !='' and empName != null">
            and empName=#{empName}
        </if>
        <if test="sex !='' and sex != null">
            and sex=#{sex}
        </if>
        <if test="age !='' and age != null">
            and age=#{age}
        </if>
    </select>

2、where

如果需要用where条件的话,一般使用where和if标签

  • 1、如果if有为true的,则自动加上where关键字,并将第一个满足条件的and去掉,比如where and sex=#{sex}
  • 2、如果if中没有为true的,则不会加上where关键字
  • 3、and需要放前面,比如:and sex=#{sex},不能放后面,比如:sex=#{sex} and,因为where可以去掉前面的and,不能去掉后面的and
<!--如果需要用where条件的话,一般使用where和if标签
        1、如果if有为true的,则自动加上where关键字,并将第一个满足条件的and去掉,比如where and sex=#{sex}
        2、如果if中没有为true的,则不会加上where关键字
        3、and需要放前面,比如:and sex=#{sex},不能放后面,比如:sex=#{sex} and,因为where可以去掉前面的and,不能去掉后面的and
    -->
    <select id="getEmp3" resultType="Emp">
        select * from emp
        <where>
            <if test="empName !='' and empName != null">
                empName=#{empName}
            </if>
            <if test="sex !='' and sex != null">
                and sex=#{sex}
            </if>
            <if test="age !='' and age != null">
                and age=#{age}
            </if>
        </where>
    </select>

3、trim

trim用于去掉或添加标签中的内容
常用属性:

  • prefix:在trim标签中的内容的前面添加某些内容
  • prefixOverrides:在trim标签中的内容的前面去掉某些内容
  • suffix:在trim标签中的内容的后面添加某些内容
  • suffixOverrides:在trim标签中的内容的后面去掉某些内容
  • 如果用trim标签需要这么使用:empName=#{empName} and,也就是and放后面
<!--trim用于去掉或添加标签中的内容
        常用属性:
        prefix:在trim标签中的内容的前面添加某些内容
        prefixOverrides:在trim标签中的内容的前面去掉某些内容
        suffix:在trim标签中的内容的后面添加某些内容
        suffixOverrides:在trim标签中的内容的后面去掉某些内容
        如果用trim标签需要这么使用:empName=#{empName} and,也就是and放后面-->
    <select id="getEmp4" resultType="Emp">
        select * from emp
        <trim prefix="where" suffixOverrides="and">
            <if test="empName !='' and empName != null">
                empName=#{empName} and
            </if>
            <if test="sex !='' and sex != null">
                sex=#{sex} and
            </if>
            <if test="age !='' and age != null">
                age=#{age}
            </if>
        </trim>
    </select>

4、foreach

foreach一般用来循环数组和集合
常用属性

  • collection: collection属性值分别默认用"list"、"array"代替,Map对象没有默认的属性值。
    但是,在作为入参时可以使用@Param(“keyName”)注解来设置自定义collection属性值,设置keyName后,list、array会失效,需要使用@Param(“keyName”)设置的名字
    可以看例子,
  • item:表示集合和数组在循环中的每一个数据或者每一条数据;
  • index: 在list、array中,index为元素的序号索引。但是在map中,index为遍历元素的key值,该参数为可选项;
  • open: foreach遍历前需要加的符号或字符,通常与close=")"搭配使用。使用场景in(),values()时,该参数为可选项;
  • separator: 元素之间的分隔符,类比在in()的时候,separator=“,”,最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;
  • close: foreach遍历结束后加的符号,通常与open="("搭配使用,该参数为可选项
<!--foreach标签
        collection: collection属性值分别默认用"list"、"array"代替,Map对象没有默认的属性值。
                    但是,在作为入参时可以使用@Param(“keyName”)注解来设置自定义collection属性值,设置keyName后,list、array会失效,需要使用@Param(“keyName”)设置的名字
                    可以看<select id="getEmp5">例子,
        item: 表示集合和数组在循环中的每一个数据或者每一条数据;
        index: 在list、array中,index为元素的序号索引。但是在Map中,index为遍历元素的key值,该参数为可选项;
        open: foreach遍历前需要加的符号或字符,通常与close=")"搭配使用。使用场景in(),values()时,该参数为可选项;
        separator: 元素之间的分隔符,类比在IN()的时候,separator=",",最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;
        close:foreach遍历结束后加的符号,通常与open="("搭配使用,该参数为可选项-->
    <insert id="addEmps" parameterType="java.util.ArrayList">
        insert into emp (empName,age,sex,email,did) values
        <foreach collection="list" index="index" separator="," item="item">
            (#{empName},#{age},#{sex},#{email},#{did})
        </foreach>
    </insert>

    <!--
        定义接口为:List<Emp> getEmp5(@Param(“eids”) List<String>eids)
        因为接口用@Param(“eids”),所以collection需要用eids
    -->
    <select id="getEmp5" resultType="Emp">
        select * from emp where eid in
        <foreach collection="eids" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值