Mybatis詳解動態(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í)行;
- ${} 會存在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)多表查詢中有相同的字段的話,就會報(bào)錯
<?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>
動態(tài)SQL使用
if標(biāo)簽
注冊分為必填和選填,如果在添加用戶的時(shí)候有不確定的字段傳入,就需要使用動態(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,如果沒有查詢條件,就會忽略where
其次where標(biāo)簽可以判斷第一個(gè)查詢條件前面有沒有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標(biāo)簽
和where的使用基本一樣
可以自動幫助你處理最后一個(gè)逗號,并且自動寫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詳解動態(tài)SQL以及單表多表查詢的應(yīng)用的文章就介紹到這了,更多相關(guān)Mybatis動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn)
本篇文章主要介紹了詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Java swing實(shí)現(xiàn)酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
SpringBoot?整合Security權(quán)限控制的初步配置
這篇文章主要為大家介紹了SpringBoot?整合Security權(quán)限控制的初步配置實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
idea中maven使用tomcat7插件運(yùn)行run報(bào)錯Could not start T
這篇文章主要介紹了idea中maven使用tomcat7插件運(yùn)行run報(bào)錯Could not start Tomcat問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
SpringBoot中的多RabbitMQ數(shù)據(jù)源配置實(shí)現(xiàn)
本篇博客將介紹如何在 Spring Boot 中配置和管理多個(gè) RabbitMQ 數(shù)據(jù)源,以滿足不同的應(yīng)用需求,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法
web 項(xiàng)目 能正常編譯,運(yùn)行時(shí)也正常啟動,但執(zhí)行到需要調(diào)用 org.codehaus.jackson 包中的某個(gè)方法時(shí),產(chǎn)生運(yùn)行異常,這篇文章主要介紹了Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法,需要的朋友可以參考下2024-05-05
Java實(shí)現(xiàn)各種文件類型轉(zhuǎn)換方式(收藏)
這篇文章主要介紹了Java?各種文件類型轉(zhuǎn)換的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03

