mybatis中传入基本类型参数,传入多个基本数据类型

mybatis3.2.6+spring4.0.2 关于传入基本参数类型 以及多个基本类型参数的问题

最近再练习框架,找到了一点心得分享出来,不对之处欢迎提出共勉

1 参数类型string 一个参数

方法:

User getUserByName(String username);

mapper写法:

<select id="getUserByName" parameterType="String" resultMap="user">
select * from d_user where user_name =#{1234567890}(或者#{_parameter})
</select>


2 参数类型string 两个参数

方法:

User getUserByNameAndPwd(String username, String password);

mapper写法:

<select id="getUserByNameAndPwd" parameterType="String" resultMap="user">
select * from d_user where user_name =#{param1} and user_password=#{param2}
</select>


或者方法:

User getUserByNameAndPwd(@param("username")String username, @param("password")String password);

mapper:

<select id="getUserByNameAndPwd" parameterType="String" resultMap="user">
select * from d_user where user_name =#{username} and user_password=#{password}
</select>


总结:

针对一个基本类型参数,mybatis可以准确的获取这个参数的数值,不论你在mapper中用#{XXX}怎么获取都可以,

针对多个基本参数类型,mybatis需要给他提供一个标识,你可以在方法上添加@param注解 ,当然是,mybatis的包

org.apache.ibatis.annotations.Param , 

你也可以按照参数顺序,写param1,param2之类的,推荐第一种 ,比较严谨一点


其实我们可以把参数封装进user对象中,传入实体即可,不过为了测试mybatis对于基本类型参数,所以拆开来测试下。

猜你喜欢

转载自blog.csdn.net/u013408059/article/details/79064530