目前后端与数据库交互的框架技术繁多,但主流上使用的依然是 MyBatis
,通过 xml
配置将业务中的 SQL
语句剥离出来,从而降低模块间的耦合性和代码的可读性。只要你熟悉 Java
和 SQL
,那么 MyBatis
无疑是当下相对较优的解决方案。
在开发中经常业务相对较为复杂的需求,其中往往就是要涉及到多表联结,此时 MyBatis
的优势就更为明显。将复杂的 SQL
语句从代码中抽离出来,实现高可读、易维护等特点。
本文简单介绍 MyBatis 文件配置和基本 SQL 命令编写。
一、文件配置
1. 依赖导入
在集成使用上引入相应的 starter
即可,配置如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
需注意在 Spring Boot 3.x
版本中要求 mybatis
版本同样为 3.x
。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2. 基本信息
通过 mapper
标签的 namespace
实现与 Java
类的映射关联。
<?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">
<!-- namespace:mapper类完整路径 -->
<mapper namespace="com.budailad.mapper.UserMapper">
<!-- 结果映射 -->
<!-- id:唯一识别名称 -->
<!-- type:实体类完整路径 -->
<resultMap id="userResult" type="com.budailad.entity.User">
<id property="id" column="id" />
<result property="personName" column="person_name"/>
<result property="tel" column="tel"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,
person_name,
tel
</sql>
</mapper>
二、基本操作
1. 数据查询
通过 select
标签实现查询功能。
<select id="get" resultMap="userResult" parameterType="String">
select * from tb_user
where id = #{ id }
</select>
2. 数据新增
通过 insert
标签实现数据插入操作。
<insert id="add" parameterType="com.budailad.entity.User">
insert into tb_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="personName != null">
person_name,
</if>
<if test="tel != null">
tel,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{ id },
</if>
<if test="personName != null">
#{ personName },
</if>
<if test="tel != null">
#{ tel },
</if>
</trim>
</insert>
3. 数据更新
通过 update
标签实现数据更新操作。
<update id="update" parameterType="com.budailad.entity.User">
update tb_user
<set>
<if test="personName != null">
person_name = #{ personName },
</if>
<if test="tel != null">
tel = #{ tel },
</if>
</set>
where id = #{ id }
</update>
4. 数据删除
通过 delete
标签实现数据删除操作。
<delete id="delete">
delete from tb_user
where id = #{ id }
</delete>
三、批量操作
1. 批量新增
实现批量新增即基于 insert
原生语法,通过 foreach
标签拼接后插入。
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO tb_user (
<include refid="Base_Column_List"/>
) VALUES
<foreach collection="list" item="item" index="index" separator=",">
<trim prefix="(" suffix=")" suffixOverrides=",">
#{item.id}, #{item.personName}, #{item.tel},
</trim>
</foreach>
</insert>
2. 批量更新
针对批量更新操作实现机制并不复杂,同样通过 foreach
标签拼接多条语句后一次性提交,通过减少连接交互次数从而提高性能。
<update id="updateBatch" parameterType="java.util.List" >
<foreach collection="list" item="item" open="" close="" separator=";">
update tb_user
<set>
<if test="item.personName != null" >
person_name = #{item.personName,jdbcType=VARCHAR},
</if>
<if test="item.tel != null" >
tel = #{item.tel,jdbcType=VARCHAR},
</if>
</set>
where id = #{item.id,jdbcType=BIGINT}
</foreach>
</update>
特别注意的是若需支持批量提交 SQL
需在连接信息中添加 allowMultiQueries=true
参数允许多语句提交。
jdbc:mysql://127.0.0.1:3306/database?allowMultiQueries=true
3. 批量删除
在执行批量删除最常见的方式即通过主键等唯一字段拼接后提交,若数据集合中删除条件各不一致也可以采用上述批量更新拼接语句的方式的提交。
<delete id="deleteBatch">
delete from tb_user where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{ids}
</foreach>
</delete>