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. 将本地代码上传至github

    注册github账号 https://github.com/ 安装git工具 https://git-for-windows.github.io 1.在github中创建一个项目 2.填写相应信息,点 ...

  2. VxVM如何扩展和收缩卷及文件系统

    1. 同时扩展卷和文件系统 先用vxassist命令检查DG可用空间 [root@rhelnode1 ~]# vxassist -g testdg maxsize Maximum volume siz ...

  3. 一次Mono解析Excel文档编码出错排查记录

    最近在捯饬Asp.Net站点部署到Linux平台上面,在文档导入的操作中经过网上搜索采用了能够支持跨平台的ExcelDataReader组建.在本地windows上测试通过NuGet安装的组建,这货依 ...

  4. 将openfire部署到CentOS云服务器上

    http://ishere.cn/2014/07/25/centos-64bit-openfire.html      CentOS 64位安装openfire http://www.cnblogs. ...

  5. ThinkPad E431按F1后直接进入系统无法进入BIOS

    联想的ThinkPad系列笔记本一般是按F1进如BIOS的,但是由于现在联想的笔记本多数都是预装Win 8或者更高版本的系统,所以有时候就没办法直接按F1进去BIOS.其原因是因为Win 8或者更高版 ...

  6. python的print字符串的输出

    字符串的输出使用print语句,在每个语句的输出的时候我们使用' '和" "来包含字符串比如: 如果有多个字符串的话呢我们需要用”,“来进行连接: 我们不仅可以使用字符来进行输出时 ...

  7. 重命名File

    File completeFile = new File(mFilePath + mFileName); if (completeFile.exists()) { File fileWithSuffi ...

  8. 修改oracle xe的8080端口

    1.用sys管理员身份登录,利用dbms_xdb修改端口设置 SQL> -- Change the HTTP/WEBDAV port from 8080 to 8081 SQL> call ...

  9. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  10. hibernate里的实体类中不能重写toString

    @Test报堆栈溢出,   在main中报错toString