T1Longlive 2022-09-03 16:57 采纳率: 85.7%
浏览 26
已结题

mybatis的#{}传递字符类型参数没有结果,${}有结果,为什么?

我使用idea编写javaweb时在mybatis用#{}传String类型的参数返回结果为空,但我用${}传参返回就有结果.

sql语句编写部分:

    <select id="selectById" resultMap="brandResultMap">
        select *
        from stu
        where name
        like #{name};
    </select>

test代码部分:

    @Test
    public void testSelectById() throws IOException {
        //接收参数
        String name = "马运";
        name="'"+name+"'";
        //int id = 3;
        //int math = 222;
        //int english = 77;
        Map map = new HashMap();
        map.put("name" , name);
        //map.put("english" , english);
        //1. 获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2. 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4. 执行方法
        List<Brand> brand = brandMapper.selectById(map);
        //Brand brand = brandMapper.selectById(id);
        System.out.println(brand);
        //5. 释放资源
        sqlSession.close();
    }

结果:

img

当我把xml里编写sql语句的传参方法#{}改为${},其余都不变运行就有结果

img

img

#{}传递int类型写成的sql语句查询有返回结果,为什么我用#{}传递String类型的参数就不行呢?

  • 写回答

1条回答 默认 最新

  • 关注

    没有%

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 9月3日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog