1、idea创建springboot项目(项目名:web)
1.1、删除spring-boot-starter依赖,新增spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、引入mysql驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3、整合mybatis、thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
4、实体bean
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
5、controller层代码
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/toLogin")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(User user, Model model){
Map user1 = userService.login(user);
model.addAttribute("user",user1);
return "index";
}
}
6、service层代码
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public Map login(User user){
return userMapper.login(user);
}
}
7、dao层
@Repository
@Mapper
public interface UserMapper {
Map login(User user);
}
8、mapper文件
<?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.wu.web.user.dao.UserMapper">
<select id="login" parameterType="User" resultType="Map">
select * from js_sys_user where user_code=#{name}
</select>
</mapper>
9、新建application.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/dataSource?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.wu.web.user.entity
mapper-locations: classpath:mappers/*.xml
10、前端页面
10.1登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login">
<p>账户:<input name="name"></p>
<p>密码:<input name="pwd"></p>
<p><button type="submit">提交</button> </p>
</form>
</body>
</html>
10.2、首页
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h5>index page</h5>
<p>账户:<span th:text="${user.get('user_name')}"></span></p>
<p>密码:<span th:text="${user.get('password')}"></span></p>
</body>
</html>
11、项目整体结构