2.1 定义dao

提前定义好我们的dao,这里参数用 map传递

  1. public interface UserDao {
  2. User findUser(Map<String, Object> map);
  3. }

2.2 编写mapper文件

当我们使用map时这里的参数就可以自己指定

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--绑定对应的dao接口-->
  6. <mapper namespace="com.lu.dao.UserDao">
  7. <select id="findUser" parameterType="map" resultType="com.lu.entity.User">
  8. select *
  9. from user
  10. where id = #{userID};
  11. </select>
  12. </mapper>

2.3 测试

单元测试,这里使用也是传递一个map

  1. @Test
  2. public void mapQuery() {
  3. Map<String, Object> param = new HashMap<>();
  4. param.put("userID", 3);
  5. User user = mapper.findUser(param);
  6. System.out.println(user);
  7. }