Bootstrap

Map和模糊查询

MSP

1.使用Map作为参数

package com.yhzz.dao;

import org.apache.ibatis.annotations.Param;

import java.util.Map;

public interface userDao{
    
    int updateUserMap(Map<String,object> map);

}

2.SQL语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper 
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yhzz.dao.UserDao">
    <update id="updateUserMap" parameterType="map">
        update user
        set name = #{name},pwd=#{pwd}
        where id=#{id};
    </update>
</mapper>

3.Msp赋值

public void updateUserMap(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao userDao = sqlSession.getMapper(UserDao.class);
    Map<String,Object> map = new HashMap<>();
    map.put("id","4");
    map.put("name","haha");
    map.put("pwd","222");
    int i = userDao.updateUserMap(map);
    //提交事务
    sqlSession.commit();
    sqlSession.close();
}

使用Msp传参更灵活。

模糊查询

select * from user where name like concat("%",#{name},"%");
package com.yhzz.dao;

import com.yhzz.pojo.User;
import org.apache.ibatis.annotations.Param;

public interface UserDao {

    //模糊查询
    List<User> getUserListLikeName(String name);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yhzz.dao.UserDao">

    <select id="getUserListLikeName" resultType="com.yhzz.pojo.User">
        select * from user where name like concat("%",#{name},"%");
    </select>
</mapper>
@Test
public void getUserListLikeName(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao UserDao = sqlSession.getMapper(UserDao.class);
    List<User> userList = userDao.getUserListLikeName("李");
    for (User user : userList) {
        System.out.println(user.gets());
    }
    //提交事务
    sqlSession.commit();
    sqlSession.close();
}

;