目录
mybatis批量插入数据
mybatis批量更新数据
<update id="upadateData" parameterType="java.util.List">
<foreach collection="dataList" item="item" separator=";">
UPDATE tb_data
SET score = #{item.score}
WHERE data_id = #{item.dataId}
</foreach>
</update>
使用上面的批量查询,需要在配置文件的数据库连接位置的url中添加下面这个,不然MySQL会认为是你SQL语法错了
&allowMultiQueries=true
url: jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&allowMultiQueries=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
使用这种的好处就是可以【一次网络传输】就把所有的SQL语句提交到数据库了。这样就可以不用频繁的使用MySQL连接资源,但是如果一次性添加过多的SQL语句在里面,就会导致数据库系统卡顿。
也就是说如果是在大量数据的批量更新或者是插入,那么就需要在程序中把数据分为小批量的数据进行批量入库,否则一次性处理太多数据会导致服务器阻塞的。
使用allowMultiQueries(多语句查询)的缺点:容易导致SQL注入风险。
解决方式:
-
使用mybatis的#{}来传递参数。
-
限制用户权限,不要使用超级权限的用户在配置文件中去连接数据库。
-
避免在代码中拼接SQL,特别是对于用户输入的字符串,不要直接在代码中把这个拼接到SQL中。
mybatis批量插入数据
涉及到集合的批量处理,在mapper接口中,需要使用param来进行参数绑定,否则mybatis识别不了集合的参数。
xml标签中的collection元素名称必须与mapper接口中param绑定的参数一致,否则mybatis识别不了。
int batchInsertUser(@Param("userList") List<UserEntity> userList);
<insert id="batchInsertUser" parameterType="java.util.List">
INSERT INTO tb_user (name, user_id, age, state, create_time, update_time)
VALUES
<foreach collection="userList" item="user" separator=",">
(#{user.name}, #{user.userId}, #{user.age},
#{user.state}, #{user.createTime}, #{user.updateTime})
</foreach>
</insert>
注意:如果需要插入的数据很多,那么就需要对数据进行分批处理:在程序中使用代码把数据进行分批,然后再一批一批的进行插入。通过指针和循环实现。
使用in批量查询或者是批量删除
批量查询:
int seleteUsersByUserIdList(@Param("userIds") List<Long> userIds);
<select id="seleteUsersByUserIdList" resultType="int">
select count(*) from tb_user tu where tu.state = 1 and tu.user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
</select>
批量删除:
int deleteUser(@Param("userIds") List<Long> userIds);
<delete id="deleteUser" parameterType="java.util.List">
DELETE FROM tb_user WHERE user_id IN
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
</delete>
使用mybatis获取自增数据的id
使用 useGeneratedKeys="true" keyProperty="userId",第一个参数是告诉mybatis使用的是数据库自增主键,第二个参数是告诉mybatis把新增数据的主键映射到哪个字段,下面的案例是把新增数据的主键映射到返回对象中的userId属性。
<insert id="insertUser" parameterType="com.xxx.db.pojo.UserEntity" useGeneratedKeys="true" keyProperty="userId">
INSERT INTO tb_user (name,state,age,create_time, update_time)
VALUES (#{name},#{state} ,#{age}, #{createTime},#{updateTime})
</insert>
@Data
public class UserEntity implements Serializable {
private long userId;
private String name;
private boolean state;
private String age;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
新增的主键会被映射到 UserEntity 对象中的 userId属性。
代码中获取这个主键:
UserEntity userEntity = assembleUserEntity(userPo);
int row = 0;
try {
row = valueMapper.insertUser(userEntity);
} catch (DuplicateKeyException e) {
throw new MyBusinessException("用户已经存在,请更换一个");
}
if (row != 1) {
throw new MyBusinessException("新增用户失败");
}
// 这里获取的就是新增数据的自增主键
Long userId = userEntity.getUserId();
使用mybatis返回嵌套层级关系的数据
先根据业务定义好需要的map映射关系,案例如下:
第二层的数据需要使用collection标签中的property属性进行指定,这个属性值要与实体类中的属性对应起来。
<resultMap id="selectDataMap" type="com.xxx.vo.SelectDataVo">
<result property="userId" column="user_id"/>
<result property="name" column="name"/>
<collection property="testList" ofType="com.xxx.vo.SelectScoreByUserIdVo">
<result property="dataId" column="data_id"/>
<result property="score" column="score"/>
<result property="example" column="example"/>
</collection>
</resultMap>
java映射实体类如下:
@Data
public class SelectDataVo implements Serializable{
private String userId;
private String name;
//这里的实体类的属性要与mybatis中collection标签的property属性值一致
private List<SelectScoreByUserIdVo> testList;
}
@Data
public class SelectScoreByUserIdVo implements Serializable {
private Integer dataId;
private int score;
private String example;
}
对应的SQL查询语句:下面的SQL语句只是案例演示:最主要的是需要在sql标签中指定好刚刚定义好的Map映射。
<select id="selectData" resultMap="selectDataMap" parameterType="com.xxx.SelectxxxContent">
SELECT
te.user_id,
te.name,
te.data_id,
a.score,
a.example
FROM tb_example te
LEFT JOIN xxx on xxx = xxx
LEFT JOIN xxx a on xxx = xxx
WHERE xxx
and te.user_id = #{userId}
</select>