mybatis简单使用记录一下
mybatis官网:http://www.mybatis.org/mybatis-3/
参考博客:https://blog.csdn.net/iku5200/article/details/82856621
配置
application.properties
mybatis.mapper-locations:classpath:/mapping/*.xml mybatis.config-location:classpath:/mybatis-config.xml
model表(略)
dao层:
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface TestConfigMapper { List<TestConfigEntity> findAll(); int upsertBatch(List<TestConfigEntity> list); }
mapping/mapping.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.sensor.xxx.dao.TestConfigMapper"> <!-- 指向dao层 --> <select id="findAll" resultType="com.sensor.xxx.models.TestConfigEntity"> <!-- 返回一个列表,列表中的元素type是 entity对象 --> select * from vibration_config <!-- sql语句 --> </select> <insert id="upsertBatch" parameterType="java.util.List"> <!-- 批量操作foreach循环 --> insert into vibration_config (point_id, duration) values <foreach collection="list" item="item" index="index" separator=","> <!-- jdbcType 指定映射类型 --> (#{item.pointId,jdbcType=VARCHAR},#{item.duration,jdbcType=INTEGER}) <!-- 如果不写括号,可以用参数指定:open="(" separator="," close=")"-->
</foreach>
ON conflict(point_id) do update set
duration = excluded.duration
</insert>
</mapper>
第二个是批量 没有添加,有就更新(postgres)
====
批量删除
<delete id="deleteBatch" parameterType="java.util.List"> delete from vibration_alarm_realtime where point_id in <foreach collection="list" item="point_id" open="(" separator="," close=")"> #{point_id} </foreach> </delete> <delete id="deleteAll"> delete from vibration_alarm_realtime </delete>