mybatis mapper.xml獲取insert后的自增ID問題
mybatis mapper.xml獲取insert后的自增ID
在MyBatis中,要獲取執(zhí)行INSERT操作后的自增ID,可以在mapper.xml文件中的對應<insert>標簽中使用useGeneratedKeys屬性和keyProperty屬性。
以下是一個示例:
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
INSERT INTO users (username, email) VALUES (#{username}, #{email})
</insert>在這個例子中,假設users表有一個自增主鍵字段id。useGeneratedKeys設置為true表示我們希望獲取數(shù)據(jù)庫生成的鍵值,keyProperty設置為Java對象中的屬性名,MyBatis會將生成的ID設置到這個屬性中。
確保你的數(shù)據(jù)表設置了自增主鍵,并且你的實體類中有對應的屬性。
例如:
public class User {
private Integer id;
private String username;
private String email;
// getters and setters
}在執(zhí)行insertUser操作后,MyBatis會將生成的ID自動設置到傳入的User對象的id屬性中。
mybatis mapper.xml常用寫法
resultMap寫法
<resultMap id="BaseResultVoMap" type="*.*.*.Entity" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="value" property="value" jdbcType="VARCHAR" />
<result column="date" property="date" jdbcType="DATE" />
<result column="time" property="time" jdbcType="TIMESTAMP" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="bool" property="bool" jdbcType="BOOLEAN" />
</resultMap>if 書寫
<if test=' value != null and value!= ""'>
value = #{value}
</if>foreach 書寫
<foreach collection="ids" item="item" open="(" separator=" , " close=")" index="index">
#{item}
</foreach>批量插入
<insert id="insert" parameterType="java.util.Map">
insert into table(id, value, date, time, status)
values
<foreach collection="list" item="entity" separator=",">
(
#{entity.id},
#{entity.value},
#{entity.date},
#{entity.time},
#{entity.status}
)
</foreach>
</insert>批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="lists" item="item" index="index" open="" close="" separator=";">
UPDATE table_name
<set>
create_time = #{item.createTime}
</set>
WHERE id = #{item.id}
</foreach>
</update>choose 書寫
<choose>
<when test=' time != null and time == "1" '>
table_${time}
</when>
<otherwise>
table_${date}
</otherwise>
</choose>大于小于
| <= | <= |
| >= | >= |
sql 書寫
<sql id="BaseColumn">
id, value, date, time, status
</sql>
<select id="selectByPidsAndQids" parameterType="java.util.Map" resultMap="BaseResultVoMap">
SELECT <include refid="BaseColumn"/>
FROM table
</select>resultType中接受Date數(shù)據(jù)類型
<select id="queryMaxDate" resultType="java.util.Date">
SELECT MAX(date) as maxDate from dual
</select>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java實現(xiàn)ReadWriteLock讀寫鎖的示例
ReadWriteLock是Java并發(fā)包中的接口,定義了讀鎖和寫鎖,讀鎖允許多線程同時訪問共享資源,而寫鎖則要求獨占,這種機制適用于讀多寫少的場景,可以提高并發(fā)效率同時保證數(shù)據(jù)一致性,本文就來詳細的介紹一下如何實現(xiàn),感興趣的可以了解一下2024-09-09
Java中將List拆分為多個小list集合的實現(xiàn)代碼
這篇文章主要介紹了Java中如何將List拆分為多個小list集合,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Spring?Boot?集成?Quartz并使用Cron?表達式實現(xiàn)定時任務
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進行定時任務調(diào)度,并通過?Cron?表達式?控制任務執(zhí)行時間,Quartz?提供了更強大的任務調(diào)度能力,比?@Scheduled?注解更靈活,適用于復雜的定時任務需求2025-04-04
詳談@Cacheable不起作用的原因:bean未序列化問題
這篇文章主要介紹了@Cacheable不起作用的原因:bean未序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java使用DateUtils對日期進行數(shù)學運算經(jīng)典應用示例【附DateUtils相關包文件下載】
這篇文章主要介紹了Java使用DateUtils對日期進行數(shù)學運算的方法,可實現(xiàn)針對日期時間的各種常見運算功能,并附帶DateUtils的相關包文件供讀者下載使用,需要的朋友可以參考下2017-11-11

