软件编程
位置:首页>> 软件编程>> java编程>> Mybatis中连接查询和嵌套查询实例代码

Mybatis中连接查询和嵌套查询实例代码

作者:樱花树下的猫老师  发布时间:2021-08-23 13:24:31 

标签:mybatis,嵌套,查询
目录
  • 一、连接查询:

    • 1、多对一:

    • 2、一对多:

    • 3、多对多:

  • 二、嵌套查询:

    • 1、多对一:

    • 2、一对多:

首先在mysql中确立表:


#表一:地址国家表
CREATE TABLE address(aid INT AUTO_INCREMENT PRIMARY KEY,aname VARCHAR(20));

INSERT INTO address VALUES(NULL,"魏国");
INSERT INTO address VALUES(NULL,"蜀国");
INSERT INTO address VALUES(NULL,"吴国");

#表二:出场人物表
CREATE TABLE person(
pid INT AUTO_INCREMENT PRIMARY KEY,
pname VARCHAR(20),
paid INT,
CONSTRAINT pafk FOREIGN KEY person(paid) REFERENCES address(aid) ON UPDATE CASCADE ON DELETE CASCADE
);

INSERT INTO person VALUES(1,"曹操",1);
INSERT INTO person VALUES(2,"荀彧",1);
INSERT INTO person VALUES(3,"张辽",1);

INSERT INTO person VALUES(4,"刘备",2);
INSERT INTO person VALUES(5,"关羽",2);
INSERT INTO person VALUES(6,"张飞",2);
INSERT INTO person VALUES(7,"诸葛亮",2);

INSERT INTO person VALUES(8,"孙权",3);
INSERT INTO person VALUES(9,"周瑜",3);
INSERT INTO person VALUES(10,"陆逊",3);

INSERT INTO person VALUES(11,"公孙瓒",NULL);

#表三:交通工具表
CREATE TABLE tool(tid INT AUTO_INCREMENT PRIMARY KEY,tname VARCHAR(20));
INSERT INTO tool VALUES(1,"马");
INSERT INTO tool VALUES(2,"船");

