本文小編為大家詳細介紹“Mybatis動態SQL及單表多表查詢怎么應用”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Mybatis動態SQL及單表多表查詢怎么應用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
#{}:相當于JDBC里面替換占位符的操作方式(#{}->“”).相當于預編譯處理(預編譯處理可以防止SQL注入問題)
${}:相當于直接替換(desc這種關鍵字),但這種不能預防SQL注入
select * from userinfo where username='${name}'${} VS #{}
${}是直接替換,#{}是預執行;
${} 會存在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)
用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>
注冊分為必填和選填,如果在添加用戶的時候有不確定的字段傳入,就需要使用動態標簽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標簽的屬性
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標簽可以判斷第一個查詢條件前面有沒有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>和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屬性:
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及單表多表查詢怎么應用”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。