05.MyBtais两种取值符号以及输入参数和输出参数
生活随笔
收集整理的這篇文章主要介紹了
05.MyBtais两种取值符号以及输入参数和输出参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入參數: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;
?
轉載于:https://www.cnblogs.com/man-tou/p/11336592.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的05.MyBtais两种取值符号以及输入参数和输出参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国科大高级人工智能10-强化学习(多臂赌
- 下一篇: day33-进程池和线程池