#表四:地址国家——交通工具 多对多关系表
CREATE TABLE aandt(
a_aid INT,
a_tid INT,
PRIMARY KEY(a_aid,a_tid),#联合主键,是指多个字段组成一个组合,该组合在数据表中唯一
CONSTRAINT FOREIGN KEY aandt(a_aid) REFERENCES address(aid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY aandt(a_tid) REFERENCES tool(tid) ON UPDATE CASCADE ON DELETE CASCADE
);

INSERT INTO aandt VALUES(1,1);
INSERT INTO aandt VALUES(2,1);
INSERT INTO aandt VALUES(2,2);
INSERT INTO aandt VALUES(3,2);

查询a表的所有信息,如果a表的信息有对应的b表的信息,则查询b表的信息,如果没有,则不查询。

多对一,如:查询所有人物信息,如果人物有对应国家,则查询国家信息,如果没有,则不查询。多个人物属于一个国家。

一对多,如:查询所有国家信息,如果国家有对应人物,则查询人物信息,如果没有,则不查询。一个国家拥有多个城市。

多对多,如:查询所有国家信息,如果国家拥有对应的交通工具,则查询交通工具信息,没有则不查询。与此同时,多种交通工具存在于于多个国家。

一、连接查询:

连接查询使用时,使用偏向于a表所在方向的外连接,可获得a表所有信息,和对应的b表信息。该方式为饿汉式,内存占用较大,但对数据库访问次数较少而导致消耗时间少。

1、多对一:


<!--多对一的数据库-->
<mapper namespace="com.fh.dao.PersonDao">
<!--该映射的id为map1,该映射的内容为:将查询到的字段的结果值,按照本映射的对应关系,分别封装在Person实体类下的各个属性上,整体构成Person-->
<resultMap id="map1" type="com.fh.domain.Person">
 <id column="pid" property="pid"/><!--id:主键映射; column:数据库表字段; property:类中对应属性名-->
 <result column="pname" property="pname"/><!--result:非主键映射-->
 <result column="paid" property="paid"/>
 <association property="address" javaType="com.fh.domain.Address"><!--association:在查询到多,然后对应出一时,用于关联对应出一的一方; property:查询类中包含的子对象属性; javaType:子对象属性封装对应的类-->
  <id column="aid" property="aid"/>
  <result column="aname" property="aname"/>
 </association>
</resultMap>
<select id="findAllPerson" resultMap="map1">/*resultMap:自己编写的结果集,本查询的返回值正是该结果集所对应的映射组构成的Person*/
--   查询所有人及其对应的地址,以人为主,则对人侧外连接
 SELECT * FROM person p LEFT OUTER JOIN address a ON p.paid = a.aid
</select>
</mapper>

2、一对多:


<!--一对多的数据库-->
<mapper namespace="com.fh.dao.AddressDao">
<resultMap id="map2" type="com.fh.domain.Address">
 <id column="aid" property="aid"/>
 <result column="aname" property="aname"/>
 <collection property="personList" ofType="com.fh.domain.Person"><!--collection:查询到一,接着关联对应出多时,指向多的一方; foType:集合中每个元素所属的映射类-->
  <id column="pid" property="pid"/>
  <result column="pname" property="pname"/>
  <result column="paid" property="paid"/>
 </collection>
</resultMap>
<select id="findAllAddress" resultMap="map2">
 SELECT * FROM address a LEFT OUTER JOIN person p ON a.aid = p.paid;
</select>
</mapper>

3、多对多:


<mapper namespace="com.fh.dao.ToolDao">
<resultMap id="map3" type="com.fh.domain.Tool">
 <id column="tid" property="tid"/>
 <result column="tname" property="tname"/>
 <collection property="addressList" ofType="com.fh.domain.Address">
  <id column="aid" property="aid"/>
  <result column="aname" property="aname"/>
 </collection>
</resultMap>
<select id="findAllTool" resultMap="map3">
 SELECT t.*,a.* FROM tool AS t LEFT OUTER JOIN aandt AS a_t ON t.`tid` = a_t.`a_tid` LEFT OUTER JOIN address AS a ON a_t.`a_aid` = a.`aid`;
</select>

二、嵌套查询:

嵌套查询使用时,先查询a表的信息,然后依赖a和b表的外键约束,利用in(),再次查询b表对应到a表上的信息。该方式可以改为饿汉式,内存使用较小,但需要多次访问数据库而导致消耗时间多。

1、多对一:

PersonDao接口内写入:


//查询所有人,以及其对应的地址
List<Person> findPersonFromAddress();

对应映射配置中:


<!--多对一的数据库-->
<mapper namespace="com.fh.dao.PersonDao">
<!--person映射的基本属性对应下面person的结果集,本结果集内部再继续进行处理-->
<resultMap id="map1" type="com.fh.domain.Person">
 <id column="pid" property="pid"/>
 <result column="pname" property="pname"/>
 <result column="paid" property="paid"/>
 <!--
 对应到person内的子属性对象,column内为person对应到address的外键,由此外键,传入select内的方法进行二次嵌套查询,交由AddressDao在其中的指定方法进行处理
 即要求查询目标为:Address.aid = Person.paid
 -->
 <association property="address" column="paid" select="com.fh.dao.AddressDao.findAddressById"/>
</resultMap>
<!--第一次查询为只查询主要对象,自定义结果集-->
<select id="findPersonFromAddress" resultMap="map1">
 select * from person
</select>
</mapper>

继续编写指向AddressDao接口中的findAddressById:


//按照id查询Address
List<Address> findAddressById(Integer id);

回到AddressDao配置文件:


<mapper namespace="com.fh.dao.AddressDao">
<!--根据id对address查询-->
<select id="findAddressById" resultType="com.fh.domain.Address">
 SELECT * FROM address WHERE aid = #{aid}
</select>

2、一对多:

AddressDao接口内写入:


List<Address> findAddressWithPerson();

其对应映射配置中:


<resultMap id="map2" type="com.fh.domain.Address">
 <id column="aid" property="aid"/>
 <result column="aname" property="aname"/>
 <collection property="personList" column="aid" select="com.fh.dao.PersonDao.findPersonById"/>
</resultMap>
<select id="findAddressWithPerson" resultMap="map2">
 select * from address
</select>

针对指出的PersonDao接口的findPersonById:


List<Person> findPersonById(Integer id);

其对应的映射配置中:


<select id="findPersonById" resultType="com.fh.domain.Person">
 select * from person where pid = #{pid}
</select>

对于嵌套查询的延迟加载问题,需添加配置:

方法一:

association或collection中多加一条属性:fetchType=“lazy”

方法二:


<settings>
 <setting name="lazyLoadingEnable" value="true"/>
 <setting name="lazyLoadTriggerMethods" value="true"/><!--本条设置表示将包括原本不会延迟加载的equals/clone/hashCode/toString在内所有方法进行延迟加载-->
</settings>

来源:https://blog.csdn.net/u011798348/article/details/96843646

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com