Map in mybatis

Time:2021-8-16

Assuming that there are too many entity classes or tables, fields or parameters in the database, we should use map!

User addUser(Map<String,Object> map);
<!-- The properties of the object can be retrieved directly
Pass key of map
-->
<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 passes parameters. You can directly get the key in the map from SQL!
Object transfer parameters, you can directly get the attribute of the object in SQL!
If there is only one basic type parameter, you can get it 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 ("sun");
   
   for(User user:userList){
   
     System.out.println(user);
   }
   
   sqlSession.close();

}

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]