【MyBatis__配置】多对一映射返回

spring整合mybatis

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

AppointmentDao.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.soecode.lyf.dao.AppointmentDao">
    <insert id="insertAppointment">
        <!-- ignore 主键冲突,报错 -->
        INSERT ignore INTO appointment (book_id, student_id)
        VALUES (#{bookId}, #{studentId})
    </insert>
    
    <select id="queryByKeyWithBook" resultType="Appointment">
        <!-- 如何告诉MyBatis把结果映射到Appointment同时映射book属性 -->
        <!-- 可以自由控制SQL -->
        SELECT
            a.book_id,
            a.student_id,
            a.appoint_time,
            b.book_id "book.book_id",
            b.`name` "book.name",
            b.number "book.number"
        FROM
            appointment a
        INNER JOIN book b ON a.book_id = b.book_id
        WHERE
            a.book_id = #{bookId}
        AND a.student_id = #{studentId}
    </select>
</mapper>

相关推荐