mirror of
https://github.com/FreeeBird/hotel.git
synced 2025-05-06 19:49:26 +08:00
完成UserService、UserMapper编写,通过单元测试
This commit is contained in:
parent
ed537bd926
commit
40d4e46f74
@ -36,7 +36,7 @@ public class AdminController {
|
||||
else return 0;
|
||||
}
|
||||
|
||||
|
||||
// TODO
|
||||
|
||||
|
||||
|
||||
|
@ -27,4 +27,5 @@ public class OperatorController {
|
||||
}
|
||||
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
package cn.mafangui.hotel.controller;
|
||||
|
||||
public class UserController {
|
||||
}
|
@ -113,6 +113,20 @@ public class User {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String username, String password, String name, String gender, String phone, String email, String address, String idcard) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.name = name;
|
||||
this.gender = gender;
|
||||
this.phone = phone;
|
||||
this.email = email;
|
||||
this.address = address;
|
||||
this.idcard = idcard;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
|
@ -1,8 +1,11 @@
|
||||
package cn.mafangui.hotel.mapper;
|
||||
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public interface UserMapper {
|
||||
int deleteByPrimaryKey(Integer userId);
|
||||
@ -16,4 +19,10 @@ public interface UserMapper {
|
||||
int updateByPrimaryKeySelective(User record);
|
||||
|
||||
int updateByPrimaryKey(User record);
|
||||
|
||||
User selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password);
|
||||
|
||||
User selectByUsername(String username);
|
||||
|
||||
List<User> selectAll();
|
||||
}
|
@ -7,18 +7,18 @@ import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
User selectById(int id);
|
||||
User selectById(int userId);
|
||||
|
||||
int register(User user);
|
||||
int insertUser(User user);
|
||||
|
||||
User login(String userName, String password);
|
||||
int deleteUser(int userId);
|
||||
|
||||
User selectByUserName(String userName);
|
||||
int updateUser(User user);
|
||||
|
||||
// int count();
|
||||
//
|
||||
// List<User> findAll();
|
||||
//
|
||||
// int updateProfile(User user);
|
||||
User selectByUsernameAndPassword(String username, String password);
|
||||
|
||||
User selectByUsername(String username);
|
||||
|
||||
List<User> selectAll();
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
package cn.mafangui.hotel.service.impl;
|
||||
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.mapper.UserMapper;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public User selectById(int userId) {
|
||||
return userMapper.selectByPrimaryKey(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int insertUser(User user) {
|
||||
return userMapper.insertSelective(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteUser(int userId) {
|
||||
return userMapper.deleteByPrimaryKey(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateUser(User user) {
|
||||
return userMapper.updateByPrimaryKeySelective(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User selectByUsernameAndPassword(String username, String password) {
|
||||
return userMapper.selectByUsernameAndPassword(username,password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User selectByUsername(String username) {
|
||||
return userMapper.selectByUsername(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> selectAll() {
|
||||
return userMapper.selectAll();
|
||||
}
|
||||
}
|
@ -24,6 +24,17 @@
|
||||
from user_info
|
||||
where user_id = #{userId,jdbcType=INTEGER}
|
||||
</select>
|
||||
<select id="selectAll" resultMap="BaseResultMap">
|
||||
select * from user_info
|
||||
</select>
|
||||
<select id="selectByUsernameAndPassword" parameterType="String" resultMap="BaseResultMap">
|
||||
select * from user_info
|
||||
where username = #{username,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}
|
||||
</select>
|
||||
<select id="selectByUsername" parameterType="String" resultMap="BaseResultMap">
|
||||
select * from user_info
|
||||
where username = #{username,jdbcType=VARCHAR}
|
||||
</select>
|
||||
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
|
||||
delete from user_info
|
||||
where user_id = #{userId,jdbcType=INTEGER}
|
||||
@ -35,8 +46,8 @@
|
||||
update_time)
|
||||
values (#{userId,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
|
||||
#{name,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
|
||||
#{address,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
|
||||
#{updateTime,jdbcType=TIMESTAMP})
|
||||
#{address,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, now(),
|
||||
now())
|
||||
</insert>
|
||||
<insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.User">
|
||||
insert into user_info
|
||||
@ -68,12 +79,8 @@
|
||||
<if test="idcard != null">
|
||||
idcard,
|
||||
</if>
|
||||
<if test="createTime != null">
|
||||
create_time,
|
||||
</if>
|
||||
<if test="updateTime != null">
|
||||
update_time,
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="userId != null">
|
||||
@ -103,12 +110,8 @@
|
||||
<if test="idcard != null">
|
||||
#{idcard,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.User">
|
||||
@ -138,12 +141,7 @@
|
||||
<if test="idcard != null">
|
||||
idcard = #{idcard,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 user_id = #{userId,jdbcType=INTEGER}
|
||||
</update>
|
||||
@ -157,8 +155,7 @@
|
||||
email = #{email,jdbcType=VARCHAR},
|
||||
address = #{address,jdbcType=VARCHAR},
|
||||
idcard = #{idcard,jdbcType=VARCHAR},
|
||||
create_time = #{createTime,jdbcType=TIMESTAMP},
|
||||
update_time = #{updateTime,jdbcType=TIMESTAMP}
|
||||
update_time = now()
|
||||
where user_id = #{userId,jdbcType=INTEGER}
|
||||
</update>
|
||||
</mapper>
|
@ -0,0 +1,69 @@
|
||||
package cn.mafangui.hotel.service.impl;
|
||||
|
||||
import cn.mafangui.hotel.entity.User;
|
||||
import cn.mafangui.hotel.service.UserService;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
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 UserServiceImplTest {
|
||||
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Test
|
||||
public void selectById() {
|
||||
String res = userService.selectById(1).getPassword();
|
||||
Assert.assertEquals("aaa",res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertUser() {
|
||||
User user = new User("nihao","nihao","你好","女",
|
||||
"18749834","1239@w.c","street2.02d","123213213");
|
||||
int res = userService.insertUser(user);
|
||||
Assert.assertEquals(1,res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUser() {
|
||||
Assert.assertEquals(1,userService.deleteUser(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUser() {
|
||||
User user = new User("nihao","123456","你好","女",
|
||||
"18749834","1239@w.c","street2.02d","123213213");
|
||||
user.setUserId(2);
|
||||
int res = userService.updateUser(user);
|
||||
Assert.assertEquals(1,res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectByUsernameAndPassword() {
|
||||
int res = userService.selectByUsernameAndPassword("nihao","123456").getUserId();
|
||||
Assert.assertEquals(2,res);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectByUsername() {
|
||||
int res = userService.selectByUsername("nihao").getUserId();
|
||||
Assert.assertEquals(2,res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectAll() {
|
||||
int res = userService.selectAll().size();
|
||||
Assert.assertEquals(1,res);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user