public interface AppointDao {
//新增预约
void insertAppoint(@Param("bookID") long bookID, @Param("studentID") long studentID);
//通过主键查询预约记录,返回时并携带图书实体
Appoint queryByKeyWithBook(@Param("bookID") long bookID, @Param("studentID") long studentID);
}
对应的mapper.xml
<insert id="insertAppoint" parameterType="long">
INSERT ignore INTO appointment (book_id, student_id)
VALUES (#{bookID}, #{studentID})
</insert>
<select id="queryByKeyWithBook" parameterType="long" resultMap="appointMap">
select a.*,b.*
from book b inner join appointment a
on a.book_id = b.book_id
where a.book_id = #{bookID} and a.student_id=#{studentID}
</select>
就像这种情况一样,,如果有多个参数,mybatis可能会识别不了,有两种解决方案
第一种就像上图中的这样在dao层参数前加上@Param
第二种就是在mapper.xml文件中修改sql语句对应的参数
<insert id="insertAppoint" parameterType="long">
INSERT ignore INTO appointment (book_id, student_id)
VALUES (#{0}, #{1})
</insert>
<select id="queryByKeyWithBook" parameterType="long" resultMap="appointMap">
select a.*,b.*
from book b inner join appointment a
on a.book_id = b.book_id
where a.book_id = #{0} and a.student_id=#{1}
</select>
如果第一个参数使用#{0},第二个参数使用#{1},一次代替即可