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.執行