国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Mybatis select記錄封裝的實現

瀏覽:2日期:2023-10-22 09:14:35

select記錄封裝

返回一個List集合, resultType要寫集合中元素的類型

<!-- public List<Employee> getEmpsByLastNameLike(String lastName); --><!--resultType:如果返回的是一個集合,要寫集合中元素的類型 --><select resultType='com.atguigu.mybatis.bean.Employee'> select * from tbl_employee where last_name like #{lastName}</select>

返回一條記錄的map, key為列名, 值就是對應的值

<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); --><select resultType='map'> select * from tbl_employee where id=#{id}</select>

多條記錄封裝成一個map, key為id, 值是記錄封裝后的javaBean

//@MapKey:告訴mybatis封裝這個map的時候使用哪個屬性作為map的key@MapKey('lastName')public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);

<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); --><select resultType='com.atguigu.mybatis.bean.Employee'> select * from tbl_employee where last_name like #{lastName}</select>

自動映射配置

全局setting設置

1.autoMappingBehavior默認為PARTIAL, 開啟自動映射功能;唯一的要求是列名和javaBean屬性名一致

2.mapUnderscoreToCamelCase=true, 開啟自動駝峰命名規范映射功能

自定義resultMap, 實現高級映射功能

resultMap自定義映射規則

<!--自定義某個javaBean的封裝規則 type:自定義規則的Java類型 id:唯一id方便引用 --> <resultMap type='com.atguigu.mybatis.bean.Employee' id='MySimpleEmp'> <!--指定主鍵列的封裝規則 id定義主鍵會底層有優化; column:指定哪一列 property:指定對應的javaBean屬性 --> <id column='id' property='id'/> <!-- 定義普通列封裝規則 --> <result column='last_name' property='lastName'/> <!-- 其他不指定的列會自動封裝:我們只要寫resultMap就把全部的映射規則都寫上。 --> <result column='email' property='email'/> <result column='gender' property='gender'/> </resultMap>

創建表

create table tb_dept (id int(11) primary key auto_increment,dept_name varchar(255))

添加列

alter table tb_emp add column d_id int(11);

添加約束

alter table tb_emp add constraint fk_emp_dept foreign key(d_id) references tb_dept(id);

聯合查詢:級聯屬性封裝結果集

場景一:

查詢Employee的同時查詢員工對應的部門;一個員工有與之對應的部門信息;

<!-- 場景一: 查詢Employee的同時查詢員工對應的部門 Employee===Department 一個員工有與之對應的部門信息; id last_name gender d_id did dept_name (private Department dept;) --><!-- 聯合查詢:級聯屬性封裝結果集 --> <resultMap type='com.atguigu.mybatis.bean.Employee' id='MyDifEmp'> <id column='id' property='id'/> <result column='last_name' property='lastName'/> <result column='gender' property='gender'/> <result column='did' property='dept.id'/> <result column='dept_name' property='dept.departmentName'/> </resultMap>

使用association定義關聯的單個對象的封裝規則;

<!-- 使用association定義關聯的單個對象的封裝規則; --> <resultMap type='com.atguigu.mybatis.bean.Employee' id='MyDifEmp2'> <id column='id' property='id'/> <result column='last_name' property='lastName'/> <result column='gender' property='gender'/><!-- association可以指定聯合的javaBean對象 property='dept':指定哪個屬性是聯合的對象 javaType:指定這個屬性對象的類型[不能省略] --> <association property='dept' javaType='com.atguigu.mybatis.bean.Department'> <id column='did' property='id'/> <result column='dept_name' property='departmentName'/> </association> </resultMap>

association分步查詢

<!-- 使用association進行分步查詢: 1、先按照員工id查詢員工信息 2、根據查詢員工信息中的d_id值去部門表查出部門信息 3、部門設置到員工中; --> <!-- id last_name email gender d_id --> <resultMap type='com.atguigu.mybatis.bean.Employee' id='MyEmpByStep'> <id column='id' property='id'/> <result column='last_name' property='lastName'/> <result column='email' property='email'/> <result column='gender' property='gender'/> <!-- association定義關聯對象的封裝規則 select:表明當前屬性是調用select指定的方法查出的結果 column:指定將哪一列的值傳給這個方法 流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,并封裝給property指定的屬性 --> <association property='dept'select='com.atguigu.mybatis.dao.DepartmentMapper.getDeptById' column='d_id'> </association> </resultMap><!-- DepartmentMapper.xml --><mapper namespace='com.atguigu.mybatis.dao.DepartmentMapper'> <!--public Department getDeptById(Integer id); --> <select resultType='com.atguigu.mybatis.bean.Department'> select id,dept_name departmentName from tbl_dept where id=#{id} </select>

association分步查詢&延遲加載

