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的道友有帮助

欢迎各位吐槽

3. mybatis # 与 $ 的区别的更多相关文章

  1. mybatis 参数为list时,校验list是否为空, mybatis ${}与#{}的区别,Mybatis sql in

    1.mybatis 参数为list时,校验list是否为空 2. mybatis ${}与#{}的区别 简单来说#{} 解析的是占位符?可以防止SQL注入, 比如打印出来的语句 select * fr ...

  2. mybatis 参数为list时,校验list是否为空, mybatis ${}与#{}的区别

    一.参数list时,先判断是否为空,否则会报错. 二.mybatis ${}与#{}的区别 简单来说#{} 解析的是占位符?可以防止SQL注入, 比如打印出来的语句 select * from tab ...

  3. MySQL 和 Oracle 在 MyBatis 使用中的区别

    MySQL 和 Oracle 在 MyBatis 使用中的区别: 区别 MySQL Oracle 存储过程的参数模式 mode 为 IN 时,是否需要指定 jdbcType 不需要:MyBatis 为 ...

  4. Mybatis与Hibernate区别

    Mybatis与Hibernate区别 mybatis: 1. 入门简单,即学即用,提供了数据库查询的自动对象绑定功能,而且延续了很好的SQL使用经验,对于没有那么高的对象模型要求的项目来说,相当完美 ...

  5. Hibernate与Mybatis的概念区别

    首先简单介绍下两者的概念: Hibernate :Hibernate 是当前最流行的ORM框架,对数据库结构提供了较为完整的封装. Mybatis:Mybatis同样也是非常流行的ORM框架,主要着力 ...

  6. MyBatis知多少(26)MyBatis和Hibernate区别

    iBatis和Hibernate之间有着较大的差异,但两者解决方案很好,因为他们有特定的领域.我个人建议使用MyBatis的,如果: 你想创建自己的SQL,并愿意维持他们. 你的环境是由关系数据模型驱 ...

  7. mybatis 于 hibernate区别

    两者区别是还是非常大的,结合至今为止的经验,总结出以下几点: 1. hibernate是全自动,而mybatis是半自动. hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的Ja ...

  8. Mybatis中#和$区别(带脑图)

    零.引言 使用 #{name} 的时候,MyBatis会进行预编译,防止SQL注入的问题(官方话) 用一个通俗一点的例子来解释,比如有如下MyBatis的SQL语句 21.#{}和${}的区别.png ...

  9. mybatis之 # 与 $ 区别以及 sql 预编译

    mybatis 中使用 sqlMap 进行 sql 查询时,经常需要动态传递参数,例如我们需要根据用户的姓名来筛选用户时,sql 如下: select * from user where name = ...

随机推荐

  1. MySQL组合索引最左匹配原则

    几个重要的概念 1.对于mysql来说,一条sql中,一个表无论其蕴含的索引有多少,但是有且只用一条. 2.对于多列索引来说(a,b,c)其相当于3个索引(a),(a,b),(a,b,c)3个索引,又 ...

  2. ping 127.0.0.1请求超时的解决办法?

    转自:http://blog.51cto.com/dengyong/1429699 打开网络连接,你很有可能启用了虚拟wifi.若有无线网卡就把无线网卡关掉,然后本地连接那里(就是有线网卡的那个连接) ...

  3. 用命令查看端口占用情况 netstat -ano

    查看所有端口 netstat -ano 可以看到进程ID 参考某个具体端口,第五列就是PID进程ID了. netstat -aon|findstr "80"

  4. Access restriction required library rt.jar

    在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...

  5. centos 端口iptables配置

    1.安装iptables yum install iptables* -y 2.打开端口 iptables -I INPUT -p tcp --dport -j ACCEPT 3.查看本机关于IPTA ...

  6. idea 修改Recent projects

    idea用了一段时间了,打开的项目多了,导致Open Recent列表中的项目也非常多,在找一个项目时很不方便. 后来查询,在~/Library/Preferences/IntelliJIdea目录/ ...

  7. ROS Learning-005 beginner_Tutorials 创建ROS程序包(就是软件包)

    ROS Indigo beginner_Tutorials-04 创建ROS程序包(就是软件包) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu ...

  8. C语言-郝斌笔记-006排序及查找

    1. int partion(int *a, int low, int high) { int value = a[low]; int t; while (low < high) { while ...

  9. 适合新手的Python爬虫小程序

    介绍:此程序是使用python做的一个爬虫小程序  爬取了python百度百科中的部分内容,因为这个demo是根据网站中的静态结构爬取的,所以如果百度百科词条的html结构发生变化 需要修改部分内容. ...

  10. Visual Studio 2012自动添加注释(如版权信息等)

    http://blog.csdn.net/jiejiaozhufu/article/details/16357721注释宏的原码 /********************************** ...