RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
mybatis占位符#{}和${}的用法和区别

这篇文章主要讲解了“mybatis占位符#{}和${}的用法和区别”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis占位符#{}和${}的用法和区别”吧!

创新互联公司主营城区网站建设的网络公司,主营网站建设方案,成都app软件开发公司,城区h5重庆小程序开发搭建,城区网站营销推广欢迎城区等地区企业咨询

mybatis 之 占位符#{} 和 ${}

一. #{}占位符用来设置参数,参数的类型可以有3种:基本类型,自定义类型,Map。

基本类型:

基本类型,参数名称与占位符中的名称无关。

#{} 传入值时,sql解析时,参数是带引号的

如果用了@Param("xxx") ,则mybatis会自动生成map作为入参,那么参数名称则必须与占位符一致

   
           select * from t_role where id = #{xxxid}
       

测试:

@Test
       public void testSelectOne(){
           Role role = (Role)session.selectOne("cn.wh.mapper.RoleMapper.findById",1);
           System.out.println(role.getName());
       }

自定义类型

自定义类型作为参数,自定义类中需要为为属性提供get方法,如果没有提供get方法,那么会根据占位符中的名称去反射获取值,如果占位符中的名称和属性不一致,那么报ReflectionException。

   
           select * from t_role limit #{index},#{size}
       

测试:

   @Test
    public void testPage1(){
           PageUtil pu = new PageUtil();
           pu.setIndex(3);
           pu.setSize(3);
           List list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", pu);
           for(Role r:list){
               System.out.println(r.getName());
           }
       }

Map

Map作为参数类型,key和占位符中的名称一致即可,如果名称不一致那么将会把null,传递到占位符中。

注意:#{}占位符不能解决一下 3 类问题:

表名是动态的: Select * from #{table_name}

列名是动态的:Select #{column_name} from t_role

排序列是动态的: Select * from t_role order by #{columu}

二. ${}占位符是字符串连接符,可以用来动态设置表明,列名,排序名

${}传入值,sql解析时,参数是不带引号的。

因此${}参数不能为基本数据类型,只能为自定义类型和map



        select * from ${tableName}
 

测试:

   @Test
       public void testSelectList(){
           Map map = new HashMap();
           map.put("tableName", "t_role");
           List list = session.selectList("cn.wh.mapper.RoleMapper.findAll",map);
           for(Role role:list){
               System.out.println(role.getId()+"----"+role.getName());
           }
       }

作为连接符使用:

   
           select *from t_role where name like '${name}%';
    

测试:

   @Test
   public void testLike2(){
       Map map = new HashMap();
       map.put("name", "黄");
       List list = session.selectList("cn.wh.mapper.RoleMapper.selectLike1",map);
           for(Role r:list){
               System.out.println(r.getName());
           }
       }

三. #{}和${}的区别

在mybatis中的$与#都是在sql中动态的传入参数。

eg:select id,name,age from student where name=#{name} 这个name是动态的,可变的。当你传入什么样的值,就会根据你传入的值执行sql语句。

#{}: 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符,一个 #{ } 被解析为一个参数占位符 。

${}: 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换。

例子:

eg 1: select id,name,age from student where name=#{name} ; 解析为:select id,name,age from student where name='cy';

eg 2: select id,name,age from student where name=${name}; 解析为:select id,name,age from student where name=cy;

  1. #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id ='1'.

  2. $是将传入的数据直接显示生成sql语句,eg:select id,name,age from student where id =${id},当前端把id值1,传入到后台的时候,就相当于 select id,name,age from student where id = 1.

  3. 使用#可以很大程度上防止sql注入。(语句的拼接)

  4. 但是如果使用在order by 中就需要使用 $.

  5. 在大多数情况下还是经常使用#,但在不同情况下必须使用$.

  6. 我觉得#与的区别最大在于:#{} 传入值时,sql解析时,参数是带引号的,而的区别最大在于:#{} 传入值时,sql解析时,参数是带引号的,而{}穿入值,sql解析时,参数是不带引号的。

感谢各位的阅读,以上就是“mybatis占位符#{}和${}的用法和区别”的内容了,经过本文的学习后,相信大家对mybatis占位符#{}和${}的用法和区别这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


名称栏目:mybatis占位符#{}和${}的用法和区别
地址分享:http://scyingshan.cn/article/gieiec.html