#1#-> 适用于单个参数:

接口文件

public void deleteData(int id) throws Exception;

mapper.xml文件:#{}占位符对应传入的参数,可以随意写字符串标识符,但最好还是和传入参数对应起来,保持良好的可读性

<delete id="deleteData" parameterType="int">
     delete from Products where ProductID = #{id}
</delete>

#2#-> 使用注解传递参数

接口文件:使用param注解参数

public ArrayList<DataBean> selectAll(@Param("DateString") String DateString, @Param("BranchNo") String BranchNo) throws Exception;

mapper.xml

<select id="selectAll" resultMap="DataMap">
    select * from townbranch where datetime = #{DateString} and  branchno like #{BranchNo};
</select>

这种方式只适合少数几个参数的情况,如果参数过多建议用Javabean

#3#-> 使用JavaBean传递参数

接口文件

 public int insertData(ProductsBean product) throws Exception;

ProductsBean文件:

package com.mybatisdemo.beans;

public class ProductsBean {

    private int ProductID;
    private String ProductName;
    private double Price;
    private String ProductDescription;

    public ProductsBean() {
        super();
    }

    public ProductsBean(int productID, String productName, double price, String productDescription) {
        ProductID = productID;
        ProductName = productName;
        Price = price;
        ProductDescription = productDescription;
    }

    public ProductsBean(String productName, double price, String productDescription) {
        ProductName = productName;
        Price = price;
        ProductDescription = productDescription;
    }

    public int getProductID() {
        return ProductID;
    }

    public void setProductID(int productID) {
        ProductID = productID;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public double getPrice() {
        return Price;
    }

    public void setPrice(double price) {
        Price = price;
    }

    public String getProductDescription() {
        return ProductDescription;
    }

    public void setProductDescription(String productDescription) {
        ProductDescription = productDescription;
    }

    @Override
    public String toString() {
        return "ProductsBean{" +
                "ProductID=" + ProductID +
                ", ProductName='" + ProductName + '\'' +
                ", Price=" + Price +
                ", ProductDescription='" + ProductDescription + '\'' +
                '}';
    }
}

 mapper.xml文件:

<insert id="insertData" keyProperty="ProductID">
    insert into Products (ProductID, ProductName, Price, ProductDescription)
    values (#{ProductID},#{ProductName},#{Price},#{ProductDescription})
</insert>

 【小结】

1. 如果只有单个参数,使用#{}占位符即可。

2. 如果多个参数可以使用Param注解传递参数。

3. 如果参数太多,可以使用JavaBean,#{key}的key对应Javabean的字段。

-------------------------------------------------------------------------------------------------------------------

#-> 占位符 # 和 $ 的区别

#{}${} 都可以替代参数,即占位符

#{} 在SQL语句运行的时候显示的是一个一个的 ? ? ?

例如针对根据某个ID的一条删除语句,ID号用 #{id} 替代

<delete id="deleteData" parameterType="int">
   delete from Products where ProductID = #{id}
</delete>

输出的SQL日志如下 :

Preparing: delete from Products where ProductID = ? 
==> Parameters: 2(Integer)
<==    Updates: 1
[Products] 已经删除第2条记录!

#{ } 在处理接受的字段会当做字符串处理,会把传入的参数自动加上引号"",例如这里如果想要删掉 ID=2 的记录

当执行函数 deleteData(2)的时候,输出的SQL语句实际上是:

delete from Products where ProductID = "2"

${ } 不会自动添加引号"" ,就是简单的字符串拼接,容易引发SQL语句注入的安全隐患,所以一般还是推荐使用 #{ }.

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