MyBatis__配置多对一映射返回
Posted Kikyo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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__配置多对一映射返回的主要内容,如果未能解决你的问题,请参考以下文章