首页 > 其他分享 >MyBatis复杂映射开发之一对一查询

MyBatis复杂映射开发之一对一查询

时间:2022-08-30 11:44:59浏览次数:70  
标签:return 映射 17 一对一 public user 2022 MyBatis id

一对一查询需求

用户表和订单表的关系为:一个用户可以有多个订单,一个订单只能从属于一个用户

image-20220317170627431

一对一查询需求:查询一个订单,同时查询出该订单所对应的用户

对应的sql语句:select * from orders o,user u where o.uid=u.id

查询结果如下:

id ordertime total uid id username password birthday
1 2022-03-17 17:15:33 3000 1 1 lucy 123 2022-03-17 17:15:56
2 2022-03-17 17:15:33 4000 1 1 lucy 123 2022-03-17 17:15:56
3 2022-03-17 17:15:33 5000 2 2 tom 123 2022-03-17 17:15:56

需求分析

image-20220317184728374 image-20220317185134760

具体实现

创建User和Order实体

  • User

    /**
     * 用户信息
     *
     * @name: User
     * @author: terwer
     * @date: 2022-05-08 17:41
     */
    class User {
        var id: Int? = null
        var username: String? = null
        override fun toString(): String {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    '}'
        }
    }
    
    /**
     * 用户信息
     *
     * @name: User
     * @author: terwer
     * @date: 2022-03-17 17:41
     **/
    public class User {
        private Integer id;
        private String username;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    '}';
        }
    }
    
  • Order

    /**
     * 订单
     *
     * @name: Order
     * @author: terwer
     * @date: 2022-05-08 17:42
     */
    class Order {
        var id: Int? = null
        var orderTime: String? = null
        var total: Double? = null
    
        // 代表当前订单属于哪一个用户
        var user: User? = null
        override fun toString(): String {
            return "Order{" +
                    "id=" + id +
                    ", orderTime='" + orderTime + '\'' +
                    ", total=" + total +
                    ", user=" + user +
                    '}'
        }
    }
    
    /**
     * 订单
     *
     * @name: Order
     * @author: terwer
     * @date: 2022-03-17 17:42
     **/
    public class Order {
        private Integer id;
        private String orderTime;
        private Double total;
    
        // 代表当前订单属于哪一个用户
        private User user;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getOrderTime() {
            return orderTime;
        }
    
        public void setOrderTime(String orderTime) {
            this.orderTime = orderTime;
        }
    
        public Double getTotal() {
            return total;
        }
    
        public void setTotal(Double total) {
            this.total = total;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        @Override
        public String toString() {
            return "Order{" +
                    "id=" + id +
                    ", orderTime='" + orderTime + '\'' +
                    ", total=" + total +
                    ", user=" + user +
                    '}';
        }
    }
    

创建OrderMapper接口

/**
 * 订单映射
 *
 * @name: IUserMapper
 * @author: terwer
 * @date: 2022-05-08 17:54
 */
interface OrderMapper {
    /**
     * 查询订单同时查询订单所属用户
     * @return
     */
    fun findOrderAndUser(): List<Order?>?
}
/**
 * 订单映射
 *
 * @name: IUserMapper
 * @author: terwer
 * @date: 2022-03-17 17:54
 **/
public interface OrderMapper {
    /**
     * 查询订单同时查询订单所属用户
     * @return
     */
    public List<Order> findOrderAndUser();
}

配置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.terwergreen.mapper.OrderMapper">
    <resultMap id="orderMap" type="com.terwergreen.pojo.Order">
        <result property="id" column="id"></result>
        <result property="orderTime" column="ordertime"></result>
        <result property="total" column="total"></result>

        <association property="user" javaType="com.terwergreen.pojo.User">
            <result property="id" column="uid"></result>
            <result property="username" column="username"></result>
        </association>
    </resultMap>

    <!-- resultMap:手动配置实体属性与表字段的映射关系 -->
    <select id="findOrderAndUser" resultMap="orderMap">
        select * from orders o,user u where o.uid=u.id
    </select>
</mapper>

另外一种配置方法

 <mapper namespace="com.terwergreen.mapper.OrderMapper">
    <resultMap id="orderMap" type="com.terwergreen.pojo.Order">
       <result property="id" column="id"></result>
        <result property="orderTime" column="ordertime"></result>
        <result property="total" column="total"></result>
    
        <result property="user.id" column="uid"></result>
        <result property="user.username" column="username"></result>
    </resultMap>
  
    <!-- resultMap:手动配置实体属性与表字段的映射关系 -->
    <select id="findOrderAndUser" resultMap="orderMap">
        select * from orders o,user u where o.uid=u.id
    </select>
</mapper>

测试结果

@Test
@Throws(IOException::class)
fun test1() {
    val resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml")
    val sqlSessionFactory = SqlSessionFactoryBuilder().build(resourceAsStream)
    val sqlSession = sqlSessionFactory.openSession()
    val orderMapper = sqlSession.getMapper(OrderMapper::class.java)
    val orderList = orderMapper.findOrderAndUser()
    for (order in orderList) {
        println(order)
    }
}
@Test
public void test1() throws IOException {
    InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

    SqlSession sqlSession = sqlSessionFactory.openSession();
    OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

    List<Order> orderList = orderMapper.findOrderAndUser();
    for (Order order : orderList) {
        System.out.println(order);
    }
}
image-20220317184422603

文章更新历史

2022/05/08 feat:新增Kotlin支持。

标签:return,映射,17,一对一,public,user,2022,MyBatis,id
From: https://www.cnblogs.com/tangyouwei/p/mybatis-complex-mapping-development-one-to-one-query.

相关文章

  • mybatis批量插入时报错:syntax error, expect ‘)‘
    问题:mybatis批量插入时报错:syntaxerror,expect‘)’ 解决:是因为传入的参数list为null,在代码中加上list.size()>0的判断。mapper文件:<insertid="batchInsertSys......
  • MyBatis的基本使用
    快速入门Mybatis的官网:https://mybatis.org/mybatis-3/MyBatis的开发步骤添加MyBatis的坐标<properties><project.build.sourceEncoding>UTF-8</project.build.so......
  • MyBatis的基本介绍及优势
    MyBatis的历史由来Mybatis原本是Apache软件基金会的一个开源项目叫做iBatis,2010年这个项目由Apache迁移到了googlecode管理才改名为Mybatis,2013年又迁移到......
  • Spring Boot集成Mybatis
    集成Mybatis1、导入依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><......
  • Mybatis-Plus使用@TableField实现自动填充日期
    一、前言我们在日常开发中经常使用ORM框架,比如Mybatis、tk.Mybatis、Mybatis-Plus。不过最广泛的还是Mybatis-Plus,我们的一些表,都会有创建时间、更新时间、创建人、更新人......
  • mybatis-plus专题学习
    1、优化规范【推荐】实体类需要实现Serializable接口一旦实现了Seriazlizable接口,就意味着该对象可以通过序列化/反序列化的方式在网络上传输通讯,该接口只是一个标志接口......
  • SpringBoot整合mybatis
    9、SpringBoot整合mybatisMyBatis-Spring-Boot-Starter官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/maven仓库:https://mvnrepos......
  • mybatis中的动态sql
    动态sql是:根据不同的查询条件,生成不同的查询结果常用的动态语句包括:-if-choose(when,otherwise)-trim(where,set)-foreachif语句<!--需求1:根据作者......
  • 8/28 深入理解计算机系统笔记 内存映射
    9.8内存映射定义:将一个虚拟内存区域和一个磁盘上的对象关联起来,以初始化这个虚拟内存区域的内容的过程被称为内存映射。虚拟内存区域可以映射到下面两种类型的对象中的......
  • Mybatis中多对一与一对多
    多对一的处理在pojo中就有StudentprivateStringname;privateStringid;privateTeacherteacher;比如说多个学生对应着一个老师这样我们就要使用多表查询了方......