Mybatis联表查询起别名注意点和疑惑

本文探讨了在 MyBatis 中进行联表查询时遇到的问题,当不使用别名导致查询结果为 null 的原因。作者提出,联表查询时可能需要为字段指定别名以确保正确映射。通过示例 XML 映射文件展示了如何配置 resultMap 进行复杂对象的映射。文章旨在帮助开发者理解 MyBatis 的查询机制和结果映射配置。
摘要由CSDN通过智能技术生成
<?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.chen.dao.StudentMapper">

    <select id="getListStudent" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from student s,teacher t
        where s.tid = tid
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"></result>
        <result property="name" column="sname"></result>
        <association property="teacher" javaType="Teacher">
            <result property="id" column="tid"></result>
            <result property="name" column="tname"></result>
        </association>
    </resultMap>


<!--    <select id="getListStudent" resultMap="studentTeacher">-->
<!--        select * from student;-->
<!--    </select>-->

<!--    <resultMap id="studentTeacher" type="Student">-->
<!--        <result property="id" column="id"></result>-->
<!--        <result property="name" column="name"></result>-->

<!--        <association property="teacher" column="tid" javaType="Teacher" select="getListTeacher"></association>-->

<!--    </resultMap>-->

<!--    <select id="getListTeacher" resultType="Teacher">-->
<!--        select * from teacher;-->
<!--    </select>-->

</mapper>

如果不起别名查询结果就是null(就是sid换成s.id),我个人感觉是就像在sqlyog写语句一样,联表查询需要起一个别名来作为新查出的表的列名。
不知道我的感觉是否正确,欢迎大佬解惑,谢谢。

Spring Boot是一款基于Spring Framework的轻量级开发框架,Mybatis是一个优秀的持久层框架。在Spring Boot中使用Mybatis进行联表查询,可以通过多种方式实现,其中比较常用的方式是使用Mybatis的XML配置文件进行SQL编写,再通过Spring Boot对Mybatis进行集成。 以下是使用Spring Boot和Mybatis实现联表查询的步骤: 1. 在pom.xml文件中添加依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> ``` 2. 在application.properties文件中配置数据库连接信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建实体类,并在其中定义联表查询所需的属性和方法。 4. 在Mybatis的XML配置文件中编写SQL语句,进行联表查询操作。 例如,假设我们需要对两张表(user和order)进行联表查询操作,查询用户的所有订单信息。则可以按照以下步骤进行操作: 1. 创建User类和Order类,并在User类中添加一个List<Order>类型的属性orders。 2. 编写UserMapper接口,并在其中定义联表查询方法。 ``` public interface UserMapper { User findUserWithOrders(int userId); } ``` 3. 在UserMapper.xml配置文件中编写SQL语句,进行联表查询操作。 ``` <select id="findUserWithOrders" parameterType="int" resultMap="userMap"> select * from user u left join order o on u.id = o.user_id where u.id = #{userId} </select> <resultMap id="userMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> <collection property="orders" ofType="com.example.demo.entity.Order"> <id column="order_id" property="id" /> <result column="order_name" property="name" /> <result column="price" property="price" /> </collection> </resultMap> ``` 以上代码的含义是:通过left join将user表和order表进行联表查询查询条件为用户id。在查询结果中,将user表中的id、name、age三个字段映射到User类中的id、name、age属性中;同时将order表中的order_id、order_name、price三个字段映射到Order类中的id、name、price属性中;最后将Order对象添加到User对象的orders属性中。 4. 在UserService类中调用UserMapper接口中定义的联表查询方法,即可完成对两张表进行联表查询的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值