<parameterType >
参数类型,用于参数传递的查询
mapper.xml:
<!--parameterType 参数类型,用于参数传递的查询-->
完整代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace: 命名空间, 可以随意定义, 一般情况下要写全限定路径
MyBatis管理SQL语句是通过namespace+id来定位的
-->
<mapper namespace="com.lin.mapper.UserMapper">
<!--
select标签用于编写查询语句
id: sql语句的唯一标识, 类比为方法名
resultType: 用于设定返回结果的类型(全限定路径)
如果返回结果是集合, 要写集合泛型的类型
-->
<!-- parameterType, 参数类型, 用于参数的传递 -->
<select id="selById" resultType="user" parameterType="int">
<!--
#{}用于使用参数
可以用 index, 索引, 从0开始
也可以用 param+数字, param1, param2
select * from tb_user where id=#{param1}
-->
select * from tb_user where id = #{0}
</select>
<!--parameterType 参数类型,用于参数传递的查询-->
<select id="selUseObject" resultType="User" parameterType="User">
<!--如果参数是对象可以使用#{属性名来获取}-->
select * from tb_user where username = #{username} and password = #{password}
</select>
<select id="selUseMap" resultType="User" parameterType="map">
<!--如果是Map集合,通过#{key}来获取-->
select * from tb_user where username = #{username} and password = #{password}
</select>
</mapper>
可以从上面看出,参数的传递可以是int类型,也可以是对象,或者是Map集合
为什么会有这样的情况出现呢,主要还是受限于session.selectOne只能传递两个参数
参数1:mapper的命名空间.id
参数2:传递进来的查询条件
参数的获取用#{ }来获取
(ps 第一种int类型的查询语句#{ }内写什么都行,但是为了规范,一般写0)
查询操作代码
package