MaBatis的模糊查詢與儲存過程

2021-08-02 01:22:10 字數 1239 閱讀 1359

1、模糊查詢

如下模糊查詢語句:

select * from user where name like %o% and age between 16 and 20
是查詢名字中含字母o而且年齡在16到20歲之間的使用者

name like # and

age between # and #

顯然,三個引數我們需要直接傳入乙個物件,我們建立乙個專門用於模糊查詢的實體selectuser,包含三個屬性name,minage,maxage。然後我們測試如下:

@test

public void getuser()

2、儲存過程

回憶一下建立儲存過程的方式,sql語句如下:

delimiter $

create procedure mybatis.get_p_user_count(in ***_id int,out user_count int)

begin

if ***_id = 0 then

select count(*) from mybatis.`p_user` where p_user.***='女' into user_count;

else

select count(*) from mybatis.`p_user` where p_user.***='男' into user_count;

end if;

end$

使用如上語句可以建立乙個獲取男或者女的人數。呼叫方式如下:

#呼叫儲存過程

set @user_count = 0;

call get_p_user_count(1,@user_count);

select @user_count;

好了,有了這些基礎做鋪墊,就可以很好的使用mybatis來操作儲存過程了。配置如下:

其實也不是很難想到---傳入引數需要處理,要讓呼叫的語句知道哪乙個是入參,哪乙個是出參,入參和出參的名字都是什麼,而且必須宣告引數的jdbctype,所以就需要使用到parametermap屬性了。而且需要使用statementtype屬性來指定這是乙個儲存過程的操作。

測試如下:

@test

public void getusercount()

模糊查詢(儲存過程)

set ansi nulls on set quoted identifier on goalter procedure dbo searchvague description varchar 1000 descriptionlength int,pagenumber tinyint,product...

oracle 儲存過程模糊查詢

t sql的寫法 useadventureworks gocreate procedure findemployee emplname varchar 20 asselect emplname rtrim emplname select c.firstname,c.lastname,a.city f...

Redis使用(一) hash儲存與模糊查詢

看本文章的前提是已掌握redis的基礎操作,如插入 讀取 刪除資料等 示例使用shardedjedispool操作,實際開發可以使用redistemplate 目錄 redis hset 命令用於為雜湊表中的字段賦值 如果雜湊表不存在,乙個新的雜湊表被建立並進行 hset 操作。如果字段已經存在於雜...