最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis返回值(resultType&resultMap)的具體使用

 更新時間:2023年08月18日 15:44:22   作者:Myovlmx  
返回值屬性有兩種設(shè)置,一種是resultType,一種是resultMap,本文主要介紹了Mybatis返回值(resultType&resultMap)的具體使用,具有一定的參考價值,感興趣的可以了解一下

之前的文章里面有對resultType和resultMap的簡單介紹這一期出點詳細(xì)的

resultType:

1,返回值為簡單類型。

直接使用resultType=“類型”,如string,Integer等。

 String getEmpNameById(Integer id);
<!-- 
        指定 resultType 返回值類型時 String 類型的,
        string 在這里是一個別名,代表的是 java.lang.String 
        對于引用數(shù)據(jù)類型,都是將大寫字母轉(zhuǎn)小寫,比如 HashMap 對應(yīng)的別名是 'hashmap'
        基本數(shù)據(jù)類型考慮到重復(fù)的問題,會在其前面加上 '_',比如 byte 對應(yīng)的別名是 '_byte'
    -->
    <select id="getEmpNameById" resultType="string">
        select username from t_employee where id = #{id}
    </select>

2.返回值為List類型。

使用resultType=“list元素的類型”,一般是實體類如User,也可以是Map,對應(yīng)返回值類型是List<User> , List<Map<String,Object>>,不管是哪種,最終結(jié)果會根據(jù)接口返回值類型自動將多個 resultType指定的類型的元素(User或以一條記錄為一個Map)組裝成List。

 List<User> getUser(String age);
<select id="getUser" resultType="User">
        select * from user
        where
        age = #{age}
    </select>
 List<Map<String,Object>> findUserList();
 <select id="findUserList"  parameterType="int" resultType="java.util.Map">
        SELECT * FROM user
    </select>

 用java.util.List也是可以的,java.util.Map也是可以的至于為什么我沒有沒有研究過

 3.返回值為Map類型。(使用map要注意查詢結(jié)果的條數(shù),多條會報錯)

1. 如果查詢的結(jié)果是一條,我們可以把查詢的數(shù)據(jù)以{表字段名, 對應(yīng)的值}方式存入到Map中。

   Map<String, Object> getEmpAsMapById(Integer id);
  <!-- 
        注意這里的 resultType 返回值類型是 'map'
     -->
    <select id="getEmpAsMapById" resultType="map">
        select * from t_employee where id = #{id}
    </select>

2. 如果查詢的結(jié)果是多條數(shù)據(jù),我們也可以把查詢的數(shù)據(jù)以{表中某一字段名, JavaBean}方式來封裝成Map

// 查詢所有員工的信息,把數(shù)據(jù)庫中的 'id' 字段作為 key,對應(yīng)的 value 封裝成 Employee 對象
    // @MapKey 中的值表示用數(shù)據(jù)庫中的哪個字段名作 key
    @MapKey("id")
    Map<Integer, Employee> getAllEmpsAsMap();
 <!--
        注意 resultType 返回值類型,不再是 'map',而是 Map 的 value 對應(yīng)的 JavaBean 類型
    -->
    <select id="getAllEmpsAsMap" resultType="employee">
        select * from t_employee
    </select>

 擴展. 上面返回結(jié)果的形式都是基于查詢 (select) 的,其實對于增刪改的操作也可以返回一定類型的數(shù)據(jù),比如Boolean,Integer等。

 resultMap:

 屬于自定義的映射,用于由于各種原因數(shù)據(jù)庫的字段名跟實體類的字段名不一致

1.Emp實體類的屬性:

    private Integer empId;
    private String empName;
    private Integer age;
    private String gender;

2.表字段名: 

emp_idemp_nameagegenderdept_id

  3.映射文件配置:

<!--  Emp getEmpById(@Param("emp_id") Integer emp_id);-->
    <select id="getEmpById" resultType="Emp">
        select * from t_emp where emp_id = #{emp_id}
    </select>

4.執(zhí)行結(jié)果發(fā)現(xiàn)empId=null,empName=null

原因:數(shù)據(jù)庫表字段名中的emp_id,emp_name與實體類的屬性名empId,empName不一致,由數(shù)據(jù)庫不能映射到實體類屬性對應(yīng)的屬性名

 1. 方式一 字段名設(shè)置別名

select emp_id empid from emp where emp_id  =  #{emp_id}

 2. 方式二 下劃線映射為駝峰(在配置文件中配置)

<!--引入properties文件,此時就可以${屬性名}的方式訪問屬性值-->
    <properties resource="jdbc.properties"/>
<!--配置mybatis自動轉(zhuǎn)換為駝峰式命名-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--開啟延遲加載-->
        <setting name="lazyLoadingEnabled" value="true" />
    </settings>

3. 方式三 自定義映射resultMap

resultMap: 設(shè)置自定義的映射關(guān)系
id: 唯一標(biāo)識–>resultMap=" "
type: 處理映射關(guān)系的實體類的類型
標(biāo)簽:
id: 處理主鍵和實體類中屬性的映射關(guān)系
result: 處理普通字段和實體類中屬性的映射關(guān)系
column: 設(shè)置映射關(guān)系中的字段名,必須是sql中的某字段
property: 設(shè)置映射關(guān)系中的屬性的屬性名,必須為實體類中的屬性名

 <resultMap id="empDeptMapResultMapOne" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result column="did" property="dept.did"></result>
        <result column="dname" property="dept.dname"></result>
  </resultMap>
<select id="getEmpAndDept" resultMap="empDeptMapResultMapTwo">
        select emp.*,dept.* from emp left join dept on emp.did = dept.did where emp.eid = #{eid}
 </select>

 到此這篇關(guān)于Mybatis返回值(resultType&resultMap)的具體使用的文章就介紹到這了,更多相關(guān)Mybatis返回值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

门源| 玉环县| 灵宝市| 婺源县| 皋兰县| 海兴县| 沽源县| 香格里拉县| 湘潭县| 仙居县| 容城县| 万宁市| 慈溪市| 泰顺县| 平泉县| 延津县| 手机| 翁源县| 霍山县| 五原县| 昭苏县| 海原县| 多伦县| 东兴市| 疏附县| 台山市| 拉萨市| 遵义县| 宣武区| 天门市| 璧山县| 股票| 绥化市| 舞钢市| 彭州市| 剑川县| 涿州市| 兴和县| 柏乡县| 天镇县| 乌鲁木齐市|