添加对操作员的简单操作

This commit is contained in:
freeebird 2018-10-16 20:35:42 +08:00
parent 9c7af9d032
commit 931447aa39
7 changed files with 263 additions and 18 deletions

View File

@ -0,0 +1,54 @@
package cn.mafangui.hotel.controller;
import cn.mafangui.hotel.entity.Worker;
import cn.mafangui.hotel.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/worker")
public class WorkerController {
@Autowired
private WorkerService workerService;
@RequestMapping(method = RequestMethod.POST,value = "/login")
public int login(String userName,String password){
Worker worker = new Worker(userName,password);
if (workerService.login(worker) !=null ){
return 1;
}else {
return 0;
}
}
@RequestMapping(method = RequestMethod.POST,value = "/add")
public int addWorker(String userName,String password,String workerName,String phone,String email,String address){
Worker worker = new Worker(userName,password,workerName,phone,email,address);
return workerService.addWorker(worker);
}
@RequestMapping(method = RequestMethod.POST,value = "/del")
public int delWorker(String userName){
return workerService.delWorker(userName);
}
@RequestMapping(method = RequestMethod.POST,value = "/update")
public int updateWorker(String userName,String password,String workerName,String phone,String email,String address){
Worker worker = new Worker(userName,password,workerName,phone,email,address);
return workerService.updateWorker(worker);
}
@RequestMapping(value = "/query")
public Worker queryWorker(String userName){
return workerService.selectWorker(userName);
}
@RequestMapping(value = "/all")
public List<Worker> findAllWorkers(){
return workerService.findAllWorker();
}
}

View File

@ -92,4 +92,36 @@ public class Worker {
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Worker() {
}
public Worker(String userName, String password) {
this.userName = userName;
this.password = password;
}
public Worker(String userName, String password, String workerName, String phone, String email, String address) {
this.userName = userName;
this.password = password;
this.workerName = workerName;
this.phone = phone;
this.email = email;
this.address = address;
}
@Override
public String toString() {
return "Worker{" +
"workerId=" + workerId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", workerName='" + workerName + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}

View File

@ -1,7 +1,11 @@
package cn.mafangui.hotel.mapper;
import cn.mafangui.hotel.entity.Worker;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface WorkerMapper {
int deleteByPrimaryKey(Integer workerId);
@ -14,4 +18,10 @@ public interface WorkerMapper {
int updateByPrimaryKeySelective(Worker record);
int updateByPrimaryKey(Worker record);
int deleteByUserName(String userName);
int updateByUserNameSelective(Worker record);
Worker selectByUserName(String userName);
List<Worker> findAll();
Worker selectByUserNameAndPassword(Worker worker);
}

View File

@ -0,0 +1,15 @@
package cn.mafangui.hotel.service;
import cn.mafangui.hotel.entity.Worker;
import java.util.List;
public interface WorkerService {
int addWorker(Worker worker);
int delWorker(String userName);
int updateWorker(Worker worker);
Worker selectWorker(String userName);
List<Worker> findAllWorker();
Worker login(Worker worker);
}

View File

@ -0,0 +1,46 @@
package cn.mafangui.hotel.service.impl;
import cn.mafangui.hotel.entity.Worker;
import cn.mafangui.hotel.mapper.WorkerMapper;
import cn.mafangui.hotel.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WorkerServiceImpl implements WorkerService {
@Autowired
private WorkerMapper workerMapper;
@Override
public int addWorker(Worker worker) {
return workerMapper.insertSelective(worker);
}
@Override
public int delWorker(String userName) {
return workerMapper.deleteByUserName(userName);
}
@Override
public int updateWorker(Worker worker) {
return workerMapper.updateByUserNameSelective(worker);
}
@Override
public Worker selectWorker(String userName) {
return workerMapper.selectByUserName(userName);
}
@Override
public List<Worker> findAllWorker() {
return workerMapper.findAll();
}
@Override
public Worker login(Worker worker) {
return workerMapper.selectByUserNameAndPassword(worker);
}
}

View File

@ -27,11 +27,11 @@
where worker_id = #{workerId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="cn.mafangui.hotel.entity.Worker">
insert into worker (worker_id, user_name, password,
insert into worker (worker_id, user_name, password,
worker_name, phone, email,
address, create_time, update_time
)
values (#{workerId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
values (#{workerId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{workerName,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
@ -39,9 +39,6 @@
<insert id="insertSelective" parameterType="cn.mafangui.hotel.entity.Worker">
insert into worker
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="workerId != null">
worker_id,
</if>
<if test="userName != null">
user_name,
</if>
@ -60,17 +57,10 @@
<if test="address != null">
address,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="workerId != null">
#{workerId,jdbcType=INTEGER},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
@ -89,12 +79,8 @@
<if test="address != null">
#{address,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.Worker">
@ -139,4 +125,40 @@
update_time = #{updateTime,jdbcType=TIMESTAMP}
where worker_id = #{workerId,jdbcType=INTEGER}
</update>
<delete id="deleteByUserName" parameterType="String">
delete from worker
where user_name = #{userName,jdbcType=VARCHAR}
</delete>
<update id="updateByUserNameSelective">
update worker
set
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="workerName != null">
worker_name = #{workerName,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
update_time = now()
where user_name = #{userName,jdbcType=VARCHAR}
</update>
<select id="selectByUserName" parameterType="String" resultMap="BaseResultMap">
select * from worker
where user_name = #{userName,jdbcType=VARCHAR}
</select>
<select id="findAll" resultMap="BaseResultMap">
select * from worker
</select>
<select id="selectByUserNameAndPassword" resultMap="BaseResultMap">
select * from worker
where user_name = #{userName,jdbcType=VARCHAR} and password = #{password,jdbcType=VARCHAR}
</select>
</mapper>

View File

@ -0,0 +1,66 @@
package cn.mafangui.hotel.controller;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class WorkerControllerTest {
@Autowired
private WorkerController workerController;
@Test
public void login() {
String userName = "haha";
String pwd = "123456";
String password = "11111";
Assert.assertEquals(1,workerController.login(userName,password));
}
@Test
public void addWorker() {
String userName = "haha";
String password = "123456";
String workerName = "nidsahao";
String phone = "9834431";
String email = "fads@kjd";
String address = "street101";
Assert.assertEquals(1, workerController.addWorker(userName,password,workerName,phone,email,address));
}
@Test
public void delWorker() {
String userName = "haha";
Assert.assertEquals(1, workerController.delWorker(userName));
}
@Test
public void updateWorker() {
String userName = "haha";
String password = "11111";
String workerName = "chenhao";
String phone = null;
String email = null;
String address = null;
Assert.assertEquals(1, workerController.updateWorker(userName,password,workerName,phone,email,address));
}
@Test
public void queryWorker() {
String userName = "haha";
System.out.println(workerController.queryWorker(userName));
Assert.assertNotNull(workerController.queryWorker(userName));
}
@Test
public void findAllWorkers() {
Assert.assertNotNull(workerController.findAllWorkers());
}
}