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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • md文件编辑_第三方编辑器

    md文件编辑_第三方编辑器学长让我写博客学习c语言,写了几篇,但是总觉得文章界面不好看,然后找到一些资料,那就开始新的界面吧!开始你的MD编辑器吧!!(妈的编辑器)复制,直接应用>块引用@[TOC]#二.使用步骤##1.引入库##2.读入数据点一下蓝色的字,到相应的文章段<fontcolor=red>关注</font>这是设置字体颜色,和范围,“`cimportnumpyasnpimportp…

    2022年9月23日
    3
  • navicat premium 15 macos 激活码【2022最新】2022.02.18

    (navicat premium 15 macos 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlFZP9ED60OK-eyJsa…

    2022年4月1日
    242
  • js有序数组合并_js array map

    js有序数组合并_js array mapjavascript实现有序map

    2022年9月23日
    2
  • Android ConstraintLayout布局详解[通俗易懂]

    Android ConstraintLayout布局详解[通俗易懂]前言之前在使用AndroidStudio新建项目的时候,发现MainActivity的默认布局从RelativeLayout变成了ConstraintLayout。当时就对这个ConstraintLayout很好奇,就研究了一下。发觉确实很强大,在此做个总结。ConstraintLayout定义什么是ConstraintLayout呢?ConstraintLayout是Google在2016年的

    2022年5月5日
    87
  • 记录mybatis分页查询失败[通俗易懂]

    记录mybatis分页查询失败[通俗易懂]在进行mybatis的分页查询时出现ExceptionInIntializerError错误代码信息如下在检查mapper和插件配置后,试着换了一下mybatis的版本后原版本替换后版本成功的进行了分页查询

    2022年6月2日
    49
  • isnotempty和isnotnull_was not iterable

    isnotempty和isnotnull_was not iterable先看看isEmpty和isBlank  从效果来看,当a=””,字符中有空格时,IsEmpty是算他不为空的本质上讲:isEmpty等价于str==null||str.length==0isBlank等价于str==null||str.length==0||str.trim().length==0我们再来看非空:is…

    2022年10月7日
    1

发表回复

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

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