springboot+mybatis实现登录功能,返回json
2018-12-19 20:10
441 查看
1、新建maven项目(pom)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--自己起的--> <groupId>com.weixinone</groupId> <artifactId>ssmone</artifactId> <version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.BUILD-SNAPSHOT</version> </parent> <!-- Add typical dependencies for a web application --> <!--加入web模块--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--加入mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--gson--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
2、项目架构
3.源代码
3.1 Mapperer接口
@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); }
3.2 Excample类
@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); } } }
3.3Man类
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 + '\'' + '}'; } }
3.4MySQL
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');
4.运行
相关文章推荐
- springmvc+spring+mybatis实现用户登录功能(上)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- springmvc+mybatis+sql server实现简单登录功能【转】
- Spring MVC+mybatis实现注册登录功能
- springboot整合shiro登录失败次数限制功能的实现代码
- Spring+SpringMVC+MyBatis实现数据库连接的登录功能
- springmvc+mybatis+sql server实现简单登录功能【转】
- SpringBoot + SpringSecurity 短信验证码登录功能实现
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十二)返回JSON格式
- springmvc+mybatis+sql server实现简单登录功能【转】
- 搭建spring-boot+vue前后端分离框架并实现登录功能
- 一个简单的springboot的案例实现一个请求控制层返回一个JSON
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 用shiro+springboot+mybatis实现禁止账号重复登录
- spring+springmvc+mybatis+mysql实现登录功能(下)
- 微信小程序+SpringBoot+mybatis+MySQL实现简单的登录
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用springmvc+mybatis实现增删改查并进行登录注册功能