完成RoomService类编写,通过单元测试

This commit is contained in:
freeebird 2018-11-12 16:43:47 +08:00
parent 01475b6215
commit 435e38198d
6 changed files with 171 additions and 29 deletions

View File

@ -102,4 +102,20 @@ public class Room {
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "Room{" +
"roomId=" + roomId +
", roomNumber='" + roomNumber + '\'' +
", typeId=" + typeId +
", roomType='" + roomType + '\'' +
", roomPrice=" + roomPrice +
", roomDiscount=" + roomDiscount +
", roomStatus=" + roomStatus +
", remark='" + remark + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}

View File

@ -1,7 +1,10 @@
package cn.mafangui.hotel.mapper;
import cn.mafangui.hotel.entity.Room;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface RoomMapper {
int deleteByPrimaryKey(Integer roomId);
@ -14,4 +17,10 @@ public interface RoomMapper {
int updateByPrimaryKeySelective(Room record);
int updateByPrimaryKey(Room record);
List<Room> selectByType(Integer typeId);
List<Room> selectByStatus(Integer roomStatus);
List<Room> selectAll();
}

View File

@ -6,13 +6,11 @@ import java.util.List;
public interface RoomService {
int addRoom(Room room);
int deleteRoom(int roomId);
int deleteRoom(String roomNumber);
int updateRoom(Room room);
Room findById(int roomId);
Room findByNumber(String roomNumber);
List<Room> findByStatus(String status);
List<Room> findByType(String typeName);
List<Room> findAll();
int insert(Room room);
int delete(int roomId);
int update(Room room);
Room selectById(int roomId);
List<Room> selectByStatus(int roomStatus);
List<Room> selectByType(int typeId);
List<Room> selectAll();
}

View File

@ -0,0 +1,50 @@
package cn.mafangui.hotel.service.impl;
import cn.mafangui.hotel.entity.Room;
import cn.mafangui.hotel.mapper.RoomMapper;
import cn.mafangui.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoomServiceImpl implements RoomService {
@Autowired
private RoomMapper roomMapper;
@Override
public int insert(Room room) {
return roomMapper.insertSelective(room);
}
@Override
public int delete(int roomId) {
return roomMapper.deleteByPrimaryKey(roomId);
}
@Override
public int update(Room room) {
return roomMapper.updateByPrimaryKeySelective(room);
}
@Override
public Room selectById(int roomId) {
return roomMapper.selectByPrimaryKey(roomId);
}
@Override
public List<Room> selectByStatus(int roomStatus) {
return roomMapper.selectByStatus(roomStatus);
}
@Override
public List<Room> selectByType(int typeId) {
return roomMapper.selectByType(typeId);
}
@Override
public List<Room> selectAll() {
return roomMapper.selectAll();
}
}

View File

@ -23,6 +23,17 @@
from room_info
where room_id = #{roomId,jdbcType=INTEGER}
</select>
<select id="selectByType" parameterType="Integer" resultMap="BaseResultMap">
select * from room_info
where type_id = #{typeId,jdbcType=INTEGER}
</select>
<select id="selectByStatus" parameterType="Integer" resultMap="BaseResultMap">
select * from room_info
where room_status = #{roomStatus,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select * from room_info
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from room_info
where room_id = #{roomId,jdbcType=INTEGER}
@ -34,8 +45,8 @@
update_time)
values (#{roomId,jdbcType=INTEGER}, #{roomNumber,jdbcType=VARCHAR}, #{typeId,jdbcType=INTEGER},
#{roomType,jdbcType=VARCHAR}, #{roomPrice,jdbcType=DOUBLE}, #{roomDiscount,jdbcType=DOUBLE},
#{roomStatus,jdbcType=INTEGER}, #{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
#{updateTime,jdbcType=TIMESTAMP})
#{roomStatus,jdbcType=INTEGER}, #{remark,jdbcType=VARCHAR}, now(),
now())
</insert>
<insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.Room">
insert into room_info
@ -64,12 +75,8 @@
<if test="remark != null">
remark,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="roomId != null">
@ -96,12 +103,8 @@
<if test="remark != null">
#{remark,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
now(),
now(),
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="cn.mafangui.hotel.entity.Room">
@ -128,12 +131,7 @@
<if test="remark != null">
remark = #{remark,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
update_time = now(),
</set>
where room_id = #{roomId,jdbcType=INTEGER}
</update>
@ -146,8 +144,7 @@
room_discount = #{roomDiscount,jdbcType=DOUBLE},
room_status = #{roomStatus,jdbcType=INTEGER},
remark = #{remark,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
update_time = now()
where room_id = #{roomId,jdbcType=INTEGER}
</update>
</mapper>

View File

@ -0,0 +1,72 @@
package cn.mafangui.hotel.service.impl;
import cn.mafangui.hotel.entity.Room;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class RoomServiceImplTest {
@Autowired
RoomServiceImpl roomService;
@Test
public void insert() {
Room room = new Room();
room.setRoomNumber("101");
room.setRoomPrice(123.0);
room.setRoomDiscount(12.1);
room.setRoomStatus(0);
room.setTypeId(1);
room.setRoomType("ade");
Assert.assertEquals(1,roomService.insert(room));
}
@Test
public void delete() {
Assert.assertEquals(1,roomService.delete(1));
}
@Test
public void update() {
Room room = new Room();
room.setRoomId(1);
room.setRoomNumber("222");
room.setRoomPrice(123.0);
room.setRoomDiscount(12.1);
room.setRoomStatus(0);
room.setTypeId(1);
room.setRoomType("ade");
Assert.assertEquals(1,roomService.update(room));
}
@Test
public void selectById() {
System.out.println(roomService.selectById(1));
Assert.assertEquals("222",roomService.selectById(1).getRoomNumber());
}
@Test
public void selectByStatus() {
System.out.println(roomService.selectByStatus(0));
Assert.assertEquals(1,roomService.selectByStatus(0).size());
}
@Test
public void selectByType() {
System.out.println(roomService.selectByType(1));
Assert.assertEquals(1,roomService.selectByType(1).size());
}
@Test
public void selectAll() {
System.out.println(roomService.selectAll());
Assert.assertEquals(1,roomService.selectAll().size());
}
}