Mybatis
1、mybatis中where和where标签
属性 描述 :prefix 给sql语句拼接的前缀,suffix 给sql语句拼接的后缀,prefixOverrides 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND",suffixOverrides 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定。
<!--使用trim标签的形式--> <trim prefix="where" prefixOverrides="and"> <if test="name!=null"> name=#{name} </if> <if test="mobile!=null"> and mobile=#{mobile} </if> </trim>
2、mybatis中foreach的使用
<!--批量删除用户信息 遍历集合的时候collection="array"必须是,如果传入的是数组可以通过@Param指定参数的对应关系--> <delete id="deleteBatchAccountInfo" parameterType="java.lang.String"> DELETE FROM sys_account WHERE id IN <foreach collection="array" open="(" close=")" separator="," item="id"> #{id} </foreach> </delete>
item:集合中元素迭代时的别名,该参数为必选。
index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选
open:foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选
separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
collection: 要做foreach的对象,作为入参时,List对象默认用"list"代替作为键,数组对象有"array"代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids".如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
例如更新操作:
<!-- 根据id进行批量更新--> <update id="updateBatchOrderInfo" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" separator=";"> update purchase_order <set> <if test="item.status != null"> status = #{item.status} </if> <if test="item.updateTime != null"> update_time = #{item.updateTime}, </if> </set> where id = #{item.id} </foreach> </update>
例如插入操作:
<!--进行批量插入--> <insert id="saveBatchEnterWarehouse" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"> insert into enter_warehouse_info (enter_warehouse_no, enter_number, enter_warehouse_type, warehouse_id, basic_product_id, purchase_order_id, purchase_detail_id, supplier_id, create_time, creator, create_name, update_time, updater,department_id) values <foreach collection="list" item="item" separator="," > (#{item.enterWarehouseNo}, #{item.enterNumber}, #{item.enterWarehouseType}, #{item.warehouseId}, #{item.basicProductId}, #{item.purchaseOrderId}, #{item.purchaseDetailId}, #{item.supplierId}, now(), #{item.creator}, #{item.createName}, now(), #{item.updater},#{item.departmentId}) </foreach> </insert>
3、mybatis的xml文件中写动态sql时需要注意
<select id="search" parameterType="map" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from sys_account WHERE <if test="name!=null"> name=#{name} </if> <if test="mobile!=null"> and mobile=#{mobile} </if> </select>
写动态sql 的时候需要注意po和数据库的字段的对应关系,比如上面的例子中mobile和#{mobile}分别对应数据库中字段和po实体类中的属性字段。
4、Mybatis中的selectkey标签
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> SELECT LAST_INSERT_ID() </selectKey>
作用:在某些业务逻辑中需要获取数据库表的主键,因此需要使用selectkey标签获取主键。
keyProperty:对应的model中的主键的属性名,跟数据库的主键对应
order:AFTER 表示 SELECT LAST_INSERT_ID() 在insert执行之后执行,多用与自增主键; BEFORE 表示 SELECT LAST_INSERT_ID() 在insert执行之前执行,这样的话就拿不到主键了,适合那种主键不是自增的类型
resultType:主键类型
5、Mybatis中sql语句的条件判断和case when的使用
单个if判断:
select <include refid="Base_Column_List" />, if(event_type = 1 ,'减少','增加') as event_type_name from customer_advance_record
多个if嵌套判断:
select <include refid="Base_Column_List" />, if(event_type =4,'nnnnn',if(event_type=3,'asdd',if(event_type=2,'上调额度','ghgh'))) as event_type_name from customer_advance_record
case when end的使用:
select case event_type when 0 then '上调额度' when 1 then '下调额度' when 2 then '临时额度' when 3 then '还款冲账' when 4 then '预支' end 'even_type_name' from customer_credit_record;