预约会议dao和mapper

// RoomMapper.java
package com.example.mapper;

import com.example.entity.Room;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface RoomMapper {
@Select("SELECT * FROM room WHERE id = #{id}")
Room findById(Long id);

@Select("SELECT * FROM room")
List<Room> findAll();
}

// UserMapper.java
package com.example.mapper;

import com.example.entity.User;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(Long id);
}

// ReservationMapper.java
package com.example.mapper;

import com.example.entity.Reservation;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ReservationMapper {

@Insert("INSERT INTO reservation (room_id, user_id, start_time, end_time, topic) VALUES (#{room.id}, #{user.id}, #{startTime}, #{endTime}, #{topic})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Reservation reservation);

@Update("UPDATE reservation SET room_id = #{room.id}, user_id = #{user.id}, start_time = #{startTime}, end_time = #{endTime}, topic = #{topic} WHERE id = #{id}")
void update(Reservation reservation);

@Delete("DELETE FROM reservation WHERE id = #{id}")
void delete(Long id);

@Select("SELECT * FROM reservation WHERE id = #{id}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "room", column = "room_id", one = @One(select = "com.example.mapper.RoomMapper.findById")),
@Result(property = "user", column = "user_id", one = @One(select = "com.example.mapper.UserMapper.findById")),
@Result(property = "startTime", column = "start_time"),
@Result(property = "endTime", column = "end_time"),
@Result(property = "topic", column = "topic")
})
Reservation findById(Long id);

@Select("SELECT * FROM reservation")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "room", column = "room_id", one = @One(select = "com.example.mapper.RoomMapper.findById")),
@Result(property = "user", column = "user_id", one = @One(select = "com.example.mapper.UserMapper.findById")),
@Result(property = "startTime", column = "start_time"),
@Result(property = "endTime", column = "end_time"),
@Result(property = "topic", column = "topic")
})
List<Reservation> findAll();

@Select("SELECT * FROM reservation WHERE room_id = #{roomId} AND start_time < #{endTime} AND end_time > #{startTime}")
List<Reservation> findOverlappingReservations(@Param("roomId") Long roomId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}

 

 

<?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.example.mapper.ReservationMapper">

<resultMap id="ReservationResultMap" type="com.example.entity.Reservation">
<id property="id" column="id" />
<association property="room" column="room_id" javaType="com.example.entity.Room" select="com.example.mapper.RoomMapper.findById" />
<association property="user" column="user_id" javaType="com.example.entity.User" select="com.example.mapper.UserMapper.findById" />
<result property="startTime" column="start_time" />
<result property="endTime" column="end_time" />
<result property="topic" column="topic" />
</resultMap>

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO reservation (room_id, user_id, start_time, end_time, topic)
VALUES (#{room.id}, #{user.id}, #{startTime}, #{endTime}, #{topic})
</insert>

<update id="update">
UPDATE reservation
SET room_id = #{room.id}, user_id = #{user.id}, start_time = #{startTime}, end_time = #{endTime}, topic = #{topic}
WHERE id = #{id}
</update>

<delete id="delete">
DELETE FROM reservation WHERE id = #{id}
</delete>

<select id="findById" resultMap="ReservationResultMap">
SELECT * FROM reservation WHERE id = #{id}
</select>

<select id="findAll" resultMap="ReservationResultMap">
SELECT * FROM reservation
</select>

<select id="findOverlappingReservations" resultType="com.example.entity.Reservation">
SELECT * FROM reservation WHERE room_id = #{roomId} AND start_time < #{endTime} AND end_time > #{startTime}
</select>

</mapper>

posted @ 2024-07-25 07:54  佬zz  阅读(30)  评论(0)    收藏  举报