码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-17 00:54:34      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:bat   set   pac   settings   label   配置   dtd   tty   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>

 

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

标签:bat   set   pac   settings   label   配置   dtd   tty   mybatis   

原文地址:https://www.cnblogs.com/kikyoqiang/p/12716806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!