欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis多表操作查询功能

程序员文章站 2022-03-24 20:49:45
一对一查询用户表和订单表的关系为,一个用户多个订单,一个订单只从属一个用户一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户在只查询order表的时候,也要查询user表,所以需要将所有数...

一对一查询

用户表和订单表的关系为,一个用户多个订单,一个订单只从属一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

MyBatis多表操作查询功能

在只查询order表的时候,也要查询user表,所以需要将所有数据全部查出进行封装select *,o.id oid from orders o,user u where o.uid=u.id

MyBatis多表操作查询功能

创建order和user实体

order

public class order {
    private int id;
    private date ordertime;
    private double total;
    //表示当前订单属于哪一个用户
    private user user;

user

public class user {
    private int id;
    private string username;
    private string password;
    private date birthday;

创建ordermapper接口

public interface usermapper {
//查询全部的方法
    public list<order> findall();
}

配置ordermapper.xml

<?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.zg.mapper.ordermapper" >

    <resultmap id="ordermap" type="order">
        <!--手动指定字段与实体的映射关系-->
        <!--comlumn:数据表的字段名称 property:实体的属性名称-->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>

    </resultmap>

    <!--这里不能使用resulttype=“order”,因为order中没有user中的字段,只有一个user对象-->
    <select id="findall" resultmap="ordermap">
        select *,o.id oid from orders o,user u where o.uid=u.id
    </select>

</mapper>


sqlmapconfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--主要配置mybatis的核心配置-->
<configuration>
    <!--通过properties标签加载外部properties文件-->
    <properties resource="jdbc.properties"></properties>
    <!--自定义别名-->
    <typealiases>
        <typealias type="com.zg.domain.user" alias="user"></typealias>
        <typealias type="com.zg.domain.order" alias="order"></typealias>
    </typealiases>

    <!--配置当前数据源的环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionmanager type="jdbc"></transactionmanager>
            <datasource type="pooled">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </datasource>
        </environment>
    </environments>
    <!--加载映射文件-->
    <mappers>
        <mapper resource="com.zg.mapper/usermapper.xml"></mapper>
        <mapper resource="com.zg.mapper/ordermapper.xml"></mapper>
    </mappers>


</configuration>

MyBatis多表操作查询功能

在一对一配置的时候,在order实体中创建了一个user,所以property属性都使用user.** 的方式进行编写,但是这里还可以使用association

<?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.zg.mapper.ordermapper" >

    <resultmap id="ordermap" type="order">
        <!--手动指定字段与实体的映射关系-->
        <!--comlumn:数据表的字段名称 property:实体的属性名称-->
        <id column="oid" property="id"></id>
        <result column="ordertime" property="ordertime"></result>
        <result column="total" property="total"></result>
        <!--<result column="uid" property="user.id"></result>
        <result column="username" property="user.username"></result>
        <result column="password" property="user.password"></result>
        <result column="birthday" property="user.birthday"></result>-->
        <!--以上被封装到user内的还可以使用association进行配置-->
        <!--association匹配的意思,在order中有个属性叫user-->
        <!-- property="user"当前order实体中的属性名称,javatype="user"当前实体order中的属性类型user-->
        <association property="user" javatype="user">
            <id column="uid" property="id"></id>
            <result column="username" property="username"></result>
            <result column="password" property="password"></result>
            <result column="birthday" property="birthday"></result>
        </association>
        

    </resultmap>

    <!--这里不能使用resulttype=“order”,因为order中没有user中的字段,只有一个user对象-->
    <select id="findall" resultmap="ordermap">
        select *,o.id oid from orders o,user u where o.uid=u.id
    </select>

</mapper>


一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个当但只从属一个用户
一对多查询需求:查询一个用户,与此同时查询出该用户具有的订单

MyBatis多表操作查询功能

package com.zg.domain;

import java.util.date;
import java.util.list;

public class user {
    private int id;
    private string username;
    private string password;
    private date birthday;

    //描述当前用户存在哪些订单
    private list<order>  orderlist;

    public list<order> getorderlist() {
        return orderlist;
    }

    public void setorderlist(list<order> orderlist) {
        this.orderlist = orderlist;
    }

