Mybatis的动态SQL语句(insert,update,select语句)

摘要:这篇文章,我将讲讲mybatis中关于插入(insert)、更新(update)、查询(select)经常使用的动态SQL语句。

1. 准备实体类BookEntity(我这里是直接建立实体类,但是如果实体类比较麻烦的话,可以直接使用map)

package com.fs.mybatis.entity;

public class BookEntity {
    private String bid;
    private String bname;
    private double price;
    private String author;
    private String cid;

    public BookEntity() {
    }

    public BookEntity(String bid, String bname, double price, String author, String cid) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
        this.author = author;
        this.cid = cid;
    }

    public String getBid() {
        return bid;
    }

    public void setBid(String bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    @Override
    public String toString() {
        return "BookEntity{" +
                "bid='" + bid + '\'' +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                ", cid='" + cid + '\'' +
                '}';
    }
}

2. 准备Mapper接口

package com.fs.mybatis.mapper;

import com.fs.mybatis.entity.BookEntity;
import org.apache.ibatis.annotations.Mapper;

import javax.swing.text.html.parser.Entity;
import java.util.List;
import java.util.Map;

@Mapper
public interface BookMapper {
    // 根据条件进行查询
    public List<BookEntity> queryBook(BookEntity bookEntity);


    public boolean updateBook(BookEntity bookEntity);

    public boolean insertBook(BookEntity bookEntity);

}

3. 查询(select) 语句(动态Sql语句)---- xml文件

<!--    查询书籍  -->
    <select id="queryBook" resultType="com.fs.mybatis.entity.BookEntity">
        select * from t_book
        <where>
            <if test="bid != null and bid != ''">
                bid = #{bid}
            </if>
            <if test="bname != null and bname != ''">
                and bname like '%${bname}%'
            </if>
            <if test="price != null and price != ''">
                and price = #{price}
            </if>
            <if test="author != null and author != ''">
                and author like '%${author}%'
            </if>
            <if test="cid != null and cid != ''">
                and cid = #{cid}
            </if>
        </where>
    </select>

4. 插入(insert)语句(动态Sql语句) ---- xml文件

<!--    添加书籍  -->
    <insert id="insertBook" >
        insert into t_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bid != null and bid != ''">
                bid,
            </if>
            <if test="bname != null and bname != ''">
                bname,
            </if>
            <if test="price != null and price != ''">
                price,
            </if>
            <if test="author != null and author != ''">
                author,
            </if>
            <if test="cid != null and cid != ''">
                cid
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bid != null and bid != ''">
                #{bid},
            </if>
            <if test="bname != null and bname != ''">
                #{bname},
            </if>
            <if test="price != null and price != ''">
                #{price},
            </if>
            <if test="author != null and author != ''">
                #{author},
            </if>
            <if test="cid != null and cid != ''">
                #{cid}
            </if>
        </trim>
    </insert>

5. 修改 (update)语句(动态Sql语句) ---- xml文件

<!--    修改书籍  -->
    <update id="updateBook" parameterType="com.fs.mybatis.entity.BookEntity">
        update t_book
        <set>
            <if test="bname != null and bname != ''">
                bname = #{bname},
            </if>
            <if test="price != null and price != ''">
                price = #{price},
            </if>
            <if test="author != null and author != ''">
                author = #{author},
            </if>
            <if test="cid != null and cid != ''">
                cid = #{cid}
            </if>
        </set>
        where bid = #{bid}
    </update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值