mybatis的学习3(select查询语句多个数据表查询collection、association的使用)

在使用select语句时,有时候需要查询两个表,可以使用表连接进行查询,也可以用mybatis给我们提供的collection标签
collection标签是在结果集映射的时候添加,也就是在resultMap标签添加,且不能放在result标签前面
collection标签属性有许多,但是基本用法只需要propertyselectcolumn
例如:

<resultMap>
	<id property="结果集映射主键的实体类参数" column="数据库主键字段"></id>
	<result property="其他属性" column="数据库其他字段"></result>
	<collection property="需要映射的其他实体类" select="dao层进行查询的方法" column="两个数据表之间的关联字段" ></collection>
	
</resultMap>

上面所写是关于两个表各自查询,然后通过关联的属性获取另外实体类的值,总体代码这里就不贴了,只要记住collection 是关于两个表各自查询,然后调用此标签,association是用来sql语句表连接查询的时候使用,可在这个标签下再次写入结果集映射
mybatis官网上的结果映射resultMap各个属性详解点击跳转
mybatis官网关于resultMap标签中的属性介绍


这里是详细代码,数据库表就不贴了
(1)两个实体类

package com.csh.bean;

public class Activity {
    private Integer activityId;
    private OrdinaryUser ordinaryUser;
    private String theCityName;
    private String title;
    private String content;


    public Integer getActivityId() {
        return activityId;
    }

    public void setActivityId(Integer activityId) {
        this.activityId = activityId;
    }

    public OrdinaryUser getOrdinaryUser() {
        return ordinaryUser;
    }

    public void setOrdinaryUser(OrdinaryUser ordinaryUser) {
        this.ordinaryUser = ordinaryUser;
    }

    public String getTheCityName() {
        return theCityName;
    }

    public void setTheCityName(String theCityName) {
        this.theCityName = theCityName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Activity{" +
                "activityId=" + activityId +
                ", ordinaryUser=" + ordinaryUser +
                ", theCityName='" + theCityName + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

package com.csh.bean;

import java.util.Date;

public class OrdinaryUser {
    private String userAccount;
    private String pwd;
    private Integer sex;
    private Date birthday;


    public String getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(String userAccount) {
        userAccount = userAccount;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "OrdinaryUser{" +
                "UserAccount='" + userAccount + '\'' +
                ", pwd='" + pwd + '\'' +
                ", sex=" + sex +
                ", birthday=" + birthday +
                '}';
    }
}

(2)dao层两个接口

public interface ActivityDao {
    public Activity selectAll(Integer activityId);
}

public interface OrdinaryUserDao {
    public OrdinaryUser selectAll(String userAccount);
}

(3)两个映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csh.dao.ActivityDao">
    <select id="selectAll" resultMap="activityAll">
        select * from activity where activity_id  = #{activityId}
    </select>
    <resultMap id="activityAll" type="com.csh.bean.Activity">
        <id property="activityId" column="activity_id"></id>
        <result property="theCityName" column="the_city_name"></result>
        <result property="title" column="title"></result>
        <result property="content" column="content"></result>
<!--        <association property="ordinaryUser" select="com.csh.dao.OrdinaryUserDao.selectAll" column="user_account" >-->
<!--            <id></id>-->
<!--            <result></result>-->
<!--        </association>-->
        <!--sql关联。fetchType:懒加载关闭-->
        <collection property="ordinaryUser" select="com.csh.dao.OrdinaryUserDao.selectAll" column="user_account" fetchType="eager"></collection>

    </resultMap>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csh.dao.OrdinaryUserDao">
    <select id="selectAll" resultMap="selectAll">
        select * from ordinary_user where user_account = #{userAccount}
    </select>
    <resultMap id="selectAll" type="com.csh.bean.OrdinaryUser">
        <id column="user_account" property="userAccount"></id>
        <result property="pwd" column="pwd"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
    </resultMap>
</mapper>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,可以使用associationcollection传递多参数子查询来实现复杂查询使用association可以将多个实体类关联起来,传递多个参数进行子查询。在映射文件中,可以使用<association>标签指定要关联的实体类,并通过refid属性指定关联实体类对应的子查询SQL语句。如果需要传递多个参数,可以在<association>标签内使用<collection>标签定义一个集合,集合中的每个元素都是传递的参数值。 例如,有两个实体类User和Order,可以通过association传递多个参数进行子查询: ``` <select id="getUserOrders" parameterType="java.util.Map" resultMap="userWithOrders"> SELECT * FROM user WHERE user_id = #{userId} </select> <resultMap id="userWithOrders" type="User"> <association property="orders" javaType="java.util.List" resultMap="orderMap"> <id property="userId" column="user_id" /> <collection property="orderIds" column="order_id" /> </association> </resultMap> <resultMap id="orderMap" type="Order"> <id property="orderId" column="order_id" /> <result property="orderName" column="order_name" /> </resultMap> ``` 在上述示例中,getUserOrders使用了Map作为参数类型,可以传递多个参数,其中的#{userId}为参数值。通过association关联了User和Order,User实体类中有一个orders属性,代表User和Order的关联关系。通过id和collection标签可以指定关联关系中每个实体类的属性和对应的子查询语句使用collection也可以实现类似的功能,但不需要两个实体类之间的关联关系。使用collection传递多个参数进行子查询的方式与association类似,只需将association标签替换为collection标签即可。 总之,MyBatis提供了associationcollection等标签,可以方便传递多个参数进行子查询,实现复杂的查询需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值