一共介绍三种方式
第一种方式
用#{0}表示第一个参数、#{1}表示第二参数…以此类推
service
1 2 3
| public User findByMutiParam(String name, String password) { return userDao.findByMutiParam(name, password); }
|
dao
1
| User findByMutiParam(String name, String password);
|
xml
1 2 3 4 5 6 7 8
| <select id="findByMutiParam" resultType="com.mybatis.entity.User"> SELECT * FROM user WHERE name = #{0} and password = #{1} </select>
|
第二种方式
参数封装map集合
service
1 2 3 4 5 6
| public User findByMap(String name, String password) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", name); map.put("password", password); return userDao.findByMap(map); }
|
dao
1
| User findByMap(Map<String, Object> map);
|
xml
1 2 3 4 5 6 7 8
| <select id="findByMap" resultType="com.mybatis.entity.User"> SELECT * FROM user WHERE name = #{name} and password = #{password} </select>
|
第三种方式
使用注解(推荐)参数清晰明了
service
1 2 3
| public User findByAnnotation(String name, String password) { return userDao.findByAnnotation(name, password); }
|
dao
1
| User findByAnnotation(@Param("name") String name, @Param("password") String password);
|
xml
1 2 3 4 5 6 7 8
| <select id="findByAnnotation" resultType="com.mybatis.entity.User"> SELECT * FROM user WHERE name = #{name} and password = #{password} </select>
|