mybatis二(参数处理和map封装及自定义resultMap)

摘要:
它还将传入的列表或数组封装在映射中。;从${year}中按year_salarywherexxx拆分选择*;select*fromtbl_Emplloyeorderby${f_name}${order}9,#{}:更丰富的用法:指定参数的一些规则:javaType、jdbcType、mode、numericScale、resultMap、typeHandler、jdbcTypeName、expression;JdbcType通常需要在特定条件下设置:当我们的数据为null时,某些数据库可能无法识别mybatis对null的默认处理。
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 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>

免责声明:文章转载自《mybatis二(参数处理和map封装及自定义resultMap)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇巫妖王之怒牌子综述Android WebView如何加载assets下的html文件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

snowflake 分布式唯一ID生成器

本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 原文参考运维生存和开源中国上的代码整理 我的环境是python3.5,pip8.2的 一、python版本 前言 由于考虑到以后要动态切分数据,防止将不同表切分数据到同一个表中时出现主键相等的冲突情况,这里我们使用...

Mysql性能优化

mysql的性能优化无法一蹴而就,必须一步一步慢慢来,从各个方面进行优化,最终性能就会有大的提升。 Mysql数据库的优化技术 对mysql优化是一个综合性的技术,主要包括 表的设计合理化(符合3NF) 添加适当索引(index) [四种: 普通索引、主键索引、唯一索引unique、全文索引] 分表技术(水平分割、垂直分割) 读写[写: update/d...

python 之 数据库(修改表、复制表、删除表、单表查询)

10.8 修改表、复制表、删除表 10.81 修改表 alter table 1. 修改表名 alter table表名 rename 新表名; 2. 增加字段 alter table 表名 add 字段名 数据类型 [完整性约束条件…]; alter table t1 add stu char(10) not nullafter name;...

Oracle WIHT AS 用法

1、with table as 相当于建个临时表(用于一个语句中某些中间结果放在临时表空间的SQL语句),Oracle 9i 新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面。语法就是with tempname as (select ....)select ...例子:with t as (select * from emp whe...

CDO学习2 CDO 入门教程Tutorial

#20210117#注:如果需要用cdo对数据进行截取,可参考buguse的几篇博文: 如何利用CDO从数据集中提取数据 CDO条件性选择数据 - 云+社区 - 腾讯云 CDO转换数据集格式 - 云+社区 - 腾讯云 --------------------------------------------------------------------...

HTML、jsp页面中radio,checkbox,select数据回显功能,默认被选中问题

最近常常遇到各种复选框、单选框、下拉框的默认被选中的问题,开始也是绞尽脑汁的想办法,今天写一篇学习总结的博文来写一下学习总结。 单选框(radio)默认被选中: 一、jstl技术进行回显 <input type="radio" name="sex" <f:if test="${c.sex=='男' }">checked="checked"...