假如,咱们的实体类,或者数据库中的表,字段或者参数过多,咱们该当应用 Map!
User addUser(Map<String,Object> map);
<!-- 对象的属性,能够间接取出来
传递 map 的 key
-->
<insert id="addUser" parameterType="map">
insert into mybatis.user(id,name,pwd) values (#{userId},#{userName},#{password});
</insert>
测试
@Test
public void addUser(){SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
Map<String,Object> map = new HashMap<String,Object>();
map.put("userId",5);
map.put("password","123789");
mapper.addUser(map);
sqlSession.close();}
map 传递参数,间接在 sql 中取出 map 中的 key 即可!
对象传递参数,间接在 sql 中取对象的属性即可!
只有一个根本类型参数的状况下,能够间接在 sql 中取到!
List<User> getUserLike(String value);
<select id="getUserLike" resultType="com.jialidun.pojo.User">
select * from mybatis.user where name like "%" #{value} "%"
/select>
@Test
public void getUserLike(){SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserLike("孙");
for(User user:userList){System.out.println(user);
}
sqlSession.close();}