使用SpringBoot 尝试写个接口

第一步新建一个项目

image.png

image.png

image.png

第二步配置application.properties文件

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/frist?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=112358
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8000
#server.session.timeout=10
server.tomcat.uri-encoding=UTF-8


# mybatis.config= classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:Mapper/*.xml
# domain object's package
mybatis.typeAliasesPackage=com.lgp.SpringBoot.bean
# handler's package
# mybatis.typeHandlersPackage=
# check the mybatis configuration exists
# mybatis.check-config-location=
# mode of execution. Default is SIMPLE
# mybatis.executorType=

第三步:创建一张表

image.png

第四步:配置Mybatis-Generator自动生成实体类及dao类

下载:https://github.com/mybatis/generator/releases/

generatorConfig.xml文件配置




    
    
    
        
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        

使用命令

java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

成功生成三个文件


image.png

第四步:创建Vo类

package com.example.generator.Vo;

public class UserVo {
    private String user;

    private String pwd;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

第五步:创建control类

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

   @PostMapping("/register")
    public HashMap register (UserVo userVo){
       return loginService.insertSelective(userVo);
   }
   @GetMapping("/selectUser")
    public HashMap selectUser (String name){
       return loginService.selectUser(name);
   }
}

第六步:创建service类并编写具体逻辑方法

public interface LoginService {
    public HashMap insertSelective(UserVo userVo);
    public HashMap selectUser(String name);
}
@Service
public class LoginServiceimpl implements LoginService {
    @Resource
    private UserMapper userMapper;

    @Override
    public HashMap insertSelective(UserVo userVo) {
        HashMap messageMap = new HashMap();
        HashMap bodyMap = new HashMap();
        if ("".equals(userVo.getPwd()) || "".equals(userVo.getUser()) || null == userVo.getUser() || null == userVo.getPwd()) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "用户名或密码不能为空");
            messageMap.put("body", bodyMap);
        } else if (userMapper.selectByUser(userVo.getUser()).size() != 0) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "换个用户名");
            messageMap.put("body", bodyMap);
        } else if (userVo.getPwd().length() < 6) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "密码最小6位");
            messageMap.put("body", bodyMap);
        } else {
            try {
                User user = new User();
                user.setUser(userVo.getUser());
                user.setPwd(userVo.getPwd());
                userMapper.insertSelective(user);
                messageMap.put("status", "成功");
                bodyMap.put("result", userVo);
                messageMap.put("body", bodyMap);
            } catch (Exception e) {
                messageMap.put("status", "失败");
                bodyMap.put("result", "我也不晓得怎么回事");
                messageMap.put("body", bodyMap);
            }
        }

        return messageMap;
    }

    @Override
    public HashMap selectUser(String name) {
        HashMap messageMap = new HashMap();
        HashMap bodyMap = new HashMap();
        if (userMapper.selectByUser(name).size() != 0) {
            messageMap.put("status", "成功");
            bodyMap.put("result", "用户名已存在");
            messageMap.put("body", bodyMap);
        } else {
            messageMap.put("status", "成功");
            bodyMap.put("result", "用户名不存在");
            messageMap.put("body", bodyMap);
        }
        return messageMap;
    }
}

第七步:运行服务


image.png

image.png

image.png

你可能感兴趣的:(使用SpringBoot 尝试写个接口)