3. mybatis # 与 $ 的区别
mybatis # 与 $ 的区别
1.# % 号必须写在test中
应用场景:模糊查询
配置文档mapper.xml
<select id="selectBlogByTitle" parameterType="string" resultMap="blogResultMap">
select * from blog where title like #{tilte}
</select>
接口 mapper
List<Blog> selectBlogByTitle(String title);
测试Test
@Test
public void testSelectBlogByTitle(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.selectBlogByTitle("%w%");
session.close();
System.out.println("<<<<<------------>>>>>:"+blogList);
}
2.$ 如果是单值的话只能用value ,并且用单引号,可以写在xml中
配置文档mapper.xml
<select id="selectBlogByTitle2" parameterType="string" resultMap="blogResultMap">
select * from blog where title like '${value}'
</select>
%号还可以写在xml中
<select id="selectBlogByTitle2" parameterType="string" resultMap="blogResultMap">
select * from blog where title like '%${value}%'
</select>
接口 mapper
List<Blog> selectBlogByTitle2(String title);
测试Test
@Test
public void testSelectBlogByTitle2(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.selectBlogByTitle2("%w%");
session.close();
System.out.println("<<<<<------------>>>>>:"+blogList);
}
不写% 号在测试中
@Test
public void testSelectBlogByTitle2(){
SqlSession session = MybatisUtil.getSqlSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Blog> blogList = blogMapper.selectBlogByTitle2("w");
session.close();
System.out.println("<<<<<------------>>>>>:"+blogList);
}
欢迎各位吐槽
致敬:2020年的自己
--------------------------------------------
即使不为了什么远大理想,为了好好生活,你也得努力奋斗啊,不然别说什么风花雪月了,柴米油盐也能让你一筹莫展。