环境:win10教育版 + idea
1,新建maven项目
pom.xml
<?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.上面的
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.BUILD-SNAPSHOT</version>
</parent>
是springboot的主要模板
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
加入web模块加入web容器,实现与HTTP服务器通信
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
加入mybatis模板,实现变形
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
加入mysql驱动
<!--gson-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
加入gson,显示json返回
3.项目架构
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