Mybatis笔记(3)

Mybatis笔记(3)

一、多表查询

1.1 一对一查询

订单用户(一个订单属于一个)

Order实体类有user属性

配置resultMap(OrderMap)

<select id="findAll" resultMap="orderMap">
         SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
</select>
<resultMap id="orderMap" type="order">
        <!--手动指定字段与实体属性的映射关系
            column: 数据表的字段名称
            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>-->

        <!--
            property: 当前实体(order)中的属性名称(private User user)
            javaType: 当前实体(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>

查询结果表:

<span>Mybatis笔记(3)</span>

1.2 一对多查询

订单用户(一个用户下达多个订单)

User实体类有List

属性

配置resultMap(UserMap)

<select id="findAll" resultMap="userMap">
        SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
</select>
<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>

1.3 多对多查询

比一对多查询多一张表

<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>
<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>

小结

MyBatis多表配置方式:

一对一配置:使用

做配置

一对多配置:使用

+
做配置

多对多配置:使用

+
做配置

二、MyBatis注解开发

2.1 常用注解

注解 目标 对应的XML标签
@CacheNamespace
@CacheNamespaceRef
@Results 方法
@Result 方法

@One 方法
@Many 方法
@Insert@Update@Delete 方法



@InsertProvider@UpdateProvider@DeleteProvider@SelectProvider 方法





@Param 参数 N/A
@Options 方法 映射语句的属性
@select 方法

2.1 简单查询

@Insert 简单插入

@Insert(" insert into user(name,sex,age) values(#{name},#{sex},#{age} " )

int saveUser(User user);

@Update 简单更新

@Update("update user set name= #{name} ,sex = #{sex},age =#{age} where id = #{id}")

void updateUserById(User user);

@Delete 简单删除

@Delete("delete from  user  where id =#{id} ")

void deleteById(Integer id);

@Select 简单查询

一对一第一种方法)

@Select(" Select * from user ")
@Results({
    //id = true代表主键
    @Result(id = true, column = "id", property = "id"),
    @Result(column = "name", property = "name"),
    @Result(column = "tel", property = "tel"),
    @Result(column = "birth", property = "birth"),
    @Result(column = "address", property = "address")
})
List<User> queryAllUser()

@One

一对一第二种方法)

@Select(" select * from orders ")
    @Results({
        @Result(column = "id",property = "id"),
        @Result(column = "ordertime",property = "ordertime",
                @Result(
                    property = "user", //要封装的属性名
                    column="uid",// 根据哪个字段去查询user表数据
                    javaType = User.class,//要封装的实体类型
                    //select属性 代表查询哪个接口的方法获得数据
                    //one指示我们,查询出来的结果只有一个。
                    one = @One(select="gyb.UserMapper.findById")
                 )                    
                      
    })
    public List<Order> findAll();

@Many

一对多

User内含有orderList

	@Select(" select * from dept")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
        	@Result(
                //封装user内的userlist属性
                property = "orderList",
                //数据库内字段
                column = "id",
                //结果类型
             	javaType = "List.class",
                //使用方法,代表查询多个结果
                many = @Many(select = "gyb.OrderMapper.findById")
                
            )
            
    })
    public List<User> findUserAndOrderAll();
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115195.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 查看Tomcat进程_windows查看tomcat进程

    查看Tomcat进程_windows查看tomcat进程jps|grepBootstrappsaux|grepcatalina.startup.bootstrappsaux|greptomcatpsaux|grep8080按照端口号查看lsof-i:8080 

    2022年9月20日
    0
  • 最强PostMan使用教程(1)

    最强PostMan使用教程(1)

    2021年10月12日
    53
  • 快速幂的大数运算_快速幂模

    快速幂的大数运算_快速幂模快速幂运算1.什么是快速幂2.快速幂的“小数”运算3.高精度(大数)的快速幂1.什么是快速幂快速幂,是指在进行幂运算的时候,用一种快速方法得出答案。比如,要求2^100的值,那按照最简单的方式,就是一个一个2去相乘,然后最终得到答案,那么这样就要计算100次,非常浪费时间,那么快速幂就是使用一种技巧使得将其计算次数减少,快速得到答案。2.快速幂的“小数”运算对于系统内置类型的整型,暂且叫他“小数”,这个时候进行快速幂运算,代码如下:#include<cstdio>#include&l

    2022年10月27日
    0
  • red flag系统_red fly

    red flag系统_red fly接受协议条款之后开始设置分区。分区的目的是在硬盘上为系统分配一个或几个确定的位置,Linux系统支持多分区结构,每一部分可以存放在不同的磁盘或分区上。一般情况下,安装RedFlagLinuxDesktop5.0需要一个根文件系统分区(类型为ext3、ext2或reiserfs)和一个交换分区(类型为swap),这种分区方案适用于大多数用户。如果系统的用户数目较多,可以专门为

    2022年8月20日
    7
  • C++中string append函数的使用与字符串拼接「建议收藏」

    C++中string append函数的使用与字符串拼接「建议收藏」常用的函数原型:basic_string&amp;amp;amp;append(constbasic_string&amp;amp;amp;str);basic_string&amp;amp;amp;append(constchar*str);basic_string&amp;amp;amp;append(constbasic_string&amp;amp;amp;str,size_typeindex,size

    2022年6月24日
    25
  • Linux 系统 top 命令详解

    Linux 系统 top 命令详解文章目录前言top命令关键词详解1.VIRT:virtualmemoryusage虚拟内存2.RES:residentmemoryusage常驻内存3.SHR:sharedmemory共享内存4.DATA:数据占用的内存5.top运行中的交互命令top命令图解前言top命令是Linux下常用的性能分析工具,能够实时显示系统状况,比如cpu、内存的使用等。以下详细介绍top命令。top命令关键词详解1.VIRT:virtualmemoryusa

    2022年9月2日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号