• MyBatisday02


    Select

    • 基本查询
    • 模糊查询
      • 解决方案
        • 使用字符串拼接'%${变量名}%'
        • 使用concat方法 concat('%',#{变量名},'%')  推荐
    • 多个参数查询
      • 解决方案
        • 使用封装对象的形式传输内容
        • 通过map来进行封装传输
    List<Dept> findByCondition(Map<String,Object> map) ;
    <select id="findByCondition" resultType="dept" parameterType="map">
            SELECT * FROM dept WHERE dname = #{dname} and loc = #{loc}
    </select>
    • 数据库表中的列名与实体类的字段名不一致
      • 解决方案
        • 给列名起别名
    SELECT e.*,d.dname as dnm
    FROM
    emp e,dept d
    WHERE 
    e.deptno = d.deptno
    AND
    e.empno=110111
        • 通过resultMap实现字段映射
    <!--
            id:代表当前resultMap的名称
            type:当前resultMap的真实类型
            id:主键映射 property=“实体类字段” column=“列名”
            result:属性映射
        -->
        <resultMap id="emp_result" type="emp">
            <id property="empno" column="empno" />
            <result property="ename" column="ename" />
            <result property="job" column="job" />
            <result property="hiredate" column="hiredate" />
            <result property="sal" column="sal" />
            <result property="comm" column="comm" />
            <result property="dnm" column="dname" />
        </resultMap>
    • $和#之间的区别
      • #{}是预编译,${}是字符串替换
      • MyBatis在处理#{}时,会将sql中的#{}替换为?,调用PreparedStatement的set()的方法来赋值;
      • Mybatis在处理${}时,就是把${}替换成变量的值 
      • 使用#{}能有效防止SQL注入,提高系统安全性
    • Mapper接口形式操作
      • 声明一个Mapper接口,将接口与对应的映射文件放置在同一个文件夹中,namespace配置需要引用接口的名称,接口中的方法名与映射文件中的节点id一致,参数类型、返回值一致,在调用时,使用SqlSession对象的getMapper(Mapper.class)的形式调用
    • Mapper接口形式的原理
      • Dao接口也就是Mapper接口。接口的全限名,就是映射文件中的namespace值;接口的方法名,也就是映射文件中的id值;接口方法内的参数,就是传递个sql语句的参数。
      • Mapper接口是没有实现类的,当调用接口方法时,接口全限名+方法名拼接字符串作为key值,可唯一定位一个MappedStatement。在Mybatis中,每一个<select>、<insert>、<update>、<delete>标签都会被解析成一个MappedStatement对象。
      • Mapper接口里的方法,是不能重载的,因为使用的是全限名+方法名作为寻找的目标。Mapper接口的工作原理是JDK动态代理,Mybatis运行时会使用JDK动态代理为Mapper接口生成代理对象proxy,代理对象会拦截接口方法,转而执行MappedStatement中对应的sql,然后将执行结果返回。

    动态sql

    if

           SELECT * FROM dept where 1 = 1
                <if test="dname != null">
                    dname = #{dname},
                </if>
                <if test="loc != null">
                    and loc = #{loc}
                </if>

    trim(where,set)

            SELECT * FROM dept
            <where>
                <if test="dname != null">
                    dname = #{dname}
                </if>
                <if test="loc != null">
                    and loc = #{loc}
                </if>
            </where>

    where节点会自动为查询语句判断是否需要增加where子句,通过判断结果,自动生成“,”

            update dept
            <set>
                <if test="dname != null">
                    dname = #{dname},
                </if>
                <if test="loc != null">
                    loc = #{loc},
                </if>
            </set>
            <where>
                deptno = #{deptNO}
            </where>

    set节点不会自动去除“,”,可能会在某些情况下,导致SQL语句出错,可以利用trim实现对“,”处理

         update dept
            <trim prefix="set" suffixOverrides=",">
                <if test="dname != null">
                    dname = #{dname},
                </if>
                <if test="loc != null">
                    loc = #{loc},
                </if>
            </trim>
            <where>
                deptno = #{deptNO}
            </where>

    choose (when, otherwise)

            select * from dept
            <where>
                <choose>
                    <when test="dname != null">
                        dname = #{dname}
                    </when>
                    <when test="loc != null">
                        loc = #{loc}
                    </when>
                </choose>
            </where>

    foreach

            DELETE FROM emp
            <where>
                empno in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </where>
  • 相关阅读:
    .NET Core SignalR 和 .NET SignalR 区别
    MySQL 连接出现 Authentication plugin 'caching_sha2_password' cannot be loaded
    Geohash 基本知识及 .NET 下计算相邻8个区域编码
    ASP.NET 下配置请求大小、请求时间等参数
    .NET Core、EF、Dapper、MySQL 多种方式实现数据库操作(动态注册实体类)
    .NET Core 开发常用命令(VS Code)
    ping
    exec与xargs区别
    doc转docx
    读取docx表格中的信息
  • 原文地址:https://www.cnblogs.com/heureuxl/p/13855537.html
Copyright © 2020-2023  润新知