輸入參數:parameterType 兩種取值符號的異同java
1.類型爲簡單類型(8個基本類型+string)sql
不一樣點:app
a.#{任意值},${value} 其中的標識符只能是valuespa
b. #{}自動給String類型加上單引號(‘’) (自動類型轉換)code
${} 原樣輸出,適合於動態排序 (動態字段)對象
c.#{}能夠防止sql注入blog
${}不防止sql注入排序
相同點:ci
均可以獲取對象的值或者嵌套對象的值string
2.類型爲對象類型:
#{屬性名} ${屬性名}
輸出參數:ResultType和ResultMap
在屬性名和字段名不一致時的解決辦法:
1.使用ResultMap:
1 <select id="queryStudentById" parameterType="int" resultMap="student1"> 2 select * from student where sid = #{sid} 3 </select> 4 <resultMap type="student" id="student1"> 5 <id property="sid" column="sid" /> 6 <result property="sname" column="sname" /> 7 <result property="age" column="age" /> 8 <result property="sex" column="sex" /> 9 <association property="address" javaType="address"> 10 <result property="homeAddress" column="homeaddress" /> 11 <result property="schoolAddress" column="schooladdress" /> 12 </association> 13 </resultMap>
2.使用ResultType + HashMap解決:
1 <insert id="insertStudentWithHashMap" parameterType="HashMap"> 2 insert into student (sname,age,sex,homeaddress,schooladdress) value (#{sname},#{age},#{sex},#{homeaddress},#{schooladdress}) 3 </insert>
1 Map<String, Object> map = new HashMap<String, Object>(); 2 map.put("sname", "饅頭"); 3 map.put("age", 22); 4 map.put("sex", true); 5 map.put("homeaddress", "杭州"); 6 map.put("schooladdress", "北京"); 7 studentMapper.insertStudentWithHashMap(map);
1 //實體類
private int sno; 2 private String name; 3 private int age; 4 private Boolean sex; 5 private Address address;