我使用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();
}
结果:
当我把xml里编写sql语句的传参方法#{}改为${},其余都不变运行就有结果
#{}传递int类型写成的sql语句查询有返回结果,为什么我用#{}传递String类型的参数就不行呢?