If we have too many entity classes or tables, fields, or parameters in our database, we should use a Map!
User addUser(Map<String,Object> map);
<! -- the properties of the object, > <insert id="addUser" parameterType="map"> insert into mybatis. User (id,name, PWD) values (#{userId},#{userName},#{password}); </insert>
test
@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 pass parameters, directly in the SQL take out the key in the map can! Object to pass parameters, directly in the SQL to get the properties of the object! If there is only one basic type parameter, it can be fetched directly in 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();
}