mybati之parameterType传递多个参数mybati之parameterType传递多个参数
当在查询的时候需要传⼊多个参数的时候该怎么办呢:
1,封装成⼀个Model对象,底层HashMap还是⼀个
User user=new User();
user.setUserName("zhangsan");
user.setPassword("123456");
queryUserByuserNameAndPass(user);//为返回值类型为User
<select id="queryUserByuserNameAndPass"  resultType="User" parameterType="com.j1.User"  >
select * from user u where u.user_name=#{userName} and u.pass_word=#{passWord}
</select>
2,使⽤HashMap封装
/
/使⽤HashMap定义:
HashMap <String,Object> map=new HashMap<String,Object>();
map.put("userName","zhangsan");
map.put("password","123456");
queryUserByuserNameAndPass(map);//为返回值类型为User
//sql 如下:
<select id="queryUserByuserNameAndPass"  resultType="User"  parameterType="Map" >
select * from user u where u.user_name=#{userName} and u.pass_word=#{passWord}
</select>
3,使⽤注解,@param
queryUserByuserNameAndPass(@param(''userName")String userName,@param("password")String password)
<select id="queryUserByuserNameAndPass"  resultType="User">
select * from user u where u.user_name=#{userName} and u.pass_word=#{passWord}
</select>
queryUserByuserNameAndPass(String userName,String PassWord);  //返回值类型为Usre
//sql
<select id="queryUserByuserNameAndPass"  resultType="User">
select * from user u where u.user_name=#{userName} and u.pass_word=#{passWord}//会报错的</select>
<select id="queryUserByuserNameAndPass"  resultType="User">
select * from user u where u.user_name=#{0} and u.pass_word=#{1}//运⾏成功但是,可读性不⾼</select>
//parameterType 参数类型可以是基本数据类型(int,String,long),可以是Model对象,也可以是Map;
1,基本数据类型:
@Override
public void deleteUserById(Long id) {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
parameter数据类型sqlSession.delete("batis.user.deleteUserById", id);
}
//sql
<delete id="deleteUserById" parameterType="java.lang.Long">
DELETE FROM tb_user WHERE id = #{id}
</delete>
2,Model对象
/
/java代码:
@Override
public void saveUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
sqlSession.insert("batis.user.saveUser", user);
}
@Override
public void updateUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
sqlSession.update("batis.user.updateUser", user);
}
/
/sql
<update id="updateUser" parameterType="batis.pojo.User">
UPDATE tb_user
SET
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday},
updated = NOW()
WHERE
id = #{id};
</update>
</update>
3.map
//java
HashMap <String,Object> map=new HashMap<String,Object>();
map.put("userName","zhangsan");
map.put("password","123456");
queryUserByuserNameAndPass(map);//为返回值类型为User
//sql 如下:
<select id="queryUserByuserNameAndPass"  resultType="User">
select * from user u where u.user_name=#{userName} and u.pass_word=#{passWord}// </select>
转载:wwwblogs/wangchuanfu/p/5877169.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。