Mysql WorkBench 创建表进行一对一,一对多,多对多查询

以下内容均是用户(user)对品牌(brand)角色(role)进行分析

1.一对一 (品牌对用户)

1.1 在数据库建表user,brand。

其中对于brand表要留一个列(uid) 当做user表的外键,外键的作用不懂可以去查一下,总的来说就是两个表之间联系的纽扣

1.2 手动指定字段与实体属性的映射关系

先写好sql语句查询无误之后再接着写代码(我的如下)关键就是让主键等于外键

SELECT * FROM mybatis.brand b,mybatis.user u where b.uid=u.id;

--------!!!!!!!!!------

需要在brand实体类中声明

xml中的代码如下: 

 <resultMap id="brandMap" type="brand">
        <!--
        column:数据表的列名
        property:类的属性名称 private int id
        -->
        <id column="id" property="id"/> <!--主键所特有的表达方式-->
        <result column="brandName" property="brandName"/>
        <result column="brandCompany" property="brandCompany"/>
        <result column="brandMoney" property="brandMoney"/>
        <!--        <result column="uid" property="user.id"/>-->
        <!--        <result column="userName" property="user.userName"/>-->
        <!--        <result column="userAddress" property="user.userAddress"/>-->
        <!--        <result column="userNumber" property="user.userNumber"/>-->

        <!--
        第二种写法 更简洁 association 意为连接,联系 根据 property="user"联系起来 这个user是brand类里的属性
        property:当前实体(brand)的属性名称 private user user
        javaType:当前实体(brand)中的属性类型 也就是要关联的那个表的实体类
        -->
        <association property="user" javaType="user">
            <id column="uid" property="id"/>
            <result column="userName" property="userName"/>
            <result column="userAddress" property="userAddress"/>
            <result column="userNumber" property="userNumber"/>
        </association>
    </resultMap>

<select id="selectAll" resultMap="brandMap">
    SELECT * FROM mybatis.brand b,mybatis.user u where b.uid=u.id;
</select>

这是当时做的笔记

目的就是在<resultMap>标签里声明映射关系 因为前面你只是说两个表有关系 但是没有声明

<id>标签是主键特有的 在对应的column中写上你要查询的那个表的列名

其他普通的属性是<result>标签

2.一对多(user对brand)

 2.1 还是跟上面一样不用该表只是sql语句换了一下位置

//在这里最应该注意sql语句改了一个别名 b.id bid 因为在一对多中id会冲突不改的话查不出所有值
SELECT *,b.id bid FROM mybatis.user u,mybatis.brand b  where b.uid=u.id;

在xml中的代码:

<resultMap id="userMap" type="user">
        <id column="uid" property="id"/>
        <result column="userName" property="userName"/>
        <result column="userNumber" property="userNumber"/>
        <result column="userAddress" property="userAddress"/>

        <!--
        property:实体类(user)属性名称 private List<brand> brands
        brand:实体类(user)的属性类型
        订单只能查询到一条结果??
        用户和订单的id值如果名字一样的话,会发生冲突!这样查询结果中只会有一个订单,查不到所有订单!
        解决办法 : 在SQL文中给其中一个id起别名,然后在column属性中输入别名!!
        -->
        <collection property="brands" ofType="brand">
            <!--封装brand对象-->
            <id column="bid" property="id"/>
            <result column="brandMoney" property="brandMoney"/>
            <result column="brandCompany" property="brandCompany"/>
            <result column="brandName" property="brandName"/>
        </collection>
    </resultMap>

    <select id="findAll" resultMap="userMap">
          SELECT *,b.id bid FROM mybatis.user u,mybatis.brand b  where b.uid=u.id;
    </select>

 在这里也会有问题 

就是没有订单的用户结果为null 因为我这里用了内连接 你可以试试左,外连接 这我也不太懂 小白一个

等我懂了再来更新

3.多对多(user对role)

一个人可以有很多身份 而且身份可以重复(这就是与一对多的区别!!)

3.1 数据库结构  列名不要重复 把uuid和rid设为user和role的外键

sql语句

    SELECT *FROM mybatis.user u,mybatis.role r,mybatis.user_role ur where u.id=ur.uuid and r.id=ur.rid;

xml代码

 <!--多对多-->
    <resultMap id="roleMap" type="user">
    <!--先封装user-->
        <id column="uuid" property="id"/>
        <result column="userName" property="userName"/>
        <result column="userNumber" property="userNumber"/>
        <result column="userAddress" property="userAddress"/>
        <!--再封装role 参数是roleList 类型是role-->
        <collection property="roleList" ofType="role">
            <id column="rid" property="id"/>
            <result column="roleName" property="roleName"/>
            <result column="roleLocation" property="roleLocation"/>
            <result column="roleSalary" property="roleSalary"/>
        </collection>
    </resultMap>

    <select id="selectAllByRole" resultMap="roleMap">
    SELECT *FROM mybatis.user u,mybatis.role r,mybatis.user_role ur where u.id=ur.uuid and r.id=ur.rid;
    </select>

基本都是重复操作 !!!关键就是一对多时多的那一方要起一个别名!!

写实体类-->对实体类加入要关联的实体类参数(private user users )-->写sql-->写xml配置文件-->单元测试


版权声明:本文为Javalearnibg原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。