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 empselect>

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 empselect>

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会报错
<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
<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放后面
<select id="getEmp4" resultType="Emp">select * from emp<trim prefix="where" suffixOverrides="and"><if test="empName !='' and empName != null">empName=#{empName} andif><if test="sex !='' and sex != null">sex=#{sex} andif><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="("搭配使用,该参数为可选项
<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><select id="getEmp5" resultType="Emp">select * from emp where eid in<foreach collection="eids" item="item" separator="," open="(" close=")">#{item}foreach>select>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部