自动化测试二(登入)
controller 层
import com.choosefine.automatedtest.common.controller.BaseController; // controller层继承的 封装json import com.choosefine.automatedtest.cases.login.service.UserService; // service层 import io.swagger.annotations.ApiImplicitParam; //注解 import io.swagger.annotations.ApiImplicitParams; //注解 import io.swagger.annotations.ApiOperation; //注解 对应的CRUD import org.springframework.beans.factory.annotation.Autowired; //自动加载 import org.springframework.web.bind.annotation.GetMapping; // 查找 和 delectmapping import org.springframework.web.bind.annotation.PathVariable; //路径参数 import org.springframework.web.bind.annotation.RequestMapping; //获取路径 import org.springframework.web.bind.annotation.RestController; //controller注解 /** * Created by Administrator on 2017/7/13. */ @RestController @RequestMapping("/automated/login") //路径 public class UserController extends BaseController{ @Autowired UserService userService; @ApiOperation(value = "登录") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", name = "userName", dataType = "String", required = true, value = "登陆名"), @ApiImplicitParam(paramType = "query", name = "password", dataType = "String", required = true, value = "密码")}) @GetMapping("/{userName}/{password}") //查找 后面两个参数是对应下面userName password 用注解@pathVariable public String login(@PathVariable String userName,@PathVariable String password){ String result = userService.login(userName,password); //传到dao层 if(result.equals("error")){ //条件 如果错误就返回responseFail return responseFail("密码错误"); } if(result.equals("success")){ //否则返回成功 return responseSuccess(result); //把结果数据传输回来 } return responseFail(result); //传输失败 } }
service 层
import com.choosefine.automatedtest.cases.login.dao.UserDao; // dao import com.choosefine.automatedtest.cases.login.model.User; // model import org.springframework.beans.factory.annotation.Autowired; //自动加载 import org.springframework.data.mongodb.core.MongoTemplate; //mongoTemplate包装类的 import org.springframework.stereotype.Service; //service import java.util.List; /** * service接收dao层数据
* 条件判断1.为空 2.正确 */ @Service public class UserService { @Autowired UserDao userDao; public String login(String userName,String password){ User user = userDao.findUser(userName); if(user == null){ return "找不到该用户"; } if(user.getPassword().equals(password)){ return "success"; } return "error"; } }
dao 层
import com.choosefine.automatedtest.cases.login.model.User; import com.choosefine.automatedtest.common.dao.BaseMapper; //dao层依赖的baseMapper 继承了mapper 和sqlmapper import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; /** * select * from automated_user where user_name = 'zs' and is_valid = 1 整条数据放回user */ @Mapper public interface UserDao extends BaseMapper<User>{ @Select("select * from automated_user where user_name = #{userName} and is_valid = 1") User findUser(@Param("userName") String userName); }
BaseMapper<T>
import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; /** * Created by Administrator on 2017/7/14. */ public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> { //特别注意,该接口不能被扫描到,否则会出错 }
...