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. AngularJS入门之如何快速上手

      概述: AngularJS(ng)是一个纯JS框架,AngularJS易于构建CRUD应用(CRUD意思是增删改查) 适用于以数据操作为主的SPA(Single Page Application) ...

  2. C++ 学习之---C++基础理论知识(也适合面试回顾)

    C++ 语言编写的基础练习 具体案例放在github中 github地址:https://github.com/Master-fd/C-Base 1. 操作符重载 2. 构造与析构 3. 函数模板 4 ...

  3. Eclipse中如何开启断言(Assert),方法有二

    Eclipse中如何开启断言(Assert),方法有二:1.Run -> Run Configurations -> Arguments页签 -> VM arguments文本框中加 ...

  4. jstl 判断 null

    <c:if test="${not empty object }"> ${object}不为空 </c:if>

  5. CoreData的增删改查

    首先使用CoreData创建Demo,勾上CoreData选项 然后创建Entity对象,点击Add Entity(+)按钮 生成Entity对象 重命名双击Entity选项,然后输入Person 设 ...

  6. 51NOD 1616 最小集合

    传送门 分析 不难发现集合中的数一定是集合内其它一堆数的$gcd$ 于是我们枚举$i$,统计原来集合中有几个数是$i$的倍数,设这个值为$f(i)$ 之后对于每个$i$如果不存在$f(x*i) = f ...

  7. Entity Framework Tutorial Basics(15):Querying with EDM

    Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...

  8. C++笔记--函数

    函数的定义和声明 函数的声明和定义都必须描述相同的类型,但是声明可以不写参数名,定义则必须写参数名,但是他们的参数名字可以不同. 一个局部变量被声明为static,那么这个局部变量将只会被初始化一次, ...

  9. leetcode 6 ZigZag Converesion

    class Solution { public: string convert(string s, int nRows) { if (nRows <= 1) return s; string r ...

  10. SVM浅析

    系列博客机器学习总结,主要参考书目<统计学习方法>--李航,涉及数学公式较多,以图片的形式表现.SVM是经典的线性分类方法,通过线性映射投射到希尔伯特空间(完备的赋范内积空间)得到了无穷维 ...