当前位置:网站首页>Dynamic SQL of ibatis
Dynamic SQL of ibatis
2022-07-05 11:28:00 【Full stack programmer webmaster】
1. Introduce
1 <select id="getUsers"
2
3 parameterClass="user"
4
5 resultMap="get-user-result">
6
7 select
8
9 id,
10
11 name,
12
13 sex
14
15 from t_user
16
17 <dynamic prepend="WHERE">
18
19 <isNotEmpty prepend="AND" property="name">
20
21 name like #name#
22
23 </isNotEmpty>
24
25 <isNotEmpty prepend="AND" property="address">
26
27 address like #address#
28
29 </isNotEmpty>
30
31 </dynamic>
32
33 </select>adopt dynamic node , You can define a dynamic WHERE Clause . this WHERE Clause may contain two statements for name and address The judgment condition of the field . Whether these two fields are added to the search depends on the query conditions provided by the user .
The semantics of this node is , If the parameter class ”name” Property is not empty (isNotEmpty, That is, a non empty string ””), In the generated SQL Where The sentence includes the judgment condition (name like #name#), among #name# Will take the name Property value padding .
name Property corresponds to isNotEmpty node , because ibatis Will automatically determine whether to add prepend Prefix , here (name like #name#) yes WHERE The first conditional clause in clause , There is no need to AND Prefix , So automatically omit .
Decision nodes are not limited to isNotEmpty,ibatis Provides a wealth of decision definition functions . It can be divided into two categories :
One 、 Monism
Unary decision is the decision of attribute value itself , If the attribute is NULL, Whether it is null, etc .
In the above example isNotEmpty It's a typical unary decision .
The unary decision node has :
<isPropertyAvailable> Whether this property is provided in the parameter class
<isNotPropertyAvailable> And <isPropertyAvailable> contrary
<isNull> Whether the attribute value is NULL
<isNotNull> And <isNull> contrary
<isEmpty> If the property is Collection perhaps String, Its size whether <1,
If not the above two types , Through
String.valueOf( Property value )
Get it String After the value of type , Judge it size whether <1
<isNotEmpty> And <isEmpty> contrary .
Two 、 Binary decision
Binary decision has two decision parameters , One is the attribute name , It's the decision value , Such as
<isGreaterThan prepend=”AND” property=”age”
compareValue=”18″>
(age=#age#)
</isGreaterThan>
among ,property=”age” Property name specified ”age”,compareValue=”18” To specify
The judgment value is ”18”.
The above decision node isGreaterThan The corresponding semantics is : If age Property is greater than
18(compareValue), It's in SQL Add (age=#age#) Conditions .
The binary decision nodes are :
The node name Property value and compareValues The relationship between
<isEqual> equal .
<isNotEqual> Unequal .
<isGreaterThan> Greater than
<isGreaterEqual> Greater than or equal to
<isLessThan> Less than
<isLessEqual> Less than or equal to
2. The Works of Liezi
For some special symbols , If greater than >、 Less than no. < You need to write in <![CDATA[]] China can effectively , Otherwise, it will fail . 1、 dynamic SQL fragment
adopt SQL Fragments achieve code reuse
1 <!-- Dynamic conditional paging query -->
2 <sql id="sql_count">
3 select count(*)
4 </sql>
5 <sql id="sql_select">
6 select *
7 </sql>
8 <sql id="sql_where">
9 from icp
10 <dynamic prepend="where">
11 <isNotEmpty prepend="and" property="name">
12 name like '%$name$%'
13 </isNotEmpty>
14 <isNotEmpty prepend="and" property="path">
15 path like '%path$%'
16 </isNotEmpty>
17 <isNotEmpty prepend="and" property="area_id">
18 area_id = #area_id#
19 </isNotEmpty>
20 <isNotEmpty prepend="and" property="hided">
21 hided = #hided#
22 </isNotEmpty>
23 </dynamic>
24 <dynamic prepend="">
25 <isNotNull property="_start">
26 <isNotNull property="_size">
27 limit #_start#, #_size#
28 </isNotNull>
29 </isNotNull>
30 </dynamic>
31 </sql>
32 <select id="findByParamsForCount" parameterClass="map" resultClass="int">
33 <include refid="sql_count"/>
34 <include refid="sql_where"/>
35 </select>
36 <select id="findByParams" parameterClass="map" resultMap="icp.result_base">
37 <include refid="sql_select"/>
38 <include refid="sql_where"/>
39
40 </select>2、 Number range query
The parameter name is fabricated , Non database fields , such as _img_size_ge、_img_size_lt Field
1 <isNotEmpty prepend="and" property="_img_size_ge">
2 <![CDATA[
3 img_size >= #_img_size_ge#
4 ]]>
5 </isNotEmpty>
6 <isNotEmpty prepend="and" property="_img_size_lt">
7 <![CDATA[
8 img_size < #_img_size_lt#
9 ]]>
10 </isNotEmpty>
11
12 Multiple use of one parameter is also allowed
1 <isNotEmpty prepend="and" property="_now">
2 <![CDATA[
3 execplantime >= #_now#
4 ]]>
5 </isNotEmpty>
6 <isNotEmpty prepend="and" property="_now">
7 <![CDATA[
8 closeplantime <= #_now#
9 ]]>
10 </isNotEmpty>3、 Time range query
1 <isNotEmpty prepend="" property="_starttime">
2 <isNotEmpty prepend="and" property="_endtime">
3 <![CDATA[
4 createtime >= #_starttime#
5 and createtime < #_endtime#
6 ]]>
7 </isNotEmpty>
8 </isNotEmpty> 4、in Inquire about
1 <isNotEmpty prepend="and" property="_in_state">
2 state in ('$_in_state$')
3 </isNotEmpty>5、like Inquire about
1 <isNotEmpty prepend="and" property="chnameone">
2 (chnameone like '%$chnameone$%' or spellinitial like '%$chnameone$%')
3 </isNotEmpty>
4 <isNotEmpty prepend="and" property="chnametwo">
5 chnametwo like '%$chnametwo$%'
6 </isNotEmpty> 6、or Conditions
1 <isEqual prepend="and" property="_exeable" compareValue="N">
2 <![CDATA[
3 (t.finished='11' or t.failure=3)
4 ]]>
5 </isEqual>
6
7 <isEqual prepend="and" property="_exeable" compareValue="Y">
8 <![CDATA[
9 t.finished in ('10','19') and t.failure<3
10 ]]>
11 </isEqual>7、where Subquery
1 <isNotEmpty prepend="" property="exprogramcode">
2 <isNotEmpty prepend="" property="isRational">
3 <isEqual prepend="and" property="isRational" compareValue="N">
4 code not in
5 (select t.contentcode
6 from cms_ccm_programcontent t
7 where t.contenttype='MZNRLX_MA'
8 and t.programcode = #exprogramcode#)
9 </isEqual>
10 </isNotEmpty>
11 </isNotEmpty>
12
13 <select id="findByProgramcode" parameterClass="string" resultMap="cms_ccm_material.result">
14 select *
15 from cms_ccm_material
16 where code in
17 (select t.contentcode
18 from cms_ccm_programcontent t
19 where t.contenttype = 'MZNRLX_MA'
20 and programcode = #value#)
21 order by updatetime desc
22 </select>9、 Use of functions
1 <!-- add to -->
2 <insert id="insert" parameterClass="RuleMaster">
3 insert into rulemaster(
4 name,
5 createtime,
6 updatetime,
7 remark
8 ) values (
9 #name#,
10 now(),
11 now(),
12 #remark#
13 )
14 <selectKey keyProperty="id" resultClass="long">
15 select LAST_INSERT_ID()
16 </selectKey>
17 </insert>
18 <!-- to update -->
19 <update id="update" parameterClass="RuleMaster">
20 update rulemaster set
21 name = #name#,
22 updatetime = now(),
23 remark = #remark#
24 where id = #id#
25 </update>10、map Result set
1 <!-- Dynamic conditional paging query -->
2 <sql id="sql_count">
3 select count(a.*)
4 </sql>
5 <sql id="sql_select">
6 select a.id vid,
7 a.img imgurl,
8 a.img_s imgfile,
9 b.vfilename vfilename,
10 b.name name,
11 c.id sid,
12 c.url url,
13 c.filename filename,
14 c.status status
15 </sql>
16 <sql id="sql_where">
17 From secfiles c, juji b, videoinfo a
18 where
19 a.id = b. videoid
20 and b.id = c.segmentid
21 and c.status = 0
22 order by a.id asc,b.id asc,c.sortnum asc
23 <dynamic prepend="">
24 <isNotNull property="_start">
25 <isNotNull property="_size">
26 limit #_start#, #_size#
27 </isNotNull>
28 </isNotNull>
29 </dynamic>
30 </sql>
31 <!-- Returns the total number of records not downloaded -->
32 <select id="getUndownFilesForCount" parameterClass="map" resultClass="int">
33 <include refid="sql_count"/>
34 <include refid="sql_where"/>
35 </select>
36 <!-- Return records that have not been downloaded -->
37 <select id="getUndownFiles" parameterClass="map" resultClass="java.util.HashMap">
38 <include refid="sql_select"/>
39 <include refid="sql_where"/>
40 </select>Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109452.html Link to the original text :https://javaforall.cn
边栏推荐
- Cron表达式(七子表达式)
- POJ 3176-Cow Bowling(DP||记忆化搜索)
- Detailed explanation of MATLAB cov function
- Solve the grpc connection problem. Dial succeeds with transientfailure
- Empêcher le navigateur de reculer
- I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
- [crawler] Charles unknown error
- How to make full-color LED display more energy-saving and environmental protection
- Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
- 【Office】Excel中IF函数的8种用法
猜你喜欢

R3Live系列学习(四)R2Live源码阅读(2)

Summary of thread and thread synchronization under window

comsol--三维图形随便画----回转

Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?

In the last process before the use of the risk control model, 80% of children's shoes are trampled here

Basics - rest style development

DDR4硬件原理图设计详解

pytorch训练进程被中断了

COMSOL--建立几何模型---二维图形的建立

CDGA|数据治理不得不坚持的六个原则
随机推荐
Technology sharing | common interface protocol analysis
go语言学习笔记-分析第一个程序
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
comsol--三维图形随便画----回转
Pytorch training process was interrupted
Golang application topic - channel
Stop saying that microservices can solve all problems!
Msfconsole command encyclopedia and instructions
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
Home office things community essay
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
Prevent browser backward operation
Basic part - basic project analysis
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
go语言学习笔记-初识Go语言
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Three suggestions for purchasing small spacing LED display
Leetcode 185 All employees with the top three highest wages in the Department (July 4, 2022)
PHP中Array的hash函数实现