【Mybatis】mapper.xml文件中SQL语句传入多个参数方法

#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语句注入的安全隐患,所以一般还是推荐使用 #{ }.

  • 20
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis ,可以在 mapper.xml 文件使用 `<select>`, `<insert>`, `<update>`, `<delete>` 标签来定义 SQL 语句。这些标签的 SQL 语句可以使用参数参数的传递方式有多种,以下是其几种常用的方式: 1. 使用 `#{}` 占位符来传递参数。例如: ```xml <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{userId} </select> ``` 在这个例子,`#{userId}` 表示一个占位符,MyBatis 会在执行 SQL 语句时将其替换为实际的参数值。 2. 使用 `${}` 占位符来传递参数。例如: ```xml <select id="getUserByName" resultType="User"> SELECT * FROM user WHERE name = '${userName}' </select> ``` 在这个例子,`${userName}` 表示一个占位符,MyBatis 会在执行 SQL 语句时将其替换为实际的参数值。需要注意的是,使用 `${}` 传递参数存在 SQL 注入的风险,因为实际的参数值直接拼接到 SQL 语句,建议尽量避免使用。 3. 使用 `@Param` 注解来传递参数。例如: ```java public interface UserMapper { List<User> getUserByNameAndAge(@Param("name") String name, @Param("age") int age); } ``` ```xml <select id="getUserByNameAndAge" resultType="User"> SELECT * FROM user WHERE name = #{name} AND age = #{age} </select> ``` 在这个例子,`@Param` 注解用于指定参数的名称,MyBatis 会根据参数名称来匹配 SQL 语句的占位符。 这些方式都可以用于传递参数,具体选择哪种方式取决于具体的情况。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值