万能的Map以及模糊查询
1.0 UserMapper文件
User getUserById2(Map<String, Object> map);
1.1 UserMapper.xml 文件
<select id="getUserById2" parameterType="map" resultType="com.chris.pojo.User">
select * from chrisdb.zd_user where ID = #{ids}
</select>
1.2 junit测试单元文件
@Test
public void test05() {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("ids", 1);
User userById2 = mapper.getUserById2(map);
System.out.println(userById2);
} catch (Exception exception) {
System.out.println(exception.getMessage());
} finally {
sqlSession.close();
}
}
1.3使用Map好处
- 可以不用生产实体类(例如User类)
- UserMapper.Xml当中 的 #{} 变量名可以不与数据库的一致
- 实际开发环境中常用