第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ssm項目往oracle數(shù)據(jù)庫插入數(shù)據(jù)時如何獲取該插入數(shù)據(jù)的主鍵?

ssm項目往oracle數(shù)據(jù)庫插入數(shù)據(jù)時如何獲取該插入數(shù)據(jù)的主鍵?

SSM
Qyouu 2018-11-08 15:11:50
<insert id="addUser" parameterType="user"><selectKey keyProperty="user_id" order="BEFORE" resultType="Integer">select tntall.nextval from dual </selectKey>insert into tnt_user(user_id,user_name) values(#{user_id},#{user_name})</insert>tntall 是序列的名字,序列每次增加1. 我這樣寫每次獲取的值和數(shù)據(jù)庫中真實的值都會有偏差??墒窃谶@操作之間,我沒有再往數(shù)據(jù)庫中其他表做過操作,也就是序列都沒變化。不過在數(shù)據(jù)庫中我原來有存在觸發(fā)器就是增加時取序列值作為該表主鍵。這種情況我要如何取到插入數(shù)據(jù)的主鍵。?請大神們指點下。謝謝
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經(jīng)驗 獲得超3個贊

Controller:

@RequestMapping("/saveEmployee")

public String saveEmployee(Employee employee) {

Employee e=new Employee();

e.setEmployee_name("張無忌");

e.setDep_id(1);

EmployeeDetail detail=new EmployeeDetail();

e.setEmployeeDetail(detail);

int r=employeeService.saveEmployee(e);

if(r>0) {


}

return null;

}

Service:

Service實現(xiàn)類:

@Transactional

public int saveEmployee(Employee e) {

int r=employeeMapper.saveEmployee(e);

EmployeeDetail d=e.getEmployeeDetail();

d.setId(e.getId());

return employeeDetailMapper.saveEmployeeDetail(d);

}

mapper文件:

<!-- 保存Employee對象 -->

<insert id="saveEmployee" parameterType="com.ssm.relation.pojo.Employee" useGeneratedKeys="true" keyProperty="id" keyColumn="id">

insert into employee(id,employee_name,dep_id) values(e_seq.nextval,#{employee_name},#{dep_id})

</insert>

在Service中開啟一個事務,執(zhí)行完int r=employeeMapper.saveEmployee(e);這一行以后這個對象的主鍵就已經(jīng)有值了,并且就是主鍵。因此e.getId()

就是往數(shù)據(jù)庫存以后的主鍵值



查看完整回答
反對 回復 2018-12-05
  • 1 回答
  • 0 關注
  • 1150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號