CREATE TABLE `students` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT '',
`password` varchar(255) DEFAULT NULL,
`age` tinyint unsigned DEFAULT '0',
`height` decimal(5,2) DEFAULT NULL,
`gender` enum('男','女','中性','保密') DEFAULT '保密',
`class_id` int unsigned DEFAULT '0',
`is_delete` bit(1) DEFAULT b'0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1051 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String username;
private String password;
private int age;
private int height;
private String gender;
private int classId;
private boolean logicDelete;
}
@Repository
public interface UserMapper {
Student getInfo(@Param("name") String name, @Param("password") String password);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.practisesvr.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.practisesvr.model.Student">
SELECT *
FROM students
WHERE username = #{name} and password = #{password}
</select>
</mapper>
扫描 xml
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml #配置映射文件
扫描接口
@SpringBootApplication
@MapperScan("com.practisesvr.mapper")
public class PractisesvrApplication {
public static void main(String[] args) {
SpringApplication.run(PractisesvrApplication.class, args);
}
}
@Service
public interface UserService {
Student loginIn(String name, String password);
}
@Component
@Slf4j
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Student loginIn(String name, String password) {
log.info("input name {}, password {}", name, password);
if (ObjectUtils.anyNull(name, password)) {
log.warn("input name {}, password {}", name, password);
return null;
}
return userMapper.getInfo(name, password);
}
}
@Controller
@Slf4j
public class LoginController {
@Autowired
UserService userService;
// 为了返回登录页面
@RequestMapping("/login")
public String show() {
return "login";
}
@RequestMapping(value = "/loginIn", method = RequestMethod.POST)
public String login(String name, String password) {
Student student = userService.loginIn(name, password);
log.info("student {}", student);
if (student != null) {
return "success";
} else {
return "error";
}
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>logintitle>
head>
<body>
<form role="form" action="/loginIn" method="post">
账号:<input type="text" id="name" name="name"> <br>
密码:<input type="password" id="password" name="password"> <br>
<input type="submit" id="login" value="login">
form>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>successtitle>
head>
<body>
<h1>登录成功!h1>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>errortitle>
head>
<body>
<h1>登录失败!h1>
body>
html>
spring:
mvc:
view:
suffix: .html
static-path-pattern: /**
web:
resources:
static-locations: classpath:/templates/,classpath:/static/
spring:
datasource:
name: practisedb #数据库名
url: jdbc:mysql://localhost:3306/practisedb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
#url
username: root #用户名
password: 123456 #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.18version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
使用IDEA搭建一个简单的SpringBoot项目——详细过程