1. 程式人生 > >Mybatis動態sql語句(OGNL語法)

Mybatis動態sql語句(OGNL語法)

type 語法 key ognl select 動態 font tab class

下面是Mybatis動態sql語句(即OGNL語法)的簡單案例

1.創建表

技術分享圖片
create table test(id int primary key auto_increment,name varchar(20),job varchar(20),dept varchar(20),sal int) charset=utf8;

insert into test values
(null,魯班,java,,1456),
(null,後裔,java,,2440),
(null,劉禪,c,,3540),
(null,劉備,python,,4505),
(
null,關羽,python,,1470), (null,張飛,c,,2345), (null,狄仁傑,java,,3640), (null,蘭陵王,c,,4000), (null,花木蘭,c,,1000), (null,諸葛亮,java,,2047), (null,甄姬,python,,3000), (null,小喬,c,,4000), (null,女蝸,java,,1000), (null,妲己,java,,6705), (null,公孫策,java,,null), (null
,百裏守約,c,,null), (null,小劉,python,,842), (null,蔡文姬,python,null,500);
創建 test 表

2.<if> 標簽的使用

    <select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean">
    
        select * from test where
            <if test="job!=null">    <!--------------------這裏的 job
本質調用parameterType參數的 get()方法
--> name=#{} <!--------------------所以參數不能是基本類型(因為基本類型和包裝類沒有 get()方法) --> </if> or name=‘後裔‘   <!--------------------如果 job 等於 null(即 參數.getJob() 等於null) ,那麽語句這段sql語句變成如下所示 --> <!--------------------select * from where or name=‘後裔‘ 報錯!,這時候要用 where 標簽防止這種情況發生 --> </select>

---恢復內容結束---

Mybatis動態sql語句(OGNL語法)