当前位置: 首页 > news >正文

MyBatis动态sql标签帮你轻松搞定sql拼接

动态sql介绍

由于在开发过程不同的业务中会用到不同的操作条件,如果每个业务都拼接不同sql语句的话会是一个庞大的工作量;此时动态sql就能解决这个问题,可以针对不确定的操作条件动态拼接sql语句,根据提交的条件来完成业务sql的执行!

sql根标签

<insert>,<update>,<select>,<delete>

动态sql标签

<if>,<choose>,<when>,<otherwise>,<trim>,<foreach>,<where>,<set>,<bind>

关联关系标签

<collection>,<association>

sql根标签介绍

01<select>

<select id="getById" resultMap="orderMap"/>

02<insert>

<update id="update" parameterType="employee"/>

03<update>

<insert id="insert" puseGeneratedKeys="true" keyProperty="id"/>

04<delete>

<delete id="delete" parameterType="employee"/>

注意哦

parameterType与resultMap为查询返回结果,且不能同时存在,id不能重复,插入时候使用puseGeneratedKeys、keyProperty来保证主键生成

常用动态sql标签介绍

01<foreach>

//循环插入等场景使用foreach

<select id="selectIDInEids" parameterType="java.util.List" resultType="com.atguigu.ssm.pojo.Employee">

select * from employee where id in

<foreach collection="eids" item="eid" open="(" separator="," close=")">

${eid}

</foreach>

</select>

02<if>

//通常用来处理条件语句的判断

<select id="queryEmployeeIf" parameterType="employee" resultType="employee">

select * from employee where 1=1

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != '' ">

and gender like '%${gender}%'

</if>

</select>

03<choose><when><otherwise>

<select id="queryEmployeeChoose" parameterType="employee" resultType="employee">

select * from employee where 1=1

<choose>

<when test="empName != null and empName != ''">

and emp_name = #{empName}

</when>

<when test="gender != null and gender != '' ">

and gender like '%${gender}%'

</when>

<otherwise>

and id = 18

</otherwise>

</choose>

</select>

04<where>

// where标签标明,有符合条件则主动拼接条件;不需要额外拼接where 1=1

<select id="queryEmployeeWhere" parameterType="employee" resultType="employee">

select * from employee

<where>

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != ''">

and gender = #{gender}

</if>

</where>

</select>

05<trim>

// 效果与 where类似,前缀添加where,如果符合条件则提出默认and。

<select id="queryEmployeeTrim" parameterType="employee" resultType="employee">

select * from employee

<trim prefix="where" prefixOverrides="and" >

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != ''">

and gender = #{gender}

</if>

</trim>

</select>

06<set>

// set标签可以用来进行更新操作,mybatis操作更新只需要指定对应元素即可进行局部更新,显的更加灵活

<update id="updateEmployeeInfo" parameterType="employee" >

update employee

<set>

<if test="empName != null and empName != ''">

emp_name=#{empName},

</if>

<if test="gender != null">

gender=#{gender},

</if>

</set>

where id=#{id}

</update>

07<bind>

// 该标签主要为了给传递的参数添加特色标签而完成特殊的需求;

// 如模糊查询可以通过该标签添加%name%

<select id="queryEmployeeInfoByBind" parameterType="employee" resultType="employee">

<bind name="pattern" value="'%' + empName + '%'"/>

select * from student where empName like #{pattern}

</select>

注意哦

01字符串空值问题

字符串变量需要针对null值和空字符串判断哦;

02基本类型转换问题

针对基本的类型的变量如果实体与前台字段中存在空字符串会报错哦,所以实例变量最好使用其包装类作为基本类型的替换类型;

03xml解析&&问题

动态sql时解析时xml无法失败&&,需要替换成为and,表示并且。

关联关系标签介绍

01<association>

// 该标签专门用来映射一对一类型关联关系,如每个人都拥有自己的身份标识,且仅仅为一对一,不会存在多种的可能

<resultMap id="orderMap" type="com.atguigu.entity.Order">

<id column="order_id" property="orderId"></id>

<result column="order_name" property="orderName"></result>

<result column="customer_id" property="customerId"></result>

<association property="customer" javaType="com.atguigu.entity.Customer">

<id column="customer_id" property="customerId"></id>

<result column="customer_name" property="customerName"></result>

</association>

</resultMap>

02<collection>

//该标签则能完成一对多的关联关系,多对多则可以理解为俩次处理一对多即可

<resultMap id="customerMap" type="com.atguigu.entity.Customer">

<id property="customerId" column="customer_id"></id>

<result property="customerName" column="customer_name"></result>

<collection property="orderList" ofType="com.atguigu.entity.Order">

<id property="orderId" column="order_id"></id>

<result property="orderName" column="order_name"></result>

</collection>

</resultMap>

小结

总之,在您学完MyBatis中动态拼接sql技能后,针对不同业务场景,灵活运用动态标签即可达到您想要的结果!

http://www.lryc.cn/news/115410.html

相关文章:

  • Java课题笔记~ 使用 Spring 的事务注解管理事务(掌握)
  • UML—浅谈常用九种图
  • 算法与数据结构-跳表
  • 微信小程序nodejs+vue+uniapp校运会高校运动会报名管理系统
  • varint原理 - 负数的编码和解码
  • 大学生口才培训需求分析
  • C++:合并集合(并查集)
  • 【LeetCode】数据结构题解(10)[有效的括号]
  • 5G用户逼近7亿,5G发展迈入下半场!
  • 分布式问题
  • 教雅川学缠论06-中枢
  • 如何调教让chatgpt读取自己的数据文件(保姆级图文教程)
  • React Native Camera的使用
  • 【Matlab】Elman神经网络遗传算法(Elman-GA)函数极值寻优——非线性函数求极值
  • @ControllerAdvice注解使用及原理探究 | 京东物流技术团队
  • Error: Design has unresolved cell reference
  • uni-app 封装api请求
  • SpringCloud实用篇1——eureka注册中心 Ribbon负载均衡原理 nacos注册中心
  • 【MySQL】sql字段约束
  • 森海塞尔为 CUPRA 首款纯电轿跑 SUV – CUPRA Tavascan 注入音频魅力
  • Java、Android 加解密、编码、压缩、解压缩、Hash
  • 11_Pulsar Adaptors适配器、kafka适配器、Spark适配器
  • jupyter文档转换成markdown
  • 日志框架及其使用方法
  • ZIG:理解未来编程语言的视角
  • 让三驾马车奔腾:华为如何推动空间智能化发展?
  • 2022年03月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • WIN大恒工业相机SDK开发
  • qt qml中各种Layout之间是如何对齐的?
  • Immutable.js 进行js的复制