欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis多表操作查詢功能

 更新時(shí)間:2021年11月13日 11:03:23   作者:zgDaren  
這篇文章主要介紹了MyBatis多表操作,包括一對(duì)一查詢,一對(duì)多查詢的模型,多對(duì)多查詢的需求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

一對(duì)一查詢

用戶表和訂單表的關(guān)系為,一個(gè)用戶多個(gè)訂單,一個(gè)訂單只從屬一個(gè)用戶
一對(duì)一查詢的需求:查詢一個(gè)訂單,與此同時(shí)查詢出該訂單所屬的用戶

在這里插入圖片描述

在只查詢order表的時(shí)候,也要查詢user表,所以需要將所有數(shù)據(jù)全部查出進(jìn)行封裝SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id

在這里插入圖片描述

創(chuàng)建Order和User實(shí)體

order

public class Order {
    private int id;
    private Date ordertime;
    private double total;
    //表示當(dāng)前訂單屬于哪一個(gè)用戶
    private User user;

user

public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;

創(chuàng)建OrderMapper接口

public interface UserMapper {
//查詢?nèi)康姆椒?
    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">
        <!--手動(dòng)指定字段與實(shí)體的映射關(guān)系-->
        <!--comlumn:數(shù)據(jù)表的字段名稱 property:實(shí)體的屬性名稱-->
        <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”,因?yàn)閛rder中沒(méi)有user中的字段,只有一個(gè)user對(duì)象-->
    <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>
    <!--通過(guò)properties標(biāo)簽加載外部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>

    <!--配置當(dāng)前數(shù)據(jù)源的環(huán)境-->
    <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>

在這里插入圖片描述

在一對(duì)一配置的時(shí)候,在order實(shí)體中創(chuàng)建了一個(gè)user,所以property屬性都使用user.** 的方式進(jìn)行編寫(xiě),但是這里還可以使用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">
        <!--手動(dòng)指定字段與實(shí)體的映射關(guān)系-->
        <!--comlumn:數(shù)據(jù)表的字段名稱 property:實(shí)體的屬性名稱-->
        <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內(nèi)的還可以使用association進(jìn)行配置-->
        <!--association匹配的意思,在order中有個(gè)屬性叫user-->
        <!-- property="user"當(dāng)前order實(shí)體中的屬性名稱,javaType="user"當(dāng)前實(shí)體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”,因?yàn)閛rder中沒(méi)有user中的字段,只有一個(gè)user對(duì)象-->
    <select id="findAll" resultMap="orderMap">
        SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
    </select>

</mapper>


一對(duì)多查詢的模型

用戶表和訂單表的關(guān)系為,一個(gè)用戶有多個(gè)訂單,一個(gè)當(dāng)?shù)粡膶僖粋€(gè)用戶
一對(duì)多查詢需求:查詢一個(gè)用戶,與此同時(shí)查詢出該用戶具有的訂單

在這里插入圖片描述

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;

    //描述當(dāng)前用戶存在哪些訂單
    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;
    }
}

在這里插入圖片描述

修改User實(shí)體

在這里插入圖片描述

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;

    //描述當(dāng)前用戶存在哪些訂單
    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;
    }
}

創(chuàng)建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:當(dāng)前集合中的數(shù)據(jù)類型-->
        <collection property="orderList" ofType="order">
            <!--封裝order的數(shù)據(jù)-->
            <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>


測(cè)試

 @Test//測(cè)試一對(duì)多
    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();
    }

在這里插入圖片描述

多對(duì)多查詢

用戶表和角色表的關(guān)系為,一個(gè)用戶有多個(gè)角色,一個(gè)角色被多個(gè)用戶使用
多對(duì)多查詢的需求:查詢用戶同時(shí)查詢?cè)撚脩舻乃薪巧?/p>

在這里插入圖片描述

select * from user u,sys_user_role ur ,sys_role r where u.id=ur.userId and ur.roleId=r.id

在這里插入圖片描述

創(chuàng)建Role實(shí)體,修改User實(shí)體

在這里插入圖片描述

添加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內(nèi)部的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>

測(cè)試代碼

 @Test//測(cè)試多對(duì)多
    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();
    }

在這里插入圖片描述

到此這篇關(guān)于MyBatis多表操作的文章就介紹到這了,更多相關(guān)MyBatis多表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java單點(diǎn)登錄(SSO)的實(shí)現(xiàn)

    java單點(diǎn)登錄(SSO)的實(shí)現(xiàn)

    SSO是指在多個(gè)應(yīng)用系統(tǒng)中個(gè),用戶只需要登陸一次就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng),本文主要介紹了java單點(diǎn)登錄的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-07-07
  • Java實(shí)現(xiàn)雙保險(xiǎn)線程的示例代碼

    Java實(shí)現(xiàn)雙保險(xiǎn)線程的示例代碼

    這篇文章主要介紹了Java實(shí)現(xiàn)雙保險(xiǎn)線程的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring Boot數(shù)據(jù)庫(kù)鏈接池配置方法

    Spring Boot數(shù)據(jù)庫(kù)鏈接池配置方法

    這篇文章主要介紹了Spring Boot數(shù)據(jù)庫(kù)鏈接池配置方法,需要的朋友可以參考下
    2017-04-04
  • java自旋鎖和JVM對(duì)鎖的優(yōu)化詳解

    java自旋鎖和JVM對(duì)鎖的優(yōu)化詳解

    這篇文章主要為大家介紹了java自旋鎖和JVM對(duì)鎖的優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 解決Map集合使用get方法返回null拋出空指針異常問(wèn)題

    解決Map集合使用get方法返回null拋出空指針異常問(wèn)題

    這篇文章主要介紹了解決Map集合使用get方法返回null拋出空指針異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 淺談Java設(shè)計(jì)模式系列-裝飾器模式

    淺談Java設(shè)計(jì)模式系列-裝飾器模式

    這篇文章主要介紹了Java設(shè)計(jì)模式系列-裝飾器模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 已解決:No ''Access-Control-Allow-Origin''跨域問(wèn)題

    已解決:No ''Access-Control-Allow-Origin''跨域問(wèn)題

    這篇文章主要介紹了已解決:No 'Access-Control-Allow-Origin' 跨域,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringMVC和rabbitmq集成的使用案例

    SpringMVC和rabbitmq集成的使用案例

    這篇文章主要介紹了SpringMVC和rabbitmq集成的使用案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Java多線程的其他知識(shí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java多線程的其他知識(shí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了Java多線程的其他知識(shí),需要的朋友可以參考下
    2017-05-05
  • Java?synchronized底層實(shí)現(xiàn)原理以及鎖優(yōu)化

    Java?synchronized底層實(shí)現(xiàn)原理以及鎖優(yōu)化

    Synchronized是Java中解決并發(fā)問(wèn)題的一種最常用的方法,也是最簡(jiǎn)單的一種方法,下面這篇文章主要給大家介紹了關(guān)于Java?synchronized底層實(shí)現(xiàn)原理以及鎖優(yōu)化的相關(guān)資料,需要的朋友可以參考下
    2022-02-02

最新評(píng)論