温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis占位符#{}和${}的用法和区别

发布时间:2021-06-26 09:56:56 来源:亿速云 阅读:164 作者:chen 栏目:大数据

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

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

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

基本类型:

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

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

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

   <select id="findById" parameterType="int" resultType="cn.wh.vo.Role">
           select * from t_role where id = #{xxxid}
       </select>

测试:

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

自定义类型

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

   <select id="findListBypage" parameterType="cn.wh.util.PageUtil" resultType="Role">
           select * from t_role limit #{index},#{size}
       </select>

测试:

   @Test
    public void testPage1(){
           PageUtil pu = new PageUtil();
           pu.setIndex(3);
           pu.setSize(3);
           List<Role> 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 id="findAll" parameterType="map" resultType="cn.wh.vo.Role">
        select * from ${tableName}
 </select>

测试:

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

作为连接符使用:

   <select id="selectLike1" parameterType="map" resultType="Role">
           select *from t_role where name like '${name}%';
    </select>

测试:

   @Test
   public void testLike2(){
       Map<String,String> map = new HashMap<String,String>();
       map.put("name", "黄");
       List<Role> 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占位符#{}和${}的用法和区别这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI