Mybatis詳解動(dòng)態(tài)SQL以及單表多表查詢的應(yīng)用
單表查詢操作
參數(shù)占位符#{}和${}
- #{}:相當(dāng)于JDBC里面替換占位符的操作方式(#{}->“”).相當(dāng)于預(yù)編譯處理(預(yù)編譯處理可以防止SQL注入問題)
- ${}:相當(dāng)于直接替換(desc這種關(guān)鍵字),但這種不能預(yù)防SQL注入
select * from userinfo where username='${name}'
${} VS #{}
- ${}是直接替換,#{}是預(yù)執(zhí)行;
- ${} 會(huì)存在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進(jìn)行字符串拼接
<select id="findListByName" resultMap="BaseMap"> select * from userinfo where username like concat('%',#{name},'%') </select>
多表查詢操作
一對一多表查詢
一對一的多表查詢:需要設(shè)置resultMap中有個(gè)association標(biāo)簽,property對應(yīng)實(shí)體類的屬性名,resultMap是關(guān)聯(lián)屬性的字典映射(必須要設(shè)置),columnPrefix是設(shè)置前綴,當(dāng)多表查詢中有相同的字段的話,就會(huì)報(bào)錯(cuò)
<?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標(biāo)簽,用法同association
<resultMap id="BaseMapper2" type="com.example.demo.model.UserInfo"> <!--映射主鍵的)(表中主鍵和程序?qū)嶓w類中的主鍵)--> <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> <!--外部關(guān)聯(lián)--> <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>
動(dòng)態(tài)SQL使用
if標(biāo)簽
注冊分為必填和選填,如果在添加用戶的時(shí)候有不確定的字段傳入,就需要使用動(dòng)態(tài)標(biāo)簽if來判斷
//p是傳遞過來的參數(shù)名,并不是表的字段名 <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標(biāo)簽
trim標(biāo)簽的屬性
- prefix:表示整個(gè)語句塊,以prefix的值作為前綴
- suffix:表示整個(gè)語句塊,以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標(biāo)簽
where標(biāo)簽首先可以幫助我們生成where,如果有查詢條件,那么就生成where,如果沒有查詢條件,就會(huì)忽略where
其次where標(biāo)簽可以判斷第一個(gè)查詢條件前面有沒有and,如果有則會(huì)刪除
<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標(biāo)簽
和where的使用基本一樣
可以自動(dòng)幫助你處理最后一個(gè)逗號,并且自動(dòng)寫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標(biāo)簽
- foreach屬性:
- collection:參數(shù)集合的名字
- item:給接下來要遍歷的集合起的名字
- open:加的前綴是什么
- close:加的后綴是什么
- separator:每次遍歷之間間隔的字符串
<delete id="dels"> delete from userinfo where id in <foreach collection="list" item="item" open="(" close=")" separator="," > #{item} </foreach> </delete>
到此這篇關(guān)于Mybatis詳解動(dòng)態(tài)SQL以及單表多表查詢的應(yīng)用的文章就介紹到這了,更多相關(guān)Mybatis動(dòng)態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹,本文講解了如何使用責(zé)任鏈模式,并給出了4種使用實(shí)例,需要的朋友可以參考下2015-03-03java 使用簡單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大
本篇文章介紹了,在java中使用簡單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大。需要的朋友參考下2013-05-05springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解
這篇文章主要介紹了springboot @Controller和@RestController的區(qū)別及應(yīng)用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11springboot?vue測試平臺接口定義前后端新增功能實(shí)現(xiàn)
這篇文章主要介紹了springboot?vue測試平臺接口定義前后端新增功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05IDEA SpringBoot 項(xiàng)目配置Swagger2的詳細(xì)教程
這篇文章主要介紹了IDEA SpringBoot 項(xiàng)目配置Swagger2的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11