lazy策略原理:只有在使用查詢sql返回的數(shù)據(jù)是才真正發(fā)出sql語句到數(shù)據(jù)庫,否則不發(fā)出(主要用在多表的聯(lián)合查詢)
1.一對一延遲加載:
假設(shè)數(shù)據(jù)庫中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;
假設(shè)要查詢某個人的姓名和身份證號碼:
原理:在查詢姓名時,實際本沒有查詢出身份證號碼的信息,只有當(dāng)前臺使用身份證號時才發(fā)出對card的查詢,需要查詢出身份證號碼是采取查詢的一種策略;
實現(xiàn)實例:
實現(xiàn)步驟:
1-導(dǎo)入mybatis 的依賴jar包
2-添加log4j文件 (可查看內(nèi)存中實際執(zhí)行的程序)
1-原理:只有當(dāng)前臺使用身份證號時才發(fā)出對card的查詢,否則只發(fā)出person信息的查詢
2-開啟lazy:在conf.xml
1
2
3
4
|
<settings> <setting name= "lazyloadingenabled" value= "true" /> <setting name= "aggressivelazyloading" value= "false" /> </settings> |
3.實現(xiàn):
(1)在mapper.xml映射文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<select id= "findcid" parametertype= "int" resulttype= "card" > select * from card where cid=#{value} </select> <resultmap type= "person" id= "p_c1" > <id column= "pid" property= "pid" /> <result column= "pname" property= "pname" /> <result column= "page" property= "page" /> <result column= "psex" property= "psex" /> <association property= "card" javatype= "card" select= "findcid" column= "cid" > </association> </resultmap> <select id= "selectpersonandcardlazybypid" parametertype= "int" resultmap= "p_c1" > select * from person where pid=#{value} </select> |
1-select:指定關(guān)聯(lián)的查詢語句
2-column:指定主語句中的哪個字段的值作為參數(shù)傳遞給從sql語句
(2)在mapper接口中定義方法:
1
|
public person selectpersonandcardlazybypid( int pid); |
(3)使用junit測試結(jié)果:
1.此處是只發(fā)出person信息的查詢;
1
2
3
4
5
6
7
|
@test public void testselectpersonandcardlazybypid(){ //lazy策略一對1 person p=pm.selectpersonandcardlazybypid( 1 ); //system.out.println(p); system.out.println(p.getpname()+ "," ); //system.out.println(p.getpname()+","+p.getcard().getcnum()); } |
結(jié)果執(zhí)行的查詢語句:
2.當(dāng)前臺使用身份證號時才發(fā)出對card的查詢
1
2
3
4
5
6
7
|
@test public void testselectpersonandcardlazybypid(){ //lazy策略一對1 person p=pm.selectpersonandcardlazybypid( 1 ); //system.out.println(p); system.out.println(p.getpname()+ "," ); system.out.println(p.getpname()+ "," +p.getcard().getcnum()); //當(dāng)前臺使用身份證號時才發(fā)出對card的查詢 } |
結(jié)果執(zhí)行的查詢語句:
2.一對多延遲加載:
實現(xiàn)實例:
假設(shè)數(shù)據(jù)庫中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid
假設(shè)要查詢某個人的姓名和住址,身份證號碼:
(1)mapper.xml映射文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- lazy策略一對多 --> <select id= "fingcard_adder" parametertype= "int" resulttype= "adder" > select * from adder where pid=#{value} </select> <select id= "findcid1" parametertype= "int" resulttype= "card" > select * from card where cid=#{value} </select> <resultmap type= "person" id= "p_c1_a1" > <id column= "pid" property= "pid" /> <result column= "pname" property= "pname" /> <result column= "page" property= "page" /> <result column= "psex" property= "psex" /> <association property= "card" javatype= "card" select= "findcid1" column= "cid" > </association> <collection property= "adder" oftype= "adder" select= "fingcard_adder" column= "pid" > </collection> </resultmap> <select id= "selectpersonandcardandadderlazybypid" parametertype= "int" resultmap= "p_c1_a1" > select * from person where pid=#{value} </select> |
(2)mapper接口定義方法:
1.此處是只發(fā)出person信息的查詢;
1
2
3
4
5
|
@test public void testselectpersonandcardandadderlazybypid(){ //lazy策略一對多 person p=pm.selectpersonandcardandadderlazybypid( 1 ); system.out.println(p.getpname()+ "," ); ////此處是只發(fā)出person信息的查詢 } |
結(jié)果執(zhí)行的查詢語句:
2.此處是發(fā)出person信息和身份信息的查詢;
1
2
3
4
5
6
|
@test public void testselectpersonandcardandadderlazybypid(){ //lazy策略一對多 person p=pm.selectpersonandcardandadderlazybypid( 1 ); system.out.println(p.getpname()+ "," ); //此處是只發(fā)出person信息的查詢; system.out.println(p.getpname()+ "," +p.getcard().getcnum()); //此處是發(fā)出person信息和身份信息的查詢; } |
結(jié)果執(zhí)行的查詢語句:
3.此處發(fā)出person信息和身份信息,地址信息的查詢;
1
2
3
4
5
6
7
8
9
10
|
@test public void testselectpersonandcardandadderlazybypid(){ //lazy策略一對多 person p=pm.selectpersonandcardandadderlazybypid( 1 ); system.out.println(p.getpname()+ "," ); //此處是只發(fā)出person信息的查詢; system.out.println(p.getpname()+ "," +p.getcard().getcnum()); //此處是發(fā)出person信息和身份信息的查詢; //system.out.println(p.getpname()+","+p.getcard().getcnum()); for (adder adder : p.getadder()) { ////此處發(fā)出person信息和身份信息,地址信息的查詢; system.out.println(adder.getashi()); } } |
結(jié)果執(zhí)行的查詢語句:
總結(jié)
以上所述是小編給大家介紹的mybatis中延遲加載lazy策略的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
原文鏈接:https://blog.csdn.net/m0_37905429/article/details/77074312