Mybatis

本文详细介绍了MyBatis框架中的Mapper.xml文件结构,包括头部声明、SQL片段的复用、模糊查询的编写,以及trim标签如何处理SQL语句中的多余字符,如去除and和逗号,添加前缀后缀等,适用于参数化插入、更新、删除和查询操作。
摘要由CSDN通过智能技术生成

1.Mapper.xml的头文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
    
</mapper>
 

2.Sql片段

将复用的代码段提取出来

例子:
<sql id="BASE_COLUMN_LIST">
    id,username,password,name,phone,email,avatar,role
</sql>

3.Mapper.xml中的模糊查询

username like concat('%',#{username},'%')

4.trim标签的使用

4.1 去除sql语句中多余的and关键字,逗号

4.2 给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀

4.3 可用于选择性插入、更新、删除或者条件查询等操作
 

<insert id="insert" parameterType="com.example.enetity.Admin" useGeneratedKeys="true">
        insert into admin
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="username != null ">username,</if>
            <if test="password != null ">password,</if>
            <if test="name != null ">name,</if>
            <if test="phone != null ">phone,</if>
            <if test="email != null ">email,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="username != null ">#{username},</if>
            <if test="password != null ">#{password},</if>
            <if test="name != null ">#{name},</if>
            <if test="phone != null ">#{phone},</if>
            <if test="email != null ">#{email},</if>
        </trim>
</insert>

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值