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);
 
}
希望对刚学习mybatis的道友有帮助
欢迎各位吐槽

 

posted @ 2018-06-08 15:19  胖头陀春天  阅读(206)  评论(0编辑  收藏  举报