Lazy strategy principle: Only when the data returned by query SQL is used is actually issued to the database, otherwise it will not be issued (mainly used in joint queries of multiple tables)
1. One-to-one lazy loading:
Suppose there is a person table and a card table in the database: the person table has fields pid, pname, page, psex, cid, and card table has fields cid, cnum;
Suppose you want to query someone's name and ID number:
Principle: When querying the name, the actual version does not query the ID number. Only when the current station uses the ID number will it issue a query to the card. It is a strategy to query the ID number;
Implementation example:
Implementation steps:
1-Import mybatis dependency jar package
2-Add log4j file (can view the actual executing programs in memory)
1-Principle: Only when the current station uses the ID number is used, the query to the card will be issued, otherwise only the query to the person information is issued.
2-Open lazy: in conf.xml
<settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>
3. Implementation:
(1) In the mapper.xml mapping file:
<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: Specify the associated query statement
2-column: Specify which field's value in the main statement is passed as a parameter to the slave sql statement
(2) Define the method in the mapper interface:
public Person selectpersonAndCardLazyByPid(int pid);
(3) Test results using junit:
1. Here is a query that only sends person information;
@Test public void testselectpersonAndCardLazyByPid(){//lazy policy one-to-one Person p=pm.selectpersonAndCardLazyByPid(1); //System.out.println(p); System.out.println(p.getPname()+","); //System.out.println(p.getPname()+","+p.getCard().getCnum()); }The result execution query statement:
2. The query to the card is only issued when the current station uses the ID number
@Test public void testselectpersonAndCardLazyByPid(){//Lazy policy one-to-one Person p=pm.selectpersonAndCardLazyByPid(1); //System.out.println(p); System.out.println(p.getPname()+","); System.out.println(p.getPname()+","+p.getCard().getCnum());//The query for card is issued when the current station uses the ID number}The result execution query statement:
2. One-to-many lazy loading:
Implementation example:
Assume that there is a person table and a card identity information table in the database, the adder address table: where there are fields pid in the person table, pname, page, psex, cid, card table, and fields cnum in the adder table; the adder table has fields aid, ashi, pid
Suppose you want to query someone's name and address, and your ID number:
(1) mapper.xml mapping file:
<!-- lazy strategy one-to-many--> <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 interface definition method:
1. Here is a query that only sends person information;
@Test public void testselectpersonAndCardAndAdderLazyByPid(){//lazy policy one-to-many Person p=pm.selectpersonAndCardAndAdderLazyByPid(1); System.out.println(p.getPname()+",");//// Here is a query that only sends person information}The result execution query statement:
2. Here is a query to issue person information and identity information;
@Test public void testselectpersonAndCardAndAdderLazyByPid(){//lazy policy one-to-many Person p=pm.selectpersonAndCardAndAdderLazyByPid(1); System.out.println(p.getPname()+",");//Here is a query that only emits person information; System.out.println(p.getPname()+","+p.getCard().getCnum());//Here is a query that emits person information and identity information; }The result execution query statement:
3. Query of person information, identity information and address information is issued here;
@Test public void testselectpersonAndCardAndAdderLazyByPid(){//lazy policy one-to-many Person p=pm.selectpersonAndCardAndAdderLazyByPid(1); System.out.println(p.getPname()+",");//This is a query that only emits person information; System.out.println(p.getPname()+","+p.getCard().getCnum());//This is a query that emits person information and identity information; //System.out.println(p.getPname()+","+p.getCard().getCnum()); for (Adder adder : p.getAdder()) {///// Query of person information, identity information and address information is issued here; System.out.println(adder.getAshi()); } } The result execution query statement:
Summarize
The above is the method of delay loading Lazy strategy in mybatis introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!