[Hundreds of wins] -Sanchuang competition mybati bulk add list seat information

Hello everyone, I am a pig arched by cabbage.
Purpose:
While creating a library study room, seats are automatically generated based on the rows and columns of the study room. If the rows and columns of the study room are 5,6, 30 seats are generated.
Here, the bulk addition of mybatis is used

The background mapper.xml part of the front page
Insert picture description here

<insert id="addSeats" parameterType="java.util.List">
		insert into seat(
			seatId, roomId, row, col, status
		)
		values
		<foreach collection="list" item="seat" index="index"
             separator=",">
        (
        #{seat.seatId},
        #{seat.roomId},
        #{seat.row},
        #{seat.col},
        #{seat.status}
        )
    	</foreach>
	</insert>

service part

/* 
	 * 添加自习室,并根据自习室的行与列批量添加座位
	 */
	public int addRoomAndSeats(Room room) {
		List<Seat> seatList=new ArrayList<>();
		
		int rows=room.getRows();
		int cols=room.getCols();
		
		for(int i=1;i<=rows;i++){
			for(int j=1;j<=cols;j++){
				Seat seat=new Seat();
				//确定座位的行数
				seat.setRow(i);
				seat.setCol(j);
				seat.setRoomId(room.getRoomId());
				seatList.add(seat);
			}
		}
		
		//添加自习室
		roomMapper.addRoom(room);
		return seatMapper.addSeats(seatList);
	}

This is just a word from my family, performance may be lacking, if there is a better way, please leave a message.

Published 24 original articles · praised 4 · visits 2038

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/105288577