首頁 > 軟體

Mybatis詳解動態SQL以及單表多表查詢的應用

2022-06-15 14:00:42

單表查詢操作

引數預留位置#{}和${}

  • #{}:相當於JDBC裡面替換預留位置的操作方式(#{}->“”).相當於預編譯處理(預編譯處理可以防止SQL隱碼攻擊問題)
  • ${}:相當於直接替換(desc這種關鍵字),但這種不能預防SQL隱碼攻擊
select * from userinfo where username='${name}'

${} VS #{}

  • ${}是直接替換,#{}是預執行;
  • ${} 會存在SQL 注入問題,#{}不存在SQL隱碼攻擊問題

SQL 注入

UserInfo userInfo = userMapper.login("admin","' or 1='1");

mysql> select * from userinfo where username = 'admin' and password ='' or 1='1';
+----+----------+----------+-------+---------------------+---------------------+-------+
| id | username | password | photo | createtime          | updatetime          | state |
+----+----------+----------+-------+---------------------+---------------------+-------+
|  1 | admin    | admin    |       | 2021-12-06 17:10:48 | 2021-12-06 17:10:48 |     1 |
+----+----------+----------+-------+---------------------+---------------------+-------+
1 row in set (0.00 sec)

like模糊查詢

用concat進行字串拼接

   <select id="findListByName" resultMap="BaseMap">
        select * from userinfo where username like concat('%',#{name},'%')
    </select>

多表查詢操作

一對一多表查詢

一對一的多表查詢:需要設定resultMap中有個association標籤,property對應實體類的屬性名,resultMap是關聯屬性的字典對映(必須要設定),columnPrefix是設定字首,當多表查詢中有相同的欄位的話,就會報錯

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.ArticleInfoMapper">
    <resultMap id="BaseMap" type="com.example.demo.model.ArticleInfo">
        <!--主鍵-->
        <id property="id" column="id"></id>
        <!--普通屬性-->
        <result property="updatetime" column="updatetime"></result>
        <result property="title" column="title"></result>
        <result property="content" column="content"></result>
        <result property="createtime" column="createtime"></result>
        <result property="rcount" column="rcount"></result>
        <!--自定義物件屬性-->
        <association property="user"
                     resultMap="com.example.demo.mapper.UserMapper.BaseMap"
                     columnPrefix="u_">
        </association>
    </resultMap>
    <select id="getAll" resultType="com.example.demo.model.ArticleInfo">
        select a.*,u.id from articleinfo as a left join userinfo as u on a.uid = u.id;
    </select>
    <select id="getAll2" resultMap="BaseMap">
        select a.*,u.id as u_id ,u.username as u_username,u.password as u_password from articleinfo as a left join userinfo as u on a.uid = u.id;
    </select>
</mapper>

一對多多表查詢

collection標籤,用法同association

 <resultMap id="BaseMapper2" type="com.example.demo.model.UserInfo">
        <!--對映主鍵的)(表中主鍵和程式實體類中的主鍵)-->
        <id column="id" property="id"></id>
        <!--普通列的對映-->
        <result column="username" property="name"></result>
        <result column="password" property="password"></result>
        <result column="photo" property="photo"></result>
        <result column="createtime" property="createtime"></result>
        <result column="updatetime" property="updatetime"></result>
        <!--外部關聯-->
        <collection property="artlist" resultMap="com.example.demo.mapper.ArticleInfoMapper.BaseMap"
                    columnPrefix="a_"></collection>
    </resultMap>
 <select id="getAll3" resultMap="BaseMapper2">
        select u.*,a.id a_id,a.title a_title from userinfo u left join articleinfo a on u.id=a.uid
 </select>

動態SQL使用

if標籤

註冊分為必填和選填,如果在新增使用者的時候有不確定的欄位傳入,就需要使用動態標籤if來判斷

//p是傳遞過來的引數名,並不是表的欄位名
 <insert id="add3">
        insert into userinfo(username,password,
        <if test="p!=null">
         photo,
        </if>
         state)
        values(#{username},#{password},
        <if test="p!=null">
            #{p},
        </if>
       #{state})
 </insert>

trim標籤

trim標籤的屬性

  • prefix:表示整個語句塊,以prefix的值作為字首
  • suffix:表示整個語句塊,以suffix的值作為字尾
  • prefixOverrides:去掉最前面的符合條件的字元
  • suffixOverrides:去掉最後面的符合條件的字元
 <insert id="add4">
        insert into userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                username,
            </if>
            <if test="password!=null">
                password,
            </if>
            <if test="p!=null">
                photo,
            </if>
            <if test="state!=null">
                state,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                #{username},
            </if>
            <if test="password!=null">
                #{password},
            </if>
            <if test="p!=null">
                #{p},
            </if>
            <if test="state!=null">
                #{state},
            </if>
        </trim>
    </insert>

where標籤

where標籤首先可以幫助我們生成where,如果有查詢條件,那麼就生成where,如果沒有查詢條件,就會忽略where

其次where標籤可以判斷第一個查詢條件前面有沒有and,如果有則會刪除

  <select id="login2" resultType="com.example.demo.model.UserInfo">
        select * from userinfo
        <where>
        <if test="username!=null">
            username=#{username}
        </if>
        <if test="password!=null">
            and password=#{password}
        </if>
        </where>
    </select>

set標籤

和where的使用基本一樣

可以自動幫助你處理最後一個逗號,並且自動寫set

    <update id="update" parameterType="map">
        update blog
        <set>
            <if test="newTitle != null">
                title=#{newTitle},
            </if>
            <if test="newAuthor != null">
                author=#{newAuthor},
            </if>
            <if test="newViews != null">
                views = #{newViews}
            </if>
        </set>
        <where>
            <if test="id != null">
                id=#{id}
            </if>
            <if test="title != null">
                and title=#{title}
            </if>
            <if test="author != null">
                and author=#{author}
            </if>
            <if test="views != null">
                and views = #{views}
            </if>
        </where>
    </update>

foreach標籤

  • foreach屬性:
  • collection:引數集合的名字
  • item:給接下來要遍歷的集合起的名字
  • open:加的字首是什麼
  • close:加的字尾是什麼
  • separator:每次遍歷之間間隔的字串
 <delete id="dels">
        delete from userinfo where id in
        <foreach collection="list" item="item" open="(" close=")" separator="," >
            #{item}
        </foreach>
 </delete>

到此這篇關於Mybatis詳解動態SQL以及單表多表查詢的應用的文章就介紹到這了,更多相關Mybatis動態SQL內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com