BookMapper.xml 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.sf.mapper.BookMapper">
  6. <!-- 添加图书-->
  7. <insert id="addBook" parameterType="book">
  8. insert into book (book_id , book_name , price) values (#{bookId},#{bookName},#{price})
  9. </insert>
  10. <!-- 更新图书-->
  11. <update id="updateBook" parameterType="book">
  12. update book set book_name = #{bookName} , price = #{price} where book_id = #{bookId}
  13. </update>
  14. <!-- 删除图书-->
  15. <delete id="deleteBook" parameterType="integer">
  16. delete from book where book_id = #{bookId}
  17. </delete>
  18. <!-- 查询图书列表-->
  19. <select id="getBookList" resultType="book">
  20. select * from book
  21. </select>
  22. <!--查询图书详情根据图书的id-->
  23. <select id="getBookInfo" resultType="book">
  24. select * from book where book_id = #{bookId}
  25. </select>
  26. <select id="getBookListWithIdorNameOrPrice" resultType="book">
  27. select * from book where price = #{price} and book_name=#{bookName}
  28. </select>
  29. <!-- <if test="bookId != null">
  30. and book_id = #{bookId}
  31. </if>
  32. <if>标签:作为条件判断
  33. test="bookId != null" : 是判断入参中的bookId 是否为空
  34. 如果不等于空 就拼接查询条件
  35. 等于空,就不拼接查询条件
  36. <where>标签去除and和or 帮助我们解决多条件查询sql条件拼接问题
  37. -->
  38. <select id="getBookListWithIdorNameOrPrice2" parameterType="book" resultType="book">
  39. select * from book <where>
  40. <if test="bookId != null">
  41. and book_id = #{bookId}
  42. </if>
  43. <if test="bookName != null">
  44. and book_name=#{bookName}
  45. </if>
  46. <if test="price != null">
  47. and price = #{price}
  48. </if>
  49. </where>
  50. </select>
  51. <update id="updateBook2" parameterType="book">
  52. update book <set>
  53. <if test="bookName != null">book_name = #{bookName} , </if>
  54. <if test="price != null">price = #{price} </if>
  55. </set>
  56. where book_id = #{bookId}
  57. </update>
  58. <select id="getBookListWithIdorNameOrPrice3" parameterType="book" resultType="book">
  59. select * from book where
  60. <choose>
  61. <when test="bookName != null">book_name = #{bookName}</when>
  62. <when test="price != null">price = #{price}</when>
  63. <otherwise>
  64. book_id = #{bookId}
  65. </otherwise>
  66. </choose>
  67. </select>
  68. </mapper>