mybatis二(參數處理和map封裝及自定義resultMap)


1、單個參數 mybatis不會做特殊處理。

  #{參數名/任意名}:取出參數值。

2、多個參數 mybatis會做特殊處理。

  多個參數會被封裝成 一個map。

  key:param1...paramN,或者參數的索引也可以。

  value:傳入的參數值。

  #{}就是從map中獲取指定的key的值;

  多個參數傳遞的時候要使用命名參數的形式:

3、命名參數:明確指定封裝參數時map的key;@Param("id")

  多個參數會被封裝成 一個map,
  key:使用@Param注解指定的值
  value:參數值
  #{指定的key}取出對應的參數值

4、PoJO

如果多個參數正好是我們業務邏輯的數據模型,我們就可以直接傳入pojo;#{屬性名}:取出傳入的pojo的屬性值。

5、Map:如果多個參數不是業務模型中的數據,沒有對應的pojo,不經常使用,為了方便,我們也可以傳入map。

     #{key}:取出map中對應的值

6、TO:如果多個參數不是業務模型中的數據,但是經常要使用,推薦來編寫一個TO(Transfer Object)數據傳輸對象。

  例如分頁:

    Page{
    int index;
    int size;
    }

7、例子:

 

public Employee getEmp(@Param("id")Integer id,String lastName);
    取值:id==>#{id/param1}   lastName==>#{param2}
 

public Employee getEmp(Integer id,@Param("e")Employee emp); 取值:id==>#{param1}    lastName===>#{param2.lastName/e.lastName}
  特別注意: 如果是Collection(List、Set)類型或者是數組,也會特殊處理。也是把傳入的list或者數組封裝在map中。   key的取值:       key:Collection(collection)。          List(list)          數組(array) public Employee getEmpById(List<Integer> ids); 取值:取出第一個id的值: #{list[0]} 8、參數值的獲取

  #{}:可以獲取map中的值或者pojo對象屬性的值;

  ${}:可以獲取map中的值或者pojo對象屬性的值;

select * from tbl_employee where id=${id} and last_name=#{lastName}
Preparing: select * from tbl_employee where id=2 and last_name=?
  區別:

    #{}:是以預編譯的形式,將參數設置到sql語句中;PreparedStatement;防止sql注入。

    ${}:取出的值直接拼裝在sql語句中;會有安全問題;大多情況下,我們去參數的值都應該去使用#{};

  原生jdbc不支持占位符的地方我們就可以使用${}進行取值,比如分表、排序。。。;按照年份分表拆分 

select * from ${year}_salary where xxx;
select * from tbl_employee order by ${f_name} ${order}
9、#{}:更豐富的用法:

  規定參數的一些規則:

  javaType、 jdbcType、 mode(存儲過程)、 numericScale、

  resultMap、 typeHandler、 jdbcTypeName、 expression(未來准備支持的功能);

 

  jdbcType通常需要在某種特定的條件下被設置:

    在我們數據為null的時候,有些數據庫可能不能識別mybatis對null的默認處理。比如Oracle(報錯);

    JdbcType OTHER:無效的類型;因為mybatis對所有的null都映射的是原生Jdbc的OTHER類型,oracle不能正確處理;

    由於全局配置中:jdbcTypeForNull=OTHER;oracle不支持;兩種辦法

    1、#{email,jdbcType=OTHER};

    2、jdbcTypeForNull=NULL

       <setting name="jdbcTypeForNull" value="NULL"/>

 二、封裝MAP

//返回一個map,key是列名,value是值
    public Map<String,Object> getUserByIdReturnMap(Integer id);
    //返回一個map,key是主鍵,value是值
    @MapKey("id")
    public Map<Integer,User> getUserByLastName(String lastName);
    

mapper配置

     <select id="getUserByLastName" resultType="model.User">
        SELECT * FROM
        user WHERE last_name like "%"#{lastName}"%"
    </select>
    <select id="getUserByIdReturnMap" resultType="map">
        SELECT * FROM
        user WHERE id=#{id}
    </select>

 三、自定義返回值類型

<1>

 實體類定義別名

@Alias("user")
public class User {
    
    private int id;
    private String lastName;
    private String email;
    private String gender;

public User selectUserById(Integer id);方法

mapper配置

<!-- 自定義javabeen的規則
        type自定義java規則
        id 唯一id方便引用
        column:指定哪一列
        property:指定javaBean屬性
     -->
    <resultMap type="user" id="Myuser">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>
    <select id="selectUserById" resultMap="Myuser">
        SELECT * FROM
        user WHERE id=#{id}
    </select>

 <2>

@Alias("user")
public class User {
    
    private int id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept;
@Alias("depart")
public class Department {
    
    private Integer id;
    private String departName;
    

 

public User selectUserAndDepart(Integer id);

mapper配置

<resultMap type="user" id="Mymap">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <result column="depart_id" property="dept.id"/>
        <result column="depart_name" property="dept.departName"/>
    </resultMap>
    <select id="selectUserAndDepart" resultMap="Mymap">
    SELECT u.id,u.depart_id,u.email,u.gender,u.last_name,d.depart_name FROM USER
    u,department d WHERE u.depart_id=d.id AND u.id=#{id}
    </select>

 mapper配置2

<resultMap type="user" id="Mymap2">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <association property="dept" javaType="depart">
            <id column="id" property="id"/>
            <result column="depart_name" property="departName"/>
        </association>
    </resultMap>

<select id="selectUserAndDepart" resultMap="Mymap2">
SELECT u.id,u.depart_id,u.email,u.gender,u.last_name,d.depart_name FROM USER
u,department d WHERE u.depart_id=d.id AND u.id=#{id}
</select>

 <3>分布查詢

<resultMap type="user" id="stepMap">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <association property="dept" select="mapper.DepartMapper.selectDepartById" column="depart_id" >
    
<association property="dept" select="mapper.DepartMapper.selectDepartById" column="{id=depart_id}" fetchType="eager">
        </association> {key1=column1,key2=column2}(多個參數) fetchType="lazy"(延遲加載)||"eager"(立刻加載)

 

        </association>
    </resultMap>
    <!-- 分布查詢 -->
    <select id="selectUserByIdStep" resultMap="stepMap">
        SELECT * FROM USER WHERE id =#{id}
    </select>

<mapper namespace="mapper.DepartMapper">
    <select id="selectDepartById" resultType="depart">
        SELECT * FROM
        department WHERE id=#{id}
    </select>
</mapper>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM