1. 程式人生 > >對多表進行新增資料(獲取最後一條插入的資料的ID)

對多表進行新增資料(獲取最後一條插入的資料的ID)

有時我們會需要對多張表進行關聯,今天我需要用到多表新增。不知道怎麼弄但是想了下可以獲取到第一張表最後一次進行新增的資料,我只會兩種方法:一是獲取到最後一次進行新增的時間,二是獲取到最後進行新增的ID。我用的是第二種獲取到最後進行操作新增的ID。

先將一張表進行新增資料的操作,寫上查詢語句“select LAST_INSERT_ID()”獲取到最後一次新增的ID。獲取到ID後返回一個int型的值,

對映檔案:ordersMapper.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.car.dao.OrdersMapper">
<!-- 對user表進行插入,我這裡用的是動態sql,也可以用普通的sql語句 -->
	<insert
		id="insertUser"
		parameterType="com.car.bean.User">
		insert into user
		<trim
			prefix="("
			suffix=")"
			suffixOverrides=",">
			<if test="uName != null"> u_name,
			</if>
			<if test="uPhone != null"> u_phone,
			</if>
		</trim>
		<trim
			prefix="values ("
			suffix=")"
			suffixOverrides=",">
			<if test="uName != null"> #{uName,jdbcType=VARCHAR},
			</if>
			<if test="uPhone != null"> #{uPhone,jdbcType=VARCHAR},
			</if>
		</trim>

<!-- 獲取到user最後一次插入的資料 -->
		<selectKey keyProperty="uId" resultType="int"> 
		select LAST_INSERT_ID()
		</selectKey>
	</insert>
<!-- 獲取到user最後一次插入的資料,並返回一個int型的值 -->
<select id="selectLastId" resultType="int">
select LAST_INSERT_ID()
</select>
<!-- 對orders表進行插入操作 --><insertid="insertOrders"parameterType="com.car.bean.Orders">insert into orders<trimprefix="("suffix=")"suffixOverrides=","><if test="oMoney != null"> o_money,</if><if test="oPaytype != null"> o_payType,</if><if test="oStatus != null"> o_status,</if><if test="oDate != null"> o_date,</if><if test="oOther != null"> o_other,</if><if test="oServicetype != null"> o_serviceType,</if><if test="oServiceproto != null"> o_serviceProto,</if><if test="oShigong != null"> o_shigong,</if><if test="oXiaoshou != null"> o_xiaoshou,</if><if test="oShoping != null"> o_shoping,</if><!-- 這裡就是需要使用多表進行插入的ID --><if test="uId != null"> u_id,</if><if test="commentId != null"> comment_id,</if></trim><trimprefix="values ("suffix=")"suffixOverrides=","><if test="oMoney != null"> #{oMoney,jdbcType=FLOAT},</if><if test="oPaytype != null"> #{oPaytype,jdbcType=VARCHAR},</if><if test="oStatus != null"> #{oStatus,jdbcType=VARCHAR},</if><if test="oDate != null"> #{oDate,jdbcType=FLOAT},</if><if test="oOther != null"> #{oOther,jdbcType=VARCHAR},</if><if test="oServicetype != null"> #{oServicetype,jdbcType=VARCHAR},</if><if test="oServiceproto != null"> #{oServiceproto,jdbcType=VARCHAR},</if><if test="oShigong != null"> #{oShigong,jdbcType=VARCHAR},</if><if test="oXiaoshou != null"> #{oXiaoshou,jdbcType=VARCHAR},</if><if test="oShoping != null"> #{oShoping,jdbcType=VARCHAR},</if><if test="uId != null"> #{uId,jdbcType=INTEGER},</if><if test="commentId != null"> #{commentId,jdbcType=INTEGER},</if></trim></insert></mapper> dao層(介面層):ordersMapper.java
package com.car.dao;


import com.car.bean.Orders;
import com.car.bean.User;

public interface OrdersMapper {

    //對orders表進行插入的方法
    int insertOrders(Orders record); 
    
    //對user表進行插入的方法
    int insertUser(User user);

    //獲取最後一次對user表進行插入後的ID
    int selectLastId();
}
Service層:OrdersService.java
package com.car.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.car.bean.Orders;
import com.car.bean.User;
import com.car.dao.OrdersMapper;

@Service
public class OrdersService {
	@Autowired
	OrdersMapper ordersMapper;

//獲取user表最後一次進行插入的ID,返回一個int型
	public int selectLastId(){
		int id=ordersMapper.selectLastId();
		return id;
	}
	
//對user表進行新增
	public void insertUser(User user){
		ordersMapper.insertUser(user);
	}
	
//對order表進行新增
	public void insertOrders(Orders order){
		ordersMapper.insertOrders(order);
	}
	
}
Controller層:OrdersController
package com.car.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.car.bean.Orders;
import com.car.bean.User;
import com.car.service.OrdersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/orders")
public class OrdersController
{
	@Autowired
	OrdersService ordersService;

	@RequestMapping("/select")
	public String addTest(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model)
	{
		PageHelper.startPage(pn, 4);
		List<Orders> list = ordersService.select();
		PageInfo page = new PageInfo(list, 4);
		model.addAttribute("pageInfo", page);
		return "orders";
	}
	
//先對user表進行插入
	@RequestMapping("/insertUser")
	public String insertUser(User user,Orders order,Model model){
		ordersService.insertUser(user);
//上面對user表進行插入後獲取ID,返回int型
		int id=ordersService.selectLastId();
		order.setuId(id);
		return insertOrders(order,model);
	}
	
//最後對orders表進行插入
	@RequestMapping("/insertOrders")
	public String insertOrders(Orders orders,Model model){
		ordersService.insertOrders(orders);
		return addTest(1,model);
	}
}