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

This commit is contained in:
freeebird 2018-11-12 16:19:23 +08:00
parent dbee98a4f0
commit 01475b6215
7 changed files with 178 additions and 39 deletions

View File

@ -3,7 +3,7 @@ package cn.mafangui.hotel.entity;
import java.util.Date; import java.util.Date;
public class RoomType { public class RoomType {
private Long typeId; private Integer typeId;
private String roomType; private String roomType;
@ -25,11 +25,11 @@ public class RoomType {
private Date updateTime; private Date updateTime;
public Long getTypeId() { public Integer getTypeId() {
return typeId; return typeId;
} }
public void setTypeId(Long typeId) { public void setTypeId(Integer typeId) {
this.typeId = typeId; this.typeId = typeId;
} }
@ -112,4 +112,21 @@ public class RoomType {
public void setUpdateTime(Date updateTime) { public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
@Override
public String toString() {
return "RoomType{" +
"typeId=" + typeId +
", roomType='" + roomType + '\'' +
", remark='" + remark + '\'' +
", price=" + price +
", discount=" + discount +
", area=" + area +
", bedNum=" + bedNum +
", bedSize='" + bedSize + '\'' +
", window=" + window +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
} }

View File

@ -1,17 +1,25 @@
package cn.mafangui.hotel.mapper; package cn.mafangui.hotel.mapper;
import cn.mafangui.hotel.entity.RoomType; import cn.mafangui.hotel.entity.RoomType;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface RoomTypeMapper { public interface RoomTypeMapper {
int deleteByPrimaryKey(Long typeId); int deleteByPrimaryKey(Integer typeId);
int insert(RoomType record); int insert(RoomType record);
int insertSelective(RoomType record); int insertSelective(RoomType record);
RoomType selectByPrimaryKey(Long typeId); RoomType selectByPrimaryKey(Integer typeId);
int updateByPrimaryKeySelective(RoomType record); int updateByPrimaryKeySelective(RoomType record);
int updateByPrimaryKey(RoomType record); int updateByPrimaryKey(RoomType record);
List<RoomType> selectAll();
RoomType selectByRoomType(String roomType);
} }

View File

@ -1,20 +1,20 @@
package cn.mafangui.hotel.service; package cn.mafangui.hotel.service;
import cn.mafangui.hotel.entity.RoomType; import cn.mafangui.hotel.entity.RoomType;
import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@Service
public interface RoomTypeService { public interface RoomTypeService {
int addRoomType(RoomType roomType); int insert(RoomType roomType);
int delRoomType(RoomType roomType); int delete(int typeId);
int delById(int typeId); int update(RoomType roomType);
int updateRoomType(RoomType roomType); RoomType selectByName(String roomType);
RoomType selectByName(RoomType roomType);
RoomType selectById(int typeId); RoomType selectById(int typeId);

View File

@ -0,0 +1,46 @@
package cn.mafangui.hotel.service.impl;
import cn.mafangui.hotel.entity.RoomType;
import cn.mafangui.hotel.mapper.RoomTypeMapper;
import cn.mafangui.hotel.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoomTypeServiceImpl implements RoomTypeService {
@Autowired
private RoomTypeMapper roomTypeMapper;
@Override
public int insert(RoomType roomType) {
return roomTypeMapper.insertSelective(roomType);
}
@Override
public int delete(int typeId) {
return roomTypeMapper.deleteByPrimaryKey(typeId);
}
@Override
public int update(RoomType roomType) {
return roomTypeMapper.updateByPrimaryKeySelective(roomType);
}
@Override
public RoomType selectByName(String roomType) {
return roomTypeMapper.selectByRoomType(roomType);
}
@Override
public RoomType selectById(int typeId) {
return roomTypeMapper.selectByPrimaryKey(typeId);
}
@Override
public List<RoomType> findAllType() {
return roomTypeMapper.selectAll();
}
}

View File

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mafangui.hotel.mapper.RoomTypeMapper"> <mapper namespace="cn.mafangui.hotel.mapper.RoomTypeMapper">
<resultMap id="BaseResultMap" type="cn.mafangui.hotel.entity.RoomType"> <resultMap id="BaseResultMap" type="cn.mafangui.hotel.entity.RoomType">
<id column="type_id" jdbcType="BIGINT" property="typeId" /> <id column="type_id" jdbcType="INTEGER" property="typeId" />
<result column="room_type" jdbcType="VARCHAR" property="roomType" /> <result column="room_type" jdbcType="VARCHAR" property="roomType" />
<result column="remark" jdbcType="VARCHAR" property="remark" /> <result column="remark" jdbcType="VARCHAR" property="remark" />
<result column="price" jdbcType="DOUBLE" property="price" /> <result column="price" jdbcType="DOUBLE" property="price" />
@ -18,25 +18,31 @@
type_id, room_type, remark, price, discount, area, bed_num, bed_size, window, create_time, type_id, room_type, remark, price, discount, area, bed_num, bed_size, window, create_time,
update_time update_time
</sql> </sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
from room_type from room_type
where type_id = #{typeId,jdbcType=BIGINT} where type_id = #{typeId,jdbcType=INTEGER}
</select> </select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> <select id="selectAll" resultMap="BaseResultMap">
select * from room_type
</select>
<select id="selectByRoomType" parameterType="String" resultMap="BaseResultMap">
select * from room_type where room_type = #{roomType,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from room_type delete from room_type
where type_id = #{typeId,jdbcType=BIGINT} where type_id = #{typeId,jdbcType=INTEGER}
</delete> </delete>
<insert id="insert" parameterType="cn.mafangui.hotel.entity.RoomType"> <insert id="insert" parameterType="cn.mafangui.hotel.entity.RoomType">
insert into room_type (type_id, room_type, remark, insert into room_type (type_id, room_type, remark,
price, discount, area, price, discount, area,
bed_num, bed_size, window, bed_num, bed_size, window,
create_time, update_time) create_time, update_time)
values (#{typeId,jdbcType=BIGINT}, #{roomType,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, values (#{typeId,jdbcType=INTEGER}, #{roomType,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR},
#{price,jdbcType=DOUBLE}, #{discount,jdbcType=DOUBLE}, #{area,jdbcType=INTEGER}, #{price,jdbcType=DOUBLE}, #{discount,jdbcType=DOUBLE}, #{area,jdbcType=INTEGER},
#{bedNum,jdbcType=INTEGER}, #{bedSize,jdbcType=VARCHAR}, #{window,jdbcType=INTEGER}, #{bedNum,jdbcType=INTEGER}, #{bedSize,jdbcType=VARCHAR}, #{window,jdbcType=INTEGER},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}) now(), now())
</insert> </insert>
<insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.RoomType"> <insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.RoomType">
insert into room_type insert into room_type
@ -68,16 +74,12 @@
<if test="window != null"> <if test="window != null">
window, window,
</if> </if>
<if test="createTime != null">
create_time, create_time,
</if>
<if test="updateTime != null">
update_time, update_time,
</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="typeId != null"> <if test="typeId != null">
#{typeId,jdbcType=BIGINT}, #{typeId,jdbcType=INTEGER},
</if> </if>
<if test="roomType != null"> <if test="roomType != null">
#{roomType,jdbcType=VARCHAR}, #{roomType,jdbcType=VARCHAR},
@ -103,12 +105,8 @@
<if test="window != null"> <if test="window != null">
#{window,jdbcType=INTEGER}, #{window,jdbcType=INTEGER},
</if> </if>
<if test="createTime != null"> now(),
#{createTime,jdbcType=TIMESTAMP}, now(),
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim> </trim>
</insert> </insert>
<update id="updateByPrimaryKeySelective" parameterType="cn.mafangui.hotel.entity.RoomType"> <update id="updateByPrimaryKeySelective" parameterType="cn.mafangui.hotel.entity.RoomType">
@ -138,14 +136,9 @@
<if test="window != null"> <if test="window != null">
window = #{window,jdbcType=INTEGER}, window = #{window,jdbcType=INTEGER},
</if> </if>
<if test="createTime != null"> update_time = now(),
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set> </set>
where type_id = #{typeId,jdbcType=BIGINT} where type_id = #{typeId,jdbcType=INTEGER}
</update> </update>
<update id="updateByPrimaryKey" parameterType="cn.mafangui.hotel.entity.RoomType"> <update id="updateByPrimaryKey" parameterType="cn.mafangui.hotel.entity.RoomType">
update room_type update room_type
@ -157,8 +150,7 @@
bed_num = #{bedNum,jdbcType=INTEGER}, bed_num = #{bedNum,jdbcType=INTEGER},
bed_size = #{bedSize,jdbcType=VARCHAR}, bed_size = #{bedSize,jdbcType=VARCHAR},
window = #{window,jdbcType=INTEGER}, window = #{window,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = now()
update_time = #{updateTime,jdbcType=TIMESTAMP} where type_id = #{typeId,jdbcType=INTEGER}
where type_id = #{typeId,jdbcType=BIGINT}
</update> </update>
</mapper> </mapper>

View File

@ -0,0 +1,76 @@
package cn.mafangui.hotel.service.impl;
import cn.mafangui.hotel.entity.RoomType;
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;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class RoomTypeServiceImplTest {
@Autowired
RoomTypeServiceImpl roomTypeService;
@Test
public void insert() {
String typeName = "big";
double price = 123;
double discount = 12;
int area = 10;
int bedNum = 1;
String bedSize = "15*12";
int window = 0;
RoomType roomType = new RoomType();
roomType.setArea(area);
roomType.setBedNum(bedNum);
roomType.setBedSize(bedSize);
roomType.setDiscount(discount);
roomType.setPrice(price);
roomType.setRoomType(typeName);
roomType.setWindow(window);
Assert.assertEquals(1,roomTypeService.insert(roomType));
}
@Test
public void delete() {
int id = 1;
Assert.assertEquals(1,roomTypeService.delete(id));
}
@Test
public void update() {
String typeName = "single";
int id = 1;
RoomType roomType = new RoomType();
roomType.setTypeId(id);
roomType.setRoomType(typeName);
Assert.assertEquals(1,roomTypeService.update(roomType));
}
@Test
public void selectByName() {
String typeName = "single";
int id = roomTypeService.selectByName(typeName).getTypeId();
System.out.println(roomTypeService.selectByName(typeName));
Assert.assertEquals(1,id);
}
@Test
public void selectById() {
int id = 1;
String typeName = roomTypeService.selectById(id).getRoomType();
System.out.println(roomTypeService.selectById(id));
Assert.assertEquals("single",typeName);
}
@Test
public void findAllType() {
System.out.println(roomTypeService.findAllType());
Assert.assertNotNull(roomTypeService.findAllType());
}
}