當前位置:首頁 » 編程語言 » mybatissql標簽
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

mybatissql標簽

發布時間: 2022-01-21 07:43:48

Ⅰ mybatis的怎麼在select標簽里引用where標簽

[html] view plain
<sql id="query_user_where">
<!-- 如果 userQueryVo中傳入查詢條件,再進行sql拼接-->
<!-- test中userCustom.username表示從userQueryVo讀取屬性值-->
<if test="userCustom!=null">
<if test="userCustom.username!=null and userCustom.username!=''">
and username like '%${userCustom.username}%'
</if>
<if test="userCustom.sex!=null and userCustom.sex!=''">
and sex = #{userCustom.sex}
</if>
<!-- 根據id集合查詢用戶信息 -->
<!-- 最終拼接的效果:
SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND id IN (16,22,25)
collection:集合的屬性
open:開始循環拼接的串
close:結束循環拼接的串
item:每次循環取到的對象
separator:每兩次循環中間拼接的串
-->
<foreach collection="ids" open=" AND id IN ( " close=")" item="id" separator=",">
#{id}
</foreach>
<!--
SELECT id ,username ,birthday FROM USER WHERE username LIKE '%小明%' AND (id = 16 OR id = 22 OR id = 25)
<foreach collection="ids" open=" AND ( " close=")" item="id" separator="OR">
id = #{id}
</foreach>
-->
<!-- 還有很的查詢條件 -->
</if>
</sql>
[html] view plain
</span><select id="findUserList" parameterType="userQueryVo" resultType="user">
select id,username,birthday from user
<!-- where標簽相當 於where關鍵字,可以自動去除第一個and -->
<where>
<!-- 引用sql片段,如果sql片段和引用處不在同一個mapper必須前邊加namespace -->
<include refid="query_user_where"></include>
<!-- 下邊還有很其它的條件 -->
<!-- <include refid="其它的sql片段"></include> -->
</where>
</select>

Ⅱ mybatis sql屬性用sql標簽封裝出問題了 應該怎麼改

username=#{1},password=#{2} where id=#{0}這些代碼寫的有問題,應該是對應的UserInfo裡面的屬性名,如useName,password,id等。

Ⅲ mybatis怎麼展示sql語句

mybatis的sql和你在資料庫客戶端執行的sql是一樣的,但是在mybatis中調用的sql一般都是動態的,所以用到了參數傳遞。這個mybatis有對應的標簽以及相應的變數來實現。你可以搜索下mybatis標簽。同時給你一個參考的你看看,這個是一個查詢用戶的
<select id="queryUsers" parameterType="map" resultType="xx.xx.xx.bean.UserBean">
<![CDATA[
select
ID,
LOGIN_NAME AS loginName,
PASSWORD,
REAL_NAME AS realName,
POSITION,
(SELECT D.POSITION_NAME FROM UNIT_POSITION D WHERE D.POSITION_CODE=T.POSITION) POSITIONNAME,
USER_TYPE AS userType,
SEX,
PID,
TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD') BIRTHDAY,
EMAIL,
CONTACT_TEL AS contactTel,
CONTACT_MOBILE AS contactMobile,
CONTACT_FAX AS contactFax,
CONTACT_ZIP AS contactZip,
CONTACT_ADDR AS contactAddr,
STATUS,
EDUCATION,
(SELECT D.EDUCATION_NAME FROM UNIT_EDUCATION D WHERE D.EDUCATION_CODE=T.EDUCATION AND D.STATUS=0) EDUCATIONNAME,
NATION,
POLITICAL,
REMARK,
TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD HH24:MI:SS') createDate,
(SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.CREATE_USER_ID) createUserId,
TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD HH24:MI:SS') updateDate,
(SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.UPDATE_USER_ID) updateUserId
from UNIT_USER T
]]>
<where>
T.STATUS='1'
<if test="realName !=null and realName !=''">
and T.REAL_NAME like '%${realName}%'
</if>
<if test="nexusDpartment !=null">
AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE IN (${nexusDpartment}))
</if>
<if test="deptCode !=null and deptCode !=''">
AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE = #{deptCode})
</if>
</where>
<if test="sort != null and sort != ''">
order by ${sort}
<if test="direction != null and direction != ''">
${direction}
</if>
</if>
</select>

Ⅳ mybatis的sql標簽 怎麼寫 like

selectafromtablewhereb=1andclikeconcat('%',d,'%');

Ⅳ mybatis框架配置文件里執行sql語句常用的標簽有哪些,以及動態語錄怎麼拼接

select indert update delete

where 1=1
<if test='...條件...『>
and ...
</if>

Ⅵ mybatis映射文件中,以下哪個標簽是引入其他自定義sql片段的標簽

</sql>
<select id="find" resultType="*" resultMap="*">
<include refid="select"/>
</select>

Ⅶ mybatis標簽問題疑惑,什麼是sql dialect

