Mybatis + mysql query to achieve two-stage cascade
First, demand: according to Mybatis achieve level 2 cascading queries.
All inquiries Municipal Information below it according to id province.
Second, the implementation steps
method one:
Thought: this case is " one to many ", we are one of mapper.xml in, with collection to define a collection of more than can be.
property ---> corresponding to the entity class, the subclass number, List ofType ---> corresponding to the questions that subclass column ----> parent key transmitted to the external subclass query SELECT ----> subclass query
1. Define parent entity class + subclass
package com.imocc.mall.pojo; import lombok.Data; import java.util.List; /** * @author wy */ @Data public class Province { private Integer id; private String provinceid; private String province; private List<City> list; @Override public String toString() { return "Province{" + "id=" + id + ", provinceid='" + provinceid + '\'' + ", province='" + province + '\'' + ", list=" + list + '}'; } }
package com.imocc.mall.pojo; import lombok.Data; @Data public class City { private Integer id; private String cityid; private String city; private String father; }
2.mapper class
2. mapperxml file
3. Test
JDBC Connection [HikariProxyConnection@1532915766 wrapping com.mysql.cj.jdbc.ConnectionImpl@62dbe64e] will not be managed by Spring ==> Preparing: select * from hat_province where provinceID =? ==> Parameters: 110000(String) <== Columns: id, provinceID, province <== Row: 1, 110000, 北京市 ====> Preparing: select id, cityID, city, father from hat_city where father = ? ====> Parameters: 110000(String) <==== Columns: id, cityID, city, father <==== Row: 1, 110100, 市辖区, 110000 <==== Row: 2, 110200, 县, 110000 <==== Total: 2 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8f2098e] Province{id=1, provinceid='110000', province='北京市', list=[City(id=1, cityid=110100, city=市辖区, father=110000), City(id=2, cityid=110200, city=县, father=110000)]}