    @override
    public string tostring() {
        return "user{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderlist=" + orderlist +
                '}';
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public date getbirthday() {
        return birthday;
    }

    public void setbirthday(date birthday) {
        this.birthday = birthday;
    }
}

MyBatis多表操作查询功能

修改user实体

MyBatis多表操作查询功能

package com.zg.domain;

import java.util.date;
import java.util.list;

public class user {
    private int id;
    private string username;
    private string password;
    private date birthday;

    //描述当前用户存在哪些订单
    private list<order>  orderlist;

    public list<order> getorderlist() {
        return orderlist;
    }

    public void setorderlist(list<order> orderlist) {
        this.orderlist = orderlist;
    }

    @override
    public string tostring() {
        return "user{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderlist=" + orderlist +
                '}';
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }

    public string getpassword() {
        return password;
    }

    public void setpassword(string password) {
        this.password = password;
    }

    public date getbirthday() {
        return birthday;
    }

    public void setbirthday(date birthday) {
        this.birthday = birthday;
    }
}

创建usermapper接口

package com.zg.mapper;

import com.zg.domain.user;

import java.util.list;

public interface usermapper {
    public list<user> findall();

}

配置usermapper.xml

<?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.zg.mapper.usermapper" >

    <resultmap id="usermap" type="user">
        <id column="uid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>
        <!--配置集合信息-->
        <!--property:集合名称 oftype:当前集合中的数据类型-->
        <collection property="orderlist" oftype="order">
            <!--封装order的数据-->
            <id column="oid" property="id"></id>
            <result column="ordertime" property="ordertime"></result>
            <result column="total" property="total"></result>
        </collection>
    </resultmap>

    <select id="findall" resultmap="usermap">
        select *,o.id oid from user u,orders o where u.id=o.uid
    </select>
</mapper>


测试

 @test//测试一对多
    public void test2() throws ioexception {
        inputstream resourceasstream = resources.getresourceasstream("sqlmapconfig.xml");
        sqlsessionfactory sqlsessionfactory = new sqlsessionfactorybuilder().build(resourceasstream);
        sqlsession sqlsession = sqlsessionfactory.opensession();

        usermapper mapper = sqlsession.getmapper(usermapper.class);
        list<user> userlist = mapper.findall();
        for (user user : userlist) {
            system.out.println(user);
        }



        sqlsession.close();
    }

MyBatis多表操作查询功能

多对多查询

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询该用户的所有角色

MyBatis多表操作查询功能

select * from user u,sys_user_role ur ,sys_role r where u.id=ur.userid and ur.roleid=r.id

MyBatis多表操作查询功能

创建role实体,修改user实体

MyBatis多表操作查询功能

添加usermapper接口

package com.zg.mapper;

import com.zg.domain.user;

import java.util.list;

public interface usermapper {
    public list<user> findall();

    public list<user> finduserandroleall();

}

配置usermapper.xml

<resultmap id="userrolemap" type="user">
        <!--user信息的封装-->
        <id column="userid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="birthday" property="birthday"></result>

        <!--user内部的rolelist信息-->
        <collection property="rolelist" oftype="role">
            <id column="roleid" property="id"></id>
            <result column="rolename" property="rolename"></result>
            <result column="roledesc" property="roledesc"></result>
        </collection>

    </resultmap>
    <select id="finduserandroleall" resultmap="userrolemap">
        select * from user u,sys_user_role ur ,sys_role r where u.id=ur.userid and ur.roleid=r.id
    </select>

测试代码

 @test//测试多对多
    public void test3() throws ioexception {
        inputstream resourceasstream = resources.getresourceasstream("sqlmapconfig.xml");
        sqlsessionfactory sqlsessionfactory = new sqlsessionfactorybuilder().build(resourceasstream);
        sqlsession sqlsession = sqlsessionfactory.opensession();

        usermapper mapper = sqlsession.getmapper(usermapper.class);
        list<user> userandroleall = mapper.finduserandroleall();
        for (user user : userandroleall) {
            system.out.println(user);
        }


        sqlsession.close();
    }

MyBatis多表操作查询功能

到此这篇关于mybatis多表操作的文章就介绍到这了,更多相关mybatis多表操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: MyBatis 多表