mirror of
				https://github.com/FreeeBird/hotel.git
				synced 2025-10-31 20:44:52 +08:00 
			
		
		
		
	完成UserService、UserMapper编写,通过单元测试
This commit is contained in:
		| @@ -36,7 +36,7 @@ public class AdminController { | |||||||
|         else return 0; |         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; |         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 |     @Override | ||||||
|     public String toString() { |     public String toString() { | ||||||
|         return "User{" + |         return "User{" + | ||||||
|   | |||||||
| @@ -1,8 +1,11 @@ | |||||||
| package cn.mafangui.hotel.mapper; | package cn.mafangui.hotel.mapper; | ||||||
|  |  | ||||||
| import cn.mafangui.hotel.entity.User; | import cn.mafangui.hotel.entity.User; | ||||||
|  | import org.apache.ibatis.annotations.Param; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
|  |  | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
| @Component | @Component | ||||||
| public interface UserMapper { | public interface UserMapper { | ||||||
|     int deleteByPrimaryKey(Integer userId); |     int deleteByPrimaryKey(Integer userId); | ||||||
| @@ -16,4 +19,10 @@ public interface UserMapper { | |||||||
|     int updateByPrimaryKeySelective(User record); |     int updateByPrimaryKeySelective(User record); | ||||||
|  |  | ||||||
|     int updateByPrimaryKey(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 { | 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(); |     User selectByUsernameAndPassword(String username, String password); | ||||||
| // |  | ||||||
| //    List<User> findAll(); |     User selectByUsername(String username); | ||||||
| // |  | ||||||
| //    int updateProfile(User user); |     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 |     from user_info | ||||||
|     where user_id = #{userId,jdbcType=INTEGER} |     where user_id = #{userId,jdbcType=INTEGER} | ||||||
|   </select> |   </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 id="deleteByPrimaryKey" parameterType="java.lang.Integer"> | ||||||
|     delete from user_info |     delete from user_info | ||||||
|     where user_id = #{userId,jdbcType=INTEGER} |     where user_id = #{userId,jdbcType=INTEGER} | ||||||
| @@ -35,8 +46,8 @@ | |||||||
|       update_time) |       update_time) | ||||||
|     values (#{userId,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},  |     values (#{userId,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},  | ||||||
|       #{name,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},  |       #{name,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},  | ||||||
|       #{address,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},  |       #{address,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, now(), | ||||||
|       #{updateTime,jdbcType=TIMESTAMP}) |       now()) | ||||||
|   </insert> |   </insert> | ||||||
|   <insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.User"> |   <insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.User"> | ||||||
|     insert into user_info |     insert into user_info | ||||||
| @@ -68,12 +79,8 @@ | |||||||
|       <if test="idcard != null"> |       <if test="idcard != null"> | ||||||
|         idcard, |         idcard, | ||||||
|       </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="userId != null"> |       <if test="userId != null"> | ||||||
| @@ -103,12 +110,8 @@ | |||||||
|       <if test="idcard != null"> |       <if test="idcard != null"> | ||||||
|         #{idcard,jdbcType=VARCHAR}, |         #{idcard,jdbcType=VARCHAR}, | ||||||
|       </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.User"> |   <update id="updateByPrimaryKeySelective" parameterType="cn.mafangui.hotel.entity.User"> | ||||||
| @@ -138,12 +141,7 @@ | |||||||
|       <if test="idcard != null"> |       <if test="idcard != null"> | ||||||
|         idcard = #{idcard,jdbcType=VARCHAR}, |         idcard = #{idcard,jdbcType=VARCHAR}, | ||||||
|       </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 user_id = #{userId,jdbcType=INTEGER} |     where user_id = #{userId,jdbcType=INTEGER} | ||||||
|   </update> |   </update> | ||||||
| @@ -157,8 +155,7 @@ | |||||||
|       email = #{email,jdbcType=VARCHAR}, |       email = #{email,jdbcType=VARCHAR}, | ||||||
|       address = #{address,jdbcType=VARCHAR}, |       address = #{address,jdbcType=VARCHAR}, | ||||||
|       idcard = #{idcard,jdbcType=VARCHAR}, |       idcard = #{idcard,jdbcType=VARCHAR}, | ||||||
|       create_time = #{createTime,jdbcType=TIMESTAMP}, |       update_time = now() | ||||||
|       update_time = #{updateTime,jdbcType=TIMESTAMP} |  | ||||||
|     where user_id = #{userId,jdbcType=INTEGER} |     where user_id = #{userId,jdbcType=INTEGER} | ||||||
|   </update> |   </update> | ||||||
| </mapper> | </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); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user