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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Mybatis select記錄封裝的實(shí)現(xiàn)

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

select記錄封裝

返回一個(gè)List集合, resultType要寫(xiě)集合中元素的類(lèi)型

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

返回一條記錄的map, key為列名, 值就是對(duì)應(yīng)的值

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

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

//@MapKey:告訴mybatis封裝這個(gè)map的時(shí)候使用哪個(gè)屬性作為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>

自動(dòng)映射配置

全局setting設(shè)置

1.autoMappingBehavior默認(rèn)為PARTIAL, 開(kāi)啟自動(dòng)映射功能;唯一的要求是列名和javaBean屬性名一致

2.mapUnderscoreToCamelCase=true, 開(kāi)啟自動(dòng)駝峰命名規(guī)范映射功能

自定義resultMap, 實(shí)現(xiàn)高級(jí)映射功能

resultMap自定義映射規(guī)則

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

創(chuàng)建表

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);

聯(lián)合查詢(xún):級(jí)聯(lián)屬性封裝結(jié)果集

場(chǎng)景一:

查詢(xún)Employee的同時(shí)查詢(xún)員工對(duì)應(yīng)的部門(mén);一個(gè)員工有與之對(duì)應(yīng)的部門(mén)信息;

<!-- 場(chǎng)景一: 查詢(xún)Employee的同時(shí)查詢(xún)員工對(duì)應(yīng)的部門(mén) Employee===Department 一個(gè)員工有與之對(duì)應(yīng)的部門(mén)信息; id last_name gender d_id did dept_name (private Department dept;) --><!-- 聯(lián)合查詢(xún):級(jí)聯(lián)屬性封裝結(jié)果集 --> <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定義關(guān)聯(lián)的單個(gè)對(duì)象的封裝規(guī)則;

<!-- 使用association定義關(guān)聯(lián)的單個(gè)對(duì)象的封裝規(guī)則; --> <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可以指定聯(lián)合的javaBean對(duì)象 property='dept':指定哪個(gè)屬性是聯(lián)合的對(duì)象 javaType:指定這個(gè)屬性對(duì)象的類(lèi)型[不能省略] --> <association property='dept' javaType='com.atguigu.mybatis.bean.Department'> <id column='did' property='id'/> <result column='dept_name' property='departmentName'/> </association> </resultMap>

association分步查詢(xún)

<!-- 使用association進(jìn)行分步查詢(xún): 1、先按照員工id查詢(xún)員工信息 2、根據(jù)查詢(xún)員工信息中的d_id值去部門(mén)表查出部門(mén)信息 3、部門(mén)設(shè)置到員工中; --> <!-- 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定義關(guān)聯(lián)對(duì)象的封裝規(guī)則 select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果 column:指定將哪一列的值傳給這個(gè)方法 流程:使用select指定的方法(傳入column指定的這列參數(shù)的值)查出對(duì)象,并封裝給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分步查詢(xún)&延遲加載

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

關(guān)聯(lián)集合

嵌套結(jié)果集的方式,使用collection標(biāo)簽定義關(guān)聯(lián)的集合類(lèi)型的屬性封裝規(guī)則

場(chǎng)景二:

查詢(xún)部門(mén)的時(shí)候?qū)⒉块T(mén)對(duì)應(yīng)的所有員工信息也查詢(xún)出來(lái):注釋在DepartmentMapper.xml中

<!-- public class Department { private Integer id; private String departmentName; private List<Employee> emps; did dept_name || eid last_name email gender --> <!--嵌套結(jié)果集的方式,使用collection標(biāo)簽定義關(guān)聯(lián)的集合類(lèi)型的屬性封裝規(guī)則 --> <resultMap type='com.atguigu.mybatis.bean.Department' id='MyDept'> <id column='did' property='id'/> <result column='dept_name' property='departmentName'/> <!-- collection定義關(guān)聯(lián)集合類(lèi)型的屬性的封裝規(guī)則 ofType:指定集合里面元素的類(lèi)型 --> <collection property='emps' ofType='com.atguigu.mybatis.bean.Employee'> <!-- 定義這個(gè)集合中元素的封裝規(guī)則 --> <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:分段查詢(xún)

<!-- collection:分段查詢(xún) --> <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> <!-- 擴(kuò)展:多列的值傳遞過(guò)去: 將多列的值封裝map傳遞; column='{key1=column1,key2=column2}' fetchType='lazy':表示使用延遲加載;- lazy:延遲- eager:立即

鑒別器

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

封裝Employee:如果查出的是女生:就把部門(mén)信息查詢(xún)出來(lái),否則不查詢(xún);如果是男生,把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:列值對(duì)應(yīng)的java類(lèi)型 --> <discriminator javaType='string' column='gender'> <!--女生 resultType:指定封裝的結(jié)果類(lèi)型;不能缺少。/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>

到此這篇關(guān)于Mybatis select記錄封裝的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis select封裝內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

相關(guān)文章:
主站蜘蛛池模板: 成人午夜精品 | 久久免费在线视频 | 久久99精品久久久久久国产越南 | 亚洲欧美日韩国产vr在线观 | 极品美女一级毛片 | 国产亚洲精品日韩已满十八 | 99久久精品一区二区三区 | 在线三级网址 | 欧美久久亚洲精品 | 亚洲天堂久久精品成人 | 可以看毛片的网站 | 日韩欧美一区二区三区免费观看 | 欧美成人午夜做爰视频在线观看 | 精品国产91久久久久久久 | 亚洲国产欧美精品一区二区三区 | 欧美一级xxxx俄罗斯一级 | 曰本美女高清在线观看免费 | 日本三级午夜 | 欧美三级美国一级 | 精品国产香蕉在线播出 | 91色综合综合热五月激情 | 青木玲中文字幕一区二区 | 欧美的高清视频在线观看 | 在线黄网 | 九九视频精品全部免费播放 | 欧美特欧美特级一片 | 中国成人在线视频 | 欧美精品一区视频 | 免费一级a毛片免费观看欧美大片 | 日韩欧美印度一级毛片 | a级片在线观看免费 | 久久久久女人精品毛片 | 亚洲精品亚洲人成在线 | 97在线免费视频 | 久久久久综合 | 欧美极品第1页专区 | 男女福利 | 一个人的视频日本免费 | jiz欧美高清 | 成人网中文字幕色 | 成人1000部免费观看视频 |