---
title: "ibatis的$与#的区别"
author: "cynhafa"
date: "2013-10-11T14:49:00Z"
tags: []
slug: "ibatis"
draft: false
lastmod: "2014-02-11T14:34:37Z"
id: 2014537
url: "https://cynhafa.iteye.com/blog/2014537"
---

\# 会根据不同类型而生产字符串，如 String 类型 : aa ---&gt; 'aa'  
$ 表示原生类型，如 String 类型 : aa ---&gt; aa

每天遇到问题记录

1.#变量名# 会转化为 jdbc 的 ？， 比如 select * from user where name=#name# 会转化为jdbc的 select * from user where name=？，把？参数设置为name的值，而$变量名$就直接把 $name$替换为 name的内容， 也就是由可能 select * from user where name=$name$ 如果name为 "' ' or 1 = 1", 那么这样就有可能导致sql注入，所以ibatis用#比$好，不会造成sql注入。

2.ibatis中的参数传入的值参数比较多，最好用bean方式传入，也就是通过set/get取值的方式给sql map注入参数，不要用hashmap结构传入，每次用hashmap传入会占用比较多的内容。如果参数少，用hashmap也比较方便简单。但是对传入参数的判断，用bean方式比较容易检测发现，配置也能够统一配置。

select * from user u where u.username=#name# //自动加''号  
select * from user u where age=$age$ and username=$username$ // username=$username$ 会出错

select * from $username$ u where age =$age$ order by $filed$ //$主要用处是动态表名 ，指定排序字端

[http://www.blogjava.net/xcp/archive/2011/05/17/350389.html](http://www.blogjava.net/xcp/archive/2011/05/17/350389.html)