假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当使用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();

}

RYGAR
1 声望0 粉丝

引用和评论

0 条评论