Mybatis使用resultMap实现一对一查询

Posted 小玄影123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis使用resultMap实现一对一查询相关的知识,希望对你有一定的参考价值。

数据库设计,部分源文件请参照resulttype那篇随笔,不再次累赘。

创建Orders.java把User.java作为属性加入

import java.util.Date;

import cn.zqq.bean.User;

public class Orders{
    
    private String number;
    private Date craetetime;
    private String note;
    private User user;
    
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public Date getCraetetime() {
        return craetetime;
    }
    public void setCraetetime(Date craetetime) {
        this.craetetime = craetetime;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    @Override
    public String toString() {
        return "Orders [number=" + number + ", craetetime=" + craetetime + ", note=" + note + ", user=" + user + "]";
    }

}

mapper.java文件如下


import java.util.List;

public interface UserCustomOrderMap {

    public List<Orders> findAllInfoUserOrdersMap()throws Exception;
}

 

 

 对应的UserCustomOrderMa.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="cn.zqq.one2one.resultmap.UserCustomOrderMap">
    
    <resultMap type="cn.zqq.one2one.resultmap.Orders" id="ordersMap">
        <result column="number" property="number"/>
        <result column="createtime" property="craetetime"/>
        <result column="note" property="note"/>
        <!-- 复杂类型的属性,引入相关类型并配置 -->
        <association javaType="cn.zqq.bean.User" property="user">
            <id column="id" property="id"/>
            <result column="username" property="username"/>
            <result column="birthday" property="birthday"/>
            <result column="sex" property="sex"/>
            <result column="address" property="address"/>
            
        </association>
    </resultMap>
    <select id="findAllInfoUserOrdersMap" resultMap="ordersMap">
        SELECT * FROM USER,orders WHERE user.id=orders.user_id;
    </select>    
</mapper>

 

 测试代码


    SqlSession sqlSession = null;

    @Before
    public void beforesourse() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
    }

 

    

@Test
public void findAllInfoUserOrders() throws Exception { UserCustomOrderMap order = sqlSession.getMapper(UserCustomOrderMap.class); List<Orders> userOrders = order.findAllInfoUserOrdersMap(); for (Orders userCustom : userOrders) { System.out.println(userCustom); } }

 

 对于SqlMapConfig.xml添加对应的包的扫描器即可

 

以上是关于Mybatis使用resultMap实现一对一查询的主要内容,如果未能解决你的问题,请参考以下文章

mybatis学习笔记(10)-一对一查询

mybatis学习 十四 resultMap标签 一对一(联合查询)

Mybatis多表查询之一对一查询的多种实现-XML配置

Mybatis中如何实现一对一,一对多的关联查询?

五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)

MyBatis4——一对一对多关联查询