方式一:用「%$%」
"selectuser"
parametertype
="com.gec.bean.user"
resulttype
="com.gec.bean.user"
>
select * from t_user where address like "%$%"
select
>
方式二:用「#{value}」,然後傳入引數時加入「%」
"selectuser"
parametertype
="com.gec.bean.user"
resulttype
="com.gec.bean.user"
>
select * from t_user where address like #
select
>
@test
public
void
testselectuser()
session.
close()
;}
以下三種方式能實現乙個或多個等值條件的查詢
1、用if標籤,新增where子句後的條件
"dynamicselect"
parametertype
="com.gec.bean.user"
resulttype
="com.gec.bean.user"
>
select * from t_user where 1=1
test
="username!=null"
>
and username=#
if>
test
="address!=null"
>
and address like #
if>
select
>
2、用where標籤新增where子句,在where標籤中新增條件
"dynamicselect2"
parametertype
="com.gec.bean.user"
resulttype
="com.gec.bean.user"
>
select * from t_user
>
test
="username!=null"
>
and username=#
if>
test
="address!=null"
>
and address like #
if>
where
>
select
>
3、在where標籤中,用choose-when新增條件
"dynamicselect3"
parametertype
="com.gec.bean.user"
resulttype
="com.gec.bean.user"
>
select * from t_user
>
>
test
="username!=null"
>
and username=#
when
>
test
="address!=null"
>
and address like #
when
>
choose
>
where
>
select
>
3、插入回填
主鍵在資料庫中是自增長,插入資料時,不管插入的id有沒有值都會被自增長的主鍵覆蓋。插入完成後,可以通過「插入回填」,把自增長的id回填得到pojo物件中
"insert"
parametertype
="com.gec.bean.user"
>
insert into t_user(id,address,username,birthday) values(#,#,#,#)
keyproperty
="id"
keycolumn
="id"
resulttype
="int"
order
="after"
>
select last_insert_id()
selectkey
>
insert
>
@test
public
void
testinsert()
catch
(ioexception e)
}
執行上面的測試,新增乙個user到資料庫,該user的在資料庫中的id為:
SQ 模糊查詢
between.and.在資料庫內部是做作特殊優化的,執行效率比 and 等這種方式快 between a and b 相當於 字段 a and欄位 b 例如 select from dbo.mystudent where s age between 20 and 30 between and還可以...
mybatis 動態Sql的模糊查詢
1 where teacher.tname like concat concat 2 distinct的使用 下面先來看看例子 table id name 1 a2 b 3 c4 c 5 b比如我想用一條語句查詢得到name不重複的所有資料,那就必須使用distinct去掉多餘的重覆記錄。selec...
Mybatis 模糊查詢
mybatis從入門到精通 書籍筆記 1 使用concat 字串連線函式and user name like concat and user name like concat concat mysql中concat函式可以連線多個引數,oracle中只支援2個引數,所以有些要用多個concat 函式...