<!-- 可以使用延遲加載(懶加載);(按需加載) Employee==>Dept: 我們每次查詢Employee對象的時候,都將一起查詢出來。 部門信息在我們使用的時候再去查詢; 分段查詢的基礎之上加上兩個配置: --> <!-- ==================association============================ --> <!-- mybatis-config.xml--> <!--顯示的指定每個我們需要更改的配置的值,即使他是默認的。防止版本更新帶來的問題 --> <setting name='lazyLoadingEnabled' value='true'/> <!-- value:false 表示按需加載; 否則會總是加載 --> <setting name='aggressiveLazyLoading' value='false'/>

關聯集合

嵌套結果集的方式,使用collection標簽定義關聯的集合類型的屬性封裝規則

場景二:

查詢部門的時候將部門對應的所有員工信息也查詢出來:注釋在DepartmentMapper.xml中

<!-- public class Department { private Integer id; private String departmentName; private List<Employee> emps; did dept_name || eid last_name email gender --> <!--嵌套結果集的方式,使用collection標簽定義關聯的集合類型的屬性封裝規則 --> <resultMap type='com.atguigu.mybatis.bean.Department' id='MyDept'> <id column='did' property='id'/> <result column='dept_name' property='departmentName'/> <!-- collection定義關聯集合類型的屬性的封裝規則 ofType:指定集合里面元素的類型 --> <collection property='emps' ofType='com.atguigu.mybatis.bean.Employee'> <!-- 定義這個集合中元素的封裝規則 --> <id column='eid' property='id'/> <result column='last_name' property='lastName'/> <result column='email' property='email'/> <result column='gender' property='gender'/> </collection> </resultMap> <!-- public Department getDeptByIdPlus(Integer id); --> <select resultMap='MyDept'> SELECT d.id did,d.dept_name dept_name,e.id eid,e.last_name last_name,e.email email,e.gender gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id} </select>

collection:分段查詢

<!-- collection:分段查詢 --> <resultMap type='com.atguigu.mybatis.bean.Department' id='MyDeptStep'> <id column='id' property='id'/> <result column='dept_name' property='departmentName'/> <collection property='emps' select='com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId' column='{deptId=id}' fetchType='lazy'></collection> </resultMap> <!-- 擴展:多列的值傳遞過去: 將多列的值封裝map傳遞; column='{key1=column1,key2=column2}' fetchType='lazy':表示使用延遲加載;- lazy:延遲- eager:立即

鑒別器

mybatis可以使用discriminator判斷某列的值,然后根據某列的值改變封裝行為

封裝Employee:如果查出的是女生:就把部門信息查詢出來,否則不查詢;如果是男生,把last_name這一列的值賦值給email;

<!-- =======================鑒別器============================ --> <resultMap type='com.atguigu.mybatis.bean.Employee' id='MyEmpDis'> <id column='id' property='id'/> <result column='last_name' property='lastName'/> <result column='email' property='email'/> <result column='gender' property='gender'/> <!-- column:指定判定的列名 javaType:列值對應的java類型 --> <discriminator javaType='string' column='gender'> <!--女生 resultType:指定封裝的結果類型;不能缺少。/resultMap--> <case value='0' resultType='com.atguigu.mybatis.bean.Employee'> <association property='dept' select='com.atguigu.mybatis.dao.DepartmentMapper.getDeptById' column='d_id'> </association> </case> <!--男生 ;如果是男生,把last_name這一列的值賦值給email; --> <case value='1' resultType='com.atguigu.mybatis.bean.Employee'> <id column='id' property='id'/> <result column='last_name' property='lastName'/> <result column='last_name' property='email'/> <result column='gender' property='gender'/> </case> </discriminator> </resultMap>

到此這篇關于Mybatis select記錄封裝的實現的文章就介紹到這了,更多相關Mybatis select封裝內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 国产一级做a爰片在线看免费 | 中国一级毛片 | 三级国产精品一区二区 | 国产精品久久久久久久久福利 | 欧美一级毛片片aa视频 | 精品一区二区三区在线播放 | 国产精品一区在线播放 | 在线日本看片免费人成视久网 | 好吊色37pao在线观看 | 亚洲免费视频网 | 亚洲视频中文字幕 | 美女张开腿让男人桶爽免费网站 | 欧美一级别 | 91久久国产精品视频 | 青青青免费手机版视频在线观看 | 亚洲在线观看视频 | 久久橹| 97视频免费播放观看在线视频 | 成人亚洲国产精品久久 | 国产亚洲欧美日韩在线看片 | 国产一区二区在线观看视频 | 国产精品黄页网站在线播放免费 | 免费一级毛片在线播放放视频 | 亚洲欧美激情视频 | 高清日本在线成人免费视频 | 国产无卡一级毛片aaa | 国产精品久久久久久久久岛 | 亚洲日产综合欧美一区二区 | 国产精品一区二区三区免费 | 长腿嫩模打开双腿呻吟 | 一级a做爰片欧欧美毛片4 | 特级a毛片 | 国产亚洲精品一区二区久久 | 久久亚洲精品成人 | 亚洲一级片在线播放 | 91精品欧美一区二区综合在线 | www中文字幕在线观看 | 美女很黄很黄免费的 | 深夜福利网站在线观看 | 国产欧美日韩在线不卡第一页 | 美国第一毛片 |