一、MyBatis 原生插入示例
实体类 User.java
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
Mapper 接口 UserMapper.java
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
// 单条插入
int insertUser(User user);
// 批量插入
int batchInsert(@Param("list") List<User> list);
}
UserMapper.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="com.xxx.mapper.UserMapper">
<!-- 单条插入 -->
<insert id="insertUser">
INSERT INTO user (name, age)
VALUES (#{name}, #{age})
</insert>
<!-- 批量插入 MySQL -->
<insert id="batchInsert">
INSERT INTO user (name, age)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.name}, #{item.age})
</foreach>
</insert>
</mapper>
使用:
// 单条
User user = new User();
user.setName("张三");
user.setAge(20);
userMapper.insertUser(user);
// 批量
List<User> list = new ArrayList<>();
list.add(new User(null,"王五",18));
list.add(new User(null,"赵六",25));
userMapper.batchInsert(list);
想要返回自增主键,加上
useGeneratedKeys="true" keyProperty="id"
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (name, age)
VALUES (#{name}, #{age})
</insert>
二、MyBatis-Plus 插入示例(重点,极简)
依赖(Maven)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
实体类(加 MP 注解)
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO) // 自增主键
private Long id;
private String name;
private Integer age;
}
Mapper 接口,继承 BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
// 不用写任何方法,自带 CRUD
}
Service 层(可选,推荐)
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
✅ MP 插入 API
// 1. 单条插入 mapper 方式
User user = new User();
user.setName("小红");
user.setAge(19);
userMapper.insert(user);
// user.getId() 自动回填自增id
// 2. 单条插入 service 方式
userService.save(user);
// 3. 批量插入 service(MP自带批量,底层循环单插,大数据量性能一般)
List<User> list = new ArrayList<>();
list.add(new User(null,"A",20));
list.add(new User(null,"B",22));
userService.saveBatch(list);
// 4. 批量插入指定批次大小,每批1000条
userService.saveBatch(list, 1000);
⚠️ 注意:
saveBatch默认不是一条 SQL 批量 VALUES,是循环多次 insert; 如果想要真正一条 SQL 批量,还是要自己写 xml foreach,和上面原生 MyBatis 一样。
三、扩展:MP 实现 ON DUPLICATE KEY UPDATE(存在更新,不存在插入)
方式 1:自定义 xml
<insert id="insertOrUpdate">
INSERT INTO user(id,name,age)
VALUES (#{id},#{name},#{age})
ON DUPLICATE KEY UPDATE
name = VALUES(name),
age = VALUES(age)
</insert>
方式 2:MP 3.5+ saveOrUpdate
// 根据主键判断:主键存在则update,不存在insert
userService.saveOrUpdate(user);
对比小结
本文原创作者:易君召,详见:https://www.yijunzhao.cc/about,转载请注明出处。
原文链接
欢迎访问