MyBaits传递多个参数
2016-04-17 16:18
477 查看
1.传递一个参数的情况比较容易
比如在mapping.xml中有:
(1)、使用map来传递多个参数:
mapping.xml中有:
(3)、通过封装为bean来调(万能的,但是对于较少的参数个数比较麻烦)
mapping.xml中:
比如在mapping.xml中有:
<select id="Find" resultType="ray.bean.User" parameterType="string"> select * from user where USERNAME=#{username} </select>那么dao层就需要这么调用:(主要是名称和参数类型要正确)
public User Find(String username);2. 传递多个参数的情况:
(1)、使用map来传递多个参数:
mapping.xml中有:
<!-- 使用map传递多个参数 --> <select id="loginUser" resultType="ray.bean.User"> select * from user where loginName=#{loginName, jdbcType=VARCHAR} and password=#{password, jdbcType=VARCHAR} and isAdmin = 1; </select>那么dao中需要这么写:(弊端是在dao接口层不知道参数具体表示什么意思)
public User loginUser(Map<String, String> paramas);在service层需要这么调:
public User loginUser(String loginName, String uPwd) { Map<String, String> param = new HashMap<String, String>(); param.put("loginName", loginName); param.put("password", uPwd); return userDao.loginUser(param); }(2)、通过顺序来调用
<!-- 使用map传递多个参数 --> <select id="loginUser" resultType="ray.bean.User"> select * from user where loginName=#{0} and password=#{1} and isAdmin = 1; </select>dao层需要这么写:
public User <span style="font-family: Arial, Helvetica, sans-serif;">loginUser </span><span style="font-family: Arial, Helvetica, sans-serif;">(String userName, String passWord);</span>
(3)、通过封装为bean来调(万能的,但是对于较少的参数个数比较麻烦)
<update id="Update" parameterType="ray.bean.User"> update user set PASSWORD=#{password} where userId=#{userId}; </update>dao层需要这么写:
public int Update(User user);(4)、使用注解@param表示参数,是对第一种方法的改进
mapping.xml中:
<!-- 使用@param传递多个参数 --> <select id="loginUser" resultType="ray.bean.User"> select * from user where loginName=#{loginName, jdbcType=VARCHAR} and password=#{password, jdbcType=VARCHAR} and isAdmin = 1; </select>在dao层这么写:
public User loginUser(@Param("userName")String userName, @Param("passWord")String passWord);service层也不需要封装map
相关文章推荐
- 【杭电oj】1789 - Doing Homework again(贪心,并查集)
- 【原创】区块链技术主流开源项目 - Major Open Source Projects of Blockchain Technologies
- XML Parser Errors See Details for more Information XML Parser Error on line 1: Document root ele
- “娇娇”这把火, 烧得整个机器人圈脸疼
- main()函数的参数
- USACO-Section 4.2 Drainage Ditches (最大流[Ford-Fulkerson])
- No user specified nor available for SSH client
- idea报Error Compilation failed
- CONTAINER类之心得体会
- *[Lintcode]Trailing Zeros
- LightOJ 1236 Pairs Forming LCM【整数分解】
- LightOJ 1236 Pairs Forming LCM【整数分解】
- Jersey(1.19.1) - Client API, Uniform Interface Constraint
- 401 Unauthorized: ERROR Failed to connect to newly launched supervisor. Agent will exit
- POJ3250 Bad Hair Day(单调栈)
- 对话吴恩达(Andrew Ng):超级大咖深度解析人工智能 以及如何成为已经数据挖掘工程师
- wait()、notify()、await()、signal()的使用
- 【LeetCode】70. Climbing Stairs
- [LeetCode]70. Climbing Stairs
- 11. Container With Most Water