看mybatis的视频时,偶然发现有人分享万能map,觉得挺有趣,就记录一下吧
首先在usermapper中写
User getUserById2(Map<String,Object> map);
在usermapper.xml中配置
<select id="getUserById2" parameterType="map" resultType="pojo.User">
select * from mybatis.user where id = #{id} and username=#{username}
</select>
测试类中这样写
public void getUserById2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id",1);
mapper.getUserById2(map);
sqlSession.close();
}
即可实现