1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?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.sf.mapper.BookMapper">
- <!-- 添加图书-->
- <insert id="addBook" parameterType="book">
- insert into book (book_id , book_name , price) values (#{bookId},#{bookName},#{price})
- </insert>
- <!-- 更新图书-->
- <update id="updateBook" parameterType="book">
- update book set book_name = #{bookName} , price = #{price} where book_id = #{bookId}
- </update>
- <!-- 删除图书-->
- <delete id="deleteBook" parameterType="integer">
- delete from book where book_id = #{bookId}
- </delete>
- <!-- 查询图书列表-->
- <select id="getBookList" resultType="book">
- select * from book
- </select>
- <!--查询图书详情根据图书的id-->
- <select id="getBookInfo" resultType="book">
- select * from book where book_id = #{bookId}
- </select>
- <select id="getBookListWithIdorNameOrPrice" resultType="book">
- select * from book where price = #{price} and book_name=#{bookName}
- </select>
- <!-- <if test="bookId != null">
- and book_id = #{bookId}
- </if>
- <if>标签:作为条件判断
- test="bookId != null" : 是判断入参中的bookId 是否为空
- 如果不等于空 就拼接查询条件
- 等于空,就不拼接查询条件
- <where>标签去除and和or 帮助我们解决多条件查询sql条件拼接问题
- -->
- <select id="getBookListWithIdorNameOrPrice2" parameterType="book" resultType="book">
- select * from book <where>
- <if test="bookId != null">
- and book_id = #{bookId}
- </if>
- <if test="bookName != null">
- and book_name=#{bookName}
- </if>
- <if test="price != null">
- and price = #{price}
- </if>
- </where>
- </select>
- <update id="updateBook2" parameterType="book">
- update book <set>
- <if test="bookName != null">book_name = #{bookName} , </if>
- <if test="price != null">price = #{price} </if>
- </set>
- where book_id = #{bookId}
- </update>
- <select id="getBookListWithIdorNameOrPrice3" parameterType="book" resultType="book">
- select * from book where
- <choose>
- <when test="bookName != null">book_name = #{bookName}</when>
- <when test="price != null">price = #{price}</when>
- <otherwise>
- book_id = #{bookId}
- </otherwise>
- </choose>
- </select>
- </mapper>
|