输入参数:parameterType 两种取值符号的异同
1.类型为简单类型(8个基本类型+string)
不同点:
a.#{任意值},${value} 其中的标识符只能是value
b. #{}自动给String类型加上单引号(‘’) (自动类型转换)
${} 原样输出,适合于动态排序 (动态字段)
c.#{}可以防止sql注入
${}不防止sql注入
相同点:
都可以获取对象的值或者嵌套对象的值
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;