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


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

    Mybatis的相关配置

    Mybatis查询

    1、查询单个实体

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

    2、查询多个实体

        public List<Emp> getEmps();
    
    • 1
        <select id="getEmps" resultType="Emp">
            select * from emp;
        select>
    
    • 1
    • 2
    • 3

    3、查询单个字段

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

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

        //返回一条数据的多个字段
        Map<String,String> getEmpToMap(Integer eid);
    
    
    • 1
    • 2
    • 3
        
        <select id="getEmpToMap" resultType="map">
            select * from emp where eid=#{eid}
        select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

        //返回多条数据的多个字段
        List<Map<String,String>>getEmpToListMap();
    
    • 1
    • 2
        
        <select id="getEmpToMap" resultType="map">
            select * from emp
        select>
    
    • 1
    • 2
    • 3
    • 4

    6、模糊查询

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

    动态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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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} and
                if>
                <if test="sex !='' and sex != null">
                    sex=#{sex} and
                if>
                <if test="age !='' and age != null">
                    age=#{age}
                if>
            trim>
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    Ubuntu不能上网解决办法
    idea所有历史版本下载
    设计模式-单例模式
    22 OpenCV 直方图计算
    机器学习笔记只隐马尔可夫模型(三)求值问题——前向算法(Forward Algorithm)
    【华为机试真题 JAVA】报数游戏-100
    MyBatis `<foreach>`
    积分值和面积、对称性
    SpringBoot教程(16) 什么是RESTful?
    阿里云OSS图片存储
  • 原文地址:https://blog.csdn.net/u013010499/article/details/126016579