${}是properties文件中的變量佔位符,它能夠用於xml標籤屬性值和sql內部,屬於字符串替換。html
<!-- ${driver} 會被替換爲 com.mysql.jdbc.driver --> <dataSource type="UNPOOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> </dataSource>
${}也能夠對傳遞進來的參數原樣拼接在sql中。實際場景中,不推薦使用${},由於會有sql注入的風險。mysql
#{}是sql的參數佔位符,mybatis會將sql中的#{}替換爲?號,在sql執行前會使用PreparedStatement的參數設置方法,按序給sql的?號佔位符設置參數。因此#{}是預編譯處理,能夠有效防止sql注入,提升系統安全性。sql
<setting name="mapUnderscoreToCamelCase" value="true" />
參考mybatis中文文檔數據庫
參考mybatis中文文檔編程
<if/> 、<where/>、 <set/>、 <choose/>、 <foreach/>、<when/>、 <otherwise/>、<trim/>緩存
不一樣的數據庫,獲取自動生成的主鍵的方式是不一樣的。mysql有兩種方式,代碼以下安全
// 方式一,使用 useGeneratedKeys + keyProperty 屬性 <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user(name, pswd) VALUE (#{name}, #{pswd}) </insert> // 方式二,使用 `<selectKey />` 標籤 <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id"> <selectKey keyProperty="id" resultType="long" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO user(name, pswd) VALUE (#{name}, #{pswd}) </insert>