CRUD思路:使用map传多个参数、模糊查询

使用map传参

map传递参数,直接在sql语句中取出key

对象传递参数,直接在sql语句中取出属性

只有一个基本数据类型参数时,可以直接在sql中取到

public interface UserMapper {

    User getLoginUser(Map<String,Object> map);
}
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace绑定对应的接口(dao/mapper)-->
<mapper namespace="dao.UserMapper">

    <select id="getLoginUser" parameterType="map" resultType="entity.User">
        select * from mybatis.user  where name = #{name} and pwd = #{pwd}
    </select>

</mapper>
public class UserDaoTest {
    @Test
    public void getLoginUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {

            UserMapper userDao = sqlSession.getMapper(UserMapper.class);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name","xxx");
            map.put("pwd","111");
            User loginUser = userDao.getLoginUser(map);
            System.out.println(loginUser);

        }finally {
            sqlSession.close();
        }
    }

模糊查询(注意要防止sql注入)

在sql语句中拼接 %

select * from user where name like "%"#{value}"%"

在测试类中拼接 %

posted @ 2022-02-11 20:21  ︶ㄣ演戲ㄣ  阅读(4)  评论(0编辑  收藏  举报  来源