日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis配置文件resultMap映射啥时候可写可不写?

發布時間:2025/3/11 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis配置文件resultMap映射啥时候可写可不写? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1、student實體類

public class Student {private Integer id;//編號private String name;//姓名private Double sal;//薪水public Student(){}public Student(Integer id, String name, Double sal) {this.id = id;this.name = name;this.sal = sal;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getSal() {return sal;}public void setSal(Double sal) {this.sal = sal;} }
2、students表結構 1? 字段名與實體類字段名

create table students(id int(5) primary key,name varchar(10),sal double(8,2) );


2.1、resultMap 映射代碼

<mapper namespace="studentNamespace"> <resultMap type="mybatis.hello.Student" id="studentMap"><id property="id" column="id"/><result property="name" column="name"/><result property="sal" column="sal"/></resultMap></mapper>


3、students表結構 2 字段名與實體類字段名 不同

create table students(students_id int(5) primary key,students_name varchar(10),students_sal double(8,2) );


3.1、resultMap 映射代碼

<mapper namespace="studentNamespace"> <resultMap type="mybatis.hello.Student" id="studentMap"><id property="id" column="students_id"/><result property="name" column="students_name"/><result property="sal" column="students_sal"/></resultMap></mapper>


1、可不寫

當實體屬性與表字段名相同的時候,即上面的1和2的情況,2.1resultMap映射代碼可不

select時,返回用 resultType?

<select id="findById" parameterType="int" resultType="mybatis.hello.Student">select id,name,sal from students where id = #{id}</select>

2、必須寫

當實體屬性與表字段名不同的時候,即上面的1和3的情況,3.1resultMap映射代碼必須寫。

select時,返回用 resultMap?

<select id="findById" parameterType="int" resultMap="studentMap">select students_id,students_name,students_salfrom studentswhere students_id = #{xxxxxxxxxxxxxxxxxx} </select>

3、為什么相同可不寫,不同必須寫?

因為用了java反射技術,如果列名和實體類字段名不同,則反射不成功。




總結

以上是生活随笔為你收集整理的Mybatis配置文件resultMap映射啥时候可写可不写?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。