mybatis的sql和你在資料庫客戶端執行的sql是一樣的,但是在mybatis中調用的sql一般都是動態的,所以用到了參數傳遞。這個mybatis有對應的標簽以及相應的變數來實現。你可以搜索下mybatis標簽。同時給你一個參考的你看看,這個是一個查詢用戶的
041424344454647484950<select id="queryUsers" parameterType="map" resultType="xx.xx.xx.bean.UserBean"> <![CDATA[ select ID, LOGIN_NAME AS loginName, PASSWORD, REAL_NAME AS realName, POSITION, (SELECT D.POSITION_NAME FROM UNIT_POSITION D WHERE D.POSITION_CODE=T.POSITION) POSITIONNAME, USER_TYPE AS userType, SEX, PID, TO_CHAR(T.BIRTHDAY,'YYYY-MM-DD') BIRTHDAY, EMAIL, CONTACT_TEL AS contactTel, CONTACT_MOBILE AS contactMobile, CONTACT_FAX AS contactFax, CONTACT_ZIP AS contactZip, CONTACT_ADDR AS contactAddr, STATUS, EDUCATION, (SELECT D.EDUCATION_NAME FROM UNIT_EDUCATION D WHERE D.EDUCATION_CODE=T.EDUCATION AND D.STATUS=0) EDUCATIONNAME, NATION, POLITICAL, REMARK, TO_CHAR(T.CREATE_DATE,'YYYY-MM-DD HH24:MI:SS') createDate, (SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.CREATE_USER_ID) createUserId, TO_CHAR(T.UPDATE_DATE,'YYYY-MM-DD HH24:MI:SS') updateDate, (SELECT D.REAL_NAME FROM UNIT_USER D WHERE D.ID= T.UPDATE_USER_ID) updateUserId from UNIT_USER T ]]> <where> T.STATUS='1' <if test="realName !=null and realName !=''"> and T.REAL_NAME like '%${realName}%' </if> <if test="nexusDpartment !=null"> AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE IN (${nexusDpartment})) </if> <if test="deptCode !=null and deptCode !=''"> AND T.ID IN (SELECT DISTINCT D.USER_ID FROM UNIT_USER_DEPT D WHERE D.DEPT_CODE = #{deptCode}) </if> </where> <if test="sort != null and sort != ''"> order by ${sort} <if test="direction != null and direction != ''"> ${direction} </if> </if> </select>

Ⅷ 6.mybatis裡面的動態sql是怎麼設定的,常用標簽有那些以及其

1、動態SQL片段
通過SQL片段達到代碼復用
<!-- 動態條件分頁查詢 -->
<sql id="sql_count">
select count(*)
</sql>
<sql id="sql_select">
select *
</sql>
<sql id="sql_where">
from icp
<dynamic prepend="where">
<isNotEmpty prepend="and" property="name">
name like '%$name$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="path">
path like '%path$%'
</isNotEmpty>
<isNotEmpty prepend="and" property="area_id">
area_id = #area_id#
</isNotEmpty>
<isNotEmpty prepend="and" property="hided">
hided = #hided#
</isNotEmpty>
</dynamic>
<dynamic prepend="">
<isNotNull property="_start">
<isNotNull property="_size">
limit #_start#, #_size#
</isNotNull>
</isNotNull>
</dynamic>
</sql>
<select id="findByParamsForCount" parameterClass="map" resultClass="int">
<include refid="sql_count"/>
<include refid="sql_where"/>
</select>
<select id="findByParams" parameterClass="map" resultMap="icp.result_base">
<include refid="sql_select"/>
<include refid="sql_where"/>
</select>

2、數字范圍查詢
所傳參數名稱是捏造所得,非資料庫欄位,比如_img_size_ge、_img_size_lt欄位
<isNotEmpty prepend="and" property="_img_size_ge">
<![CDATA[
img_size >= #_img_size_ge#
]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="_img_size_lt">
<![CDATA[
img_size < #_img_size_lt#
]]>
</isNotEmpty>

多次使用一個參數也是允許的
<isNotEmpty prepend="and" property="_now">
<![CDATA[
execplantime >= #_now#
]]>
</isNotEmpty>
<isNotEmpty prepend="and" property="_now">
<![CDATA[
closeplantime <= #_now#
]]>
</isNotEmpty>

3、時間范圍查詢
<isNotEmpty prepend="" property="_starttime">
<isNotEmpty prepend="and" property="_endtime">
<![CDATA[
createtime >= #_starttime#
and createtime < #_endtime#
]]>
</isNotEmpty>
</isNotEmpty>

Ⅸ mybatis設置sql通過什麼標簽

設置數據類型的話,以製作的可變數據標簽為例子,設置的話需要在軟體上方「資料庫設置」,添加對應的數據類型,之後在彈出的對應窗口中,通過選擇電腦上面的資料庫類型文件,之後今天添加即可。

Ⅹ mybatis sql標簽<update>怎麼獲取返回值

mybatis的update默認返回操作記錄的條數,先定義一個resultMap:
<resultMap id="integer" type="java.lang.Integer">
</resultMap>
然後標簽裡面返回integer:
<update id="do..." resultMap="integer"></update>
在定義介面裡面返回Integer即可:
public Integer do...();