Mybatis实战 之 入参封装
来源:互联网 发布:拜耳驱虫药淘宝哪里买 编辑:程序博客网 时间:2024/06/14 10:25
Mybatis实战 之 入参封装
本节我们将详细学习一下Mybatis对传入参数的封装或处理。
实例
单个参数的处理
对于单个参数的传入,在Mapper接口中我们可以直接使用该参数例子
int deleteByPrimaryKey(Integer pid);
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from products where pid = #{pid,jdbcType=INTEGER} </delete>
传入多个参数
传入多个参数并且使用Mybatis默认的处理方式的话,Mybatis会将传入的参数按先后顺序转化为一个Map,第一个参数的Key值为 param1,第二个参数的Key值为 param2,依次类推。在Mapper接口中将直接使用param1…..paramN来代表指定位置的参数。例子
Product selectProductByCritiers(String pname,String type);
<select id="selectProductByCritiers" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products where pname = #{param1,jdbcType=VARCHAR} and type=#{param2,jdbcType=VARCHAR} </select>
入参有顺序限制。
使用@Param 注解传入多个参数
在使用@Param注解传入参数时,Mybatis将为每一个参数根据@Param中的值封装成一个Map,Map中的每一个Key值为每一个@Param中的值,无先后顺序,@Param中的值唯一。例子
Product selectProductByCritiers(@Param("productName")String pname,@Param("ProductType")String type);
<select id="selectProductByCritiers" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products where pname = #{productName,jdbcType=VARCHAR} and type=#{ProductType,jdbcType=VARCHAR} </select>
使用POJO 入参
在使用Mybatis 我们常用整个POJO 对象作为参数的传入,此时Mybatis 将会把 POJO 封装成一个Map,Key为属性名称,value值属性值。在使用参数是,直接使用Map中的Key值即属性名称即可例子
int insert(Product product);
<insert id="insert" parameterType="com.sstps.market.entity.Product"> insert into products (pid, pname, type, price, createTime) values (#{pid,jdbcType=INTEGER}, #{pname,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{price,jdbcType=DOUBLE}, #{createTime,jdbcType=TIMESTAMP}) </insert>
使用场景为传入参数恰好是POJO对象中对应的属性(可以不是全)。
- 使用Map 入参
从以上的入参形式可以看出,Mybatis 对参数的处理不管时那一种那是都会将入参参数封装成一个Map,Mapper接口取用的便是封装后Map的Key,使用的值便是Map对应Key的Value值。 例子
Map<String,Object> paramMap = new HashMap<>();paramMap.put("pname", "SpringBootDemo");paramMap.put("type", "第一次世界大战");Product product = martkeService.selectByMap(paramMap);
Product selectByMap(Map<String,Object> map);
<select id="selectByMap" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products where pname = #{pname,jdbcType=VARCHAR} and type=#{type,jdbcType=VARCHAR} </select>
一般使用场景为,实体POJO 对象无法满足传入参数对应的类型且参数较多时。
特殊场景
然而在实际的运用过程中以上的入参方式标准都无法满足我们的需求,那么我们可以看看一下特殊场景下入参的实现方式:部分参数声明
Product selectProductByCritiers(@Param("productName")String pname,String type);
<select id="selectProductByCritiers" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!--已声明的参数使用声明时的Value值--> where pname = #{productName,jdbcType=VARCHAR} <!--未声明的参数 按照顺序入参即可--> and type=#{param2,jdbcType=VARCHAR} </select>
多参数+POJO
Product selectProductByMultParam(@Param("id")Integer id,Product product );
<!--此时可以不用指定传入参数的类型--> <select id="selectProductByMultParam" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products where 1 = 1 <if test="pid != null"> <!--直接使用声明的参数--> and pid=#{id,jdbcType=INTEGER} </if> <if test="pname != null"> <!--使用POJO 默认的属性名称--> and pname =#{pname,jdbcType=VARCHAR} </if> <if test="type != null"> and type =#{type,jdbcType=VARCHAR} </if> <if test="price != null"> and price =#{price,jdbcType=DOUBLE} </if> <if test="orderByClause != null"> order by ${orderByClause} </if> </select>
使用其他Conllection入参
在实际的开发中我们也经常使用List/Set/Array来做参数传入,下面我们来详细学习一下各自使用的场景的实现吧。首先Mybatis 对 List/Set/Array 入参是可以直接使用 conllection 来使用指定的元素,并且Mybatis 对List 和Array 还做了特殊处理,可以直接使用list/array来使用指定元素。使用List 入参
Product selectProductByList(List<Object> paramList);
<select id="selectProductByList" parameterType="java.util.list" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!-- 此时我们规定传入的第一个参数时pid,第二个参数时type --> where pid=#{conlletion[0],jdbcType=INTEGER} and type=#{conlletion[1],jdbcType=VARCHAR} </select>
由于Mybatis 对List有做特殊处理因而可以是:
<select id="selectProductByList" parameterType="java.util.list" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!-- 此时我们规定传入的第一个参数时pid,第二个参数时type --> where pid=#{list[0],jdbcType=INTEGER} and type=#{list[1],jdbcType=VARCHAR} </select>
使用Aarray 数组入参
Product selectProductByArray(String [] array);
<select id="selectProductByArray" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!-- 此时我们规定传入的第一个参数时pid,第二个参数时type --> where pid=#{conllection[0],jdbcType=INTEGER} and type=#{conllection[1],jdbcType=VARCHAR} </select>
由于Mybatis 对Array 数组入参也做了特殊处理,因而可以是:
<select id="selectProductByArray" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!-- 此时我们规定传入的第一个参数时pid,第二个参数时type --> where pid=#{array[0],jdbcType=INTEGER} and type=#{array[1],jdbcType=VARCHAR} </select>
使用Set 入参
Product selectProductByHashSet(HashSet<Object> paramSet);
<select id="selectProductByHashSet" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from products <!-- 此时我们规定传入的第一个参数时pid,第二个参数时type --> where pid=#{conllection[0],jdbcType=INTEGER} and type=#{conllection[1],jdbcType=VARCHAR} </select>
小结
- Mybatis 提供了多样的方式来入参,但不论以哪一种方式入参,Mybatis都将会传入的参数封装成一个Map。
- Mybatis实战 之 入参封装
- Mybatis实战 之 Settings
- MyBatis学习--方法入参
- mybatis mapper.xml入参
- Mybatis方法入参处理
- Mybatis实战 之 Hello Mybatis(一)
- Mybatis实战一之入门
- mybatis入门实战之CRUD
- SpringBoot实战之整合mybatis
- Mybatis实战 之 获取主键
- Go Web 实战之HTTP的封装
- 电商类APP项目实战(一)之登入界面
- Mybatis系列之实战篇(上)
- Mybatis系列之实战篇(中)
- Mybatis系列之实战篇(下)
- 网上商城项目实战之spring mybatis整合
- MyBatis实战系列之(1)
- MyBatis实战系列之(2)
- java生成类似token的唯一随机字符串
- myeclipse2014安装JRebel插件
- 解决sqlite删除数据后,文件大小不变问题
- Android函数响应式编程——RxJava最快速度入门
- 171209之JS视频笔记1
- Mybatis实战 之 入参封装
- 2017-12-09 杭电OJ2007《平方和与立方和》
- SQL的四种连接-左外连接、右外连接、内连接、全连接
- 进程同步与信号量
- Nuix操作系统(尤尼斯)
- Python 函数
- C++中国大学mooc网----西安交大计算机程序设计第12周作业
- 油猴Tampermonkey-让百度云下载飞起来
- 8. String to Integer (atoi)