springboot+mybatis实现登录功能,返回json

环境:win10教育版 + idea

1,新建maven项目

pom.xml

 


    4.0.0


    
    com.weixinone
    ssmone
    1.0-SNAPSHOT


    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.BUILD-SNAPSHOT
    

    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.google.code.gson
            gson
            2.8.2
        
    



    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
    
        
            spring-snapshots
            https://repo.spring.io/snapshot
            true
        
        
            spring-milestones
            https://repo.spring.io/milestone
        
    
    
        
            spring-snapshots
            https://repo.spring.io/snapshot
        
        
            spring-milestones
            https://repo.spring.io/milestone
        
    

 

 

 

 

 

 

 

 

2.上面的


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.BUILD-SNAPSHOT
    

是springboot的主要模板

 


        
            org.springframework.boot
            spring-boot-starter-web
        

加入web模块加入web容器,实现与HTTP服务器通信

 


            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

加入mybatis模板,实现变形

 


        
            mysql
            mysql-connector-java
        

加入mysql驱动

 

 
        
            com.google.code.gson
            gson
            2.8.2
        

加入gson,显示json返回

3.项目架构

springboot+mybatis实现登录功能,返回json_第1张图片

4.源代码

 

@Mapper
public interface ManMapper {
    @Select("SELECT user, password FROM man WHERE user = #{user}" +
            " AND password = #{password}")
    Man findByState(@Param("user") String username,
                    @Param("password") String password);
}

 

@RestController
//为 @RequestMapping("/")下面的denglu方法添加映射
@EnableAutoConfiguration
////自动配置应用
@SpringBootApplication
public class Example{

    private final ManMapper manMapper;
//    显示调用,构造器的作用
    public Example(ManMapper manMapper) {
        this.manMapper = manMapper;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

    @RequestMapping(value = "/denglu", method = RequestMethod.GET)
    @ResponseBody
    public String denglu(@RequestParam(value = "name", required = true) String name,
                         @RequestParam(value = "pwd", required = true) String pwd) throws Exception {


        Logger.getGlobal().info(pwd);
        Logger.getGlobal().info("结果"+this.manMapper.findByState(name, pwd));

        if (this.manMapper.findByState(name, pwd)!=null){
            String msg="登录成功";
            Gson gson = new Gson();
            return gson.toJson(msg);
        }
        else {
            String msg="登录失败";
            Gson gson = new Gson();
            return gson.toJson(msg);
        }
    }
}

 

public  class Man implements Serializable{
    private Long id;
    private String user;
    private String password;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Man{" +
                "id=" + id +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 

CREATE TABLE IF NOT EXISTS man  (
  id int(11) NOT NULL AUTO_INCREMENT,
  user varchar(255) NOT NULL,
  password varchar(255) NOT NULL,
  PRIMARY KEY (id)
)CHARACTER SET utf8;
INSERT INTO man VALUES (1, 'test', '123');

5.效果

http://localhost:8080/denglu?name=test&pwd=123

springboot+mybatis实现登录功能,返回json_第2张图片

你可能感兴趣的:(项目实战)