MyBatis参数详解

目录

1.parameterType

2.dao接口方法参数一个简单类型的参数

3.dao接口方法中有多个简单类型的参数

4.dao接口方法使用一个对象作为参数

5.dao接口中多个简单类型的参数,使用位置

6.dao接口参数是一个Map


参数就是通过java程序把数据传入到mapper文件中的sql语句。参数主要是指dao接口方法的形参

1.parameterType

parameterType:表示参数的类型,指定dao方法的形参数据类型。这个形参的数据类型是给mybatis使用。mybatis是给sql语句的参数赋值时使用。

PreparedStatement.setXXX(位置, 值)

要想理解这里必须对JDBC中的PreparedStatement有足够的了解,推荐看我之前写的一篇文章

一文学会JDBC(两万字,适用于新手)_ypxcan的博客-CSDN博客

我们来看mapper文件中select查询语句

    <select id="selectStudentById" resultType="com.lu.entity.Student">
        select id, name, email, age from student where id = #{studentId}
    </select>

mybatis要执行的sql语句为:

select id, name, email, age from student where id = ?

?是占位符,使用JDBC中的PreparedStatement执行这样的sql语句

PreparedStatement ps = conn.prepareStatement("select id, name, email, age from student where id = ?");

我们给 ? 位置进行赋值

  • 如果参数是Integer,则执行ps.setInt(1, 1005)
  • 如果参数是String,则执行ps.setString(1, "1005")

那么parameterType的第一个用法:

java类型的全限定类型名称        parameterType = "java.lang.Integer"

那么代码就是这样写:

    <select id="selectStudentById" parameterType="java.lang.Integer" resultType="com.lu.entity.Student">
        select id, name, email, age from student where id = #{studentId}
    </select>

第二种用法,使用别名

别名 映射的类型
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
object Object
map Map
hashmap HashMap
list List
arraylist ArrayList
collection Collection
iterator Iterator

既然我们这里是Integer类型,那么别名就是int&

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再让我学一会吧!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值