mybatis mapper.xml獲取insert后的自增ID問題
mybatis mapper.xml獲取insert后的自增ID
在MyBatis中,要獲取執(zhí)行INSERT操作后的自增ID,可以在mapper.xml文件中的對(duì)應(yīng)<insert>標(biāo)簽中使用useGeneratedKeys屬性和keyProperty屬性。
以下是一個(gè)示例:
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
INSERT INTO users (username, email) VALUES (#{username}, #{email})
</insert>在這個(gè)例子中,假設(shè)users表有一個(gè)自增主鍵字段id。useGeneratedKeys設(shè)置為true表示我們希望獲取數(shù)據(jù)庫(kù)生成的鍵值,keyProperty設(shè)置為Java對(duì)象中的屬性名,MyBatis會(huì)將生成的ID設(shè)置到這個(gè)屬性中。
確保你的數(shù)據(jù)表設(shè)置了自增主鍵,并且你的實(shí)體類中有對(duì)應(yīng)的屬性。
例如:
public class User {
private Integer id;
private String username;
private String email;
// getters and setters
}在執(zhí)行insertUser操作后,MyBatis會(huì)將生成的ID自動(dòng)設(shè)置到傳入的User對(duì)象的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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏詳解
在項(xiàng)目中有些敏感信息不能直接展示,比如客戶手機(jī)號(hào)、身份證、車牌號(hào)等信息,展示時(shí)均需要進(jìn)行數(shù)據(jù)脫敏,防止泄露客戶隱私。本文將利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,需要的可以參考一下2023-03-03
Java編程實(shí)現(xiàn)的模擬行星運(yùn)動(dòng)示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)的模擬行星運(yùn)動(dòng),涉及java基于swing組建繪制動(dòng)態(tài)效果及數(shù)值運(yùn)算相關(guān)操作技巧,并總結(jié)分析了java面向?qū)ο蟮南嚓P(guān)特性,需要的朋友可以參考下2018-04-04
Springboot中靜態(tài)文件的兩種引入方式總結(jié)
這篇文章主要介紹了Springboot中靜態(tài)文件的兩種引入方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java嵌套for循環(huán)的幾種常見優(yōu)化方案
這篇文章主要給大家介紹了關(guān)于Java嵌套for循環(huán)的幾種常見優(yōu)化,在Java中優(yōu)化嵌套for循環(huán)可以通過以下幾種方式來提高性能和效率,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
java中\(zhòng)t,\n,\r,\b,\f 的作用及說明
這篇文章主要介紹了java中\(zhòng)t,\n,\r,\b,\f 的作用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

