实体类
public class UserRoom {
private Integer id;
private String userIp;
private Integer roomId;
private String roomName;
private LocalDateTime createTime;
}
首先是数据层接口
这里接口返回值为空就行,参数为自己的实体类,最后会将主键id赋给这个类
void insertUserRoom(UserRoom userRoom);
Mybatis xml 实现
useGeneratedKeys="true" keyProperty="id"
,这个id
为ChatRoom
实体类属性
<insert id="insertUserRoom" parameterType="UserRoom" useGeneratedKeys="true" keyProperty="id">
insert into user_room (userIp, roomId, createTime) VALUES (#{userIp}, #{roomId}, #{createTime})
</insert>
使用
调用chatRoomMapper.insertUserRoom(userRoom);
之后userRoom
的id
就是插入后的主键值
UserRoom userRoom = UserRoom.builder()
.userIp(ip)
.roomId(queryChatRoom.get(0).getRoomId())
.createTime(LocalDateTime.now())
.build();
chatRoomMapper.insertUserRoom(userRoom);
评论区