目录
参数就是通过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 |