Hibernate使用MySQL自定義函式

2021-09-02 18:25:57 字數 4888 閱讀 9064

新建表

create

table

`sys_user`

(`id`

varchar(32

)not

null

comment

'id'

,`name`

varchar(50

)not

null

comment

'登入名'

,`parent_id`

varchar(32

)default

null

comment

'父id'

,primary

key(

`id`),

key`fkl6y9o1vwawvu2w5o3pgioj3yd`

(`parent_id`),

constraint

`fkl6y9o1vwawvu2w5o3pgioj3yd`

foreign

key(

`parent_id`

)references

`sys_user`

(`id`))

engine

=innodb

default

charset

=utf8;

測試資料

insert

into

`sys_user`

values

('0'

,'0'

,null);

insert

into

`sys_user`

values

('10'

,'10'

,'0');

insert

into

`sys_user`

values

('1001'

,'1001'

,'10');

insert

into

`sys_user`

values

('100101'

,'100101'

,'1001');

insert

into

`sys_user`

values

('10010101'

,'10010101'

,'100101');

insert

into

`sys_user`

values

('1001010101'

,'1001010101'

,'10010101');

insert

into

`sys_user`

values

('10010102'

,'10010102'

,'100101');

insert

into

`sys_user`

values

('100102'

,'100102'

,'1001');

insert

into

`sys_user`

values

('1002'

,'1002'

,'10');

insert

into

`sys_user`

values

('20'

,'20'

,'0');

insert

into

`sys_user`

values

('2001'

,'2001'

,'20');

insert

into

`sys_user`

values

('200101'

,'200101'

,'2001');

insert

into

`sys_user`

values

('20010101'

,'20010101'

,'200101');

insert

into

`sys_user`

values

('20010102'

,'20010102'

,'200101');

insert

into

`sys_user`

values

('200102'

,'200102'

,'2001');

insert

into

`sys_user`

values

('2002'

,'2002'

,'20');

insert

into

`sys_user`

values

('2003'

,'2003'

,'20');

insert

into

`sys_user`

values

('30'

,'30'

,'0');

insert

into

`sys_user`

values

('3001'

,'3001'

,'30');

insert

into

`sys_user`

values

('3002'

,'3002'

,'30');

insert

into

`sys_user`

values

('300201'

,'300201'

,'3002');

insert

into

`sys_user`

values

('300202'

,'300202'

,'3002');

insert

into

`sys_user`

values

('300203'

,'300203'

,'3002');

insert

into

`sys_user`

values

('30020301'

,'30020301'

,'300203'

);

執行函式生成語句

drop

function

ifexists

`getchildlist`

;-- 不寫這一句有時候會報錯

setglobal log_bin_trust_function_creators=

true

;-- 建立函式

create

function

`getchildlist`

(rootid varchar

(100))

returns

varchar

(10000

)charset utf8

begin

-- 宣告變數

declare schildlist varchar

(10000);

declare schildtemp varchar

(1000);

-- 賦值

set schildtemp =rootid;

-- 具體實現邏輯

while schildtemp is

notnull

doif

(schildlist is

notnull

)then

set schildlist = concat(schildtemp,

',',schildlist)

;else

set schildlist = concat(schildtemp)

;endif;

-- 結果處理

select group_concat(id)

into schildtemp from sys_user where find_in_set(parent_id, schildtemp)

>0;

endwhile

;return schildlist;

end

測試一下

select

*from sys_user where find_in_set(id,getchildlist(

'10'))

>

0

結果

}使用自定義的mysql方言

key=

"hibernate.dialect"

>

com.demo.util.mylocaldialectprop

>

hql

select u from user u where 1

=1 and find_in_set

(u.id,

getchildlist

(:userid)

)>

0

python使用peewee實現mysql資料操作

peewee可用class來建立表,增刪改查,應該是相對餘單錶 本人幾乎沒用過,自以為如此 想實現sql查詢,得到list,比如這樣的結果 但是查詢結果是全是資料 元組 且找不到列名,後dir後逐個嘗試發現列名可以使用result.description j 0 獲取 元組使用起來不方便,現實現將資...

Zeppelin使用JDBC連線MySQL

在interpreter列表頁create new interpreter。輸入名稱 mysql interpreter group 選擇jdbc properties 修改 name value default.driver com.mysql.jdbc.driver default.url jd...

contos 使用docker安裝mysql

安裝docker yum install docker檢視docker是否安裝成功 docker v 檢視docker版本啟動docker 兩種啟動方式 systemctl start docker 正常啟動 systemctl enable docker 開機啟動常用命令 docker image...