用到的工具:idea 2021、Maven 3.6.3、postman
框架:SpringBoot、Mybatis
数据库:Mysql8.0.30
安装&配置参考博文
注意:
1.下载maven注意idea与Maven版本的适配:
IDEA 2022 兼容maven 3.8.1及之前的所用版本
IDEA 2021 兼容maven 3.8.1及之前的所用版本
IDEA 2020 兼容Maven 3.6.3及之前所有版本
IDEA 2018 兼容Maven3.6.1及之前所有版本
2.为了避免每次创建项目都要改Maven配置,可以修改idea创建新项目的设置
mysql8安装参考博文
**注意:**连接不上往往是驱动的问题,把对应的驱动下载好即可
Hello
SpringBoot 简单启动页面
package com.susu.testsimplespringboot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @ClassName HelloController
* @Description TODO
* @Author susu
* @Date 2023/4/9 15:53
* @Version 1.0
*/
@Controller
public class HelloController {
@RequestMapping("/index")
public String sayHello() {
return "index";
}
}
application文件有两种文件格式,一种是以**.properties为后缀,一种是以.yml**为后缀的,两种配置方式略有差别,详情可参考:https://blog.csdn.net/qq_29648651/article/details/78503853;在这我是用.yml后缀的文件格式。右键application文件选择Refact,选择Rename,将后缀改为yml;
spring:
datasource:
name: test
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.susu.testsimplespringboot.DTO.UserLoginDTO
(1)在application配置文件中添加:数据库配置、MyBatis配置:
spring:
datasource:
name: test
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.susu.testsimplespringboot.DTO.UserLoginDTO
(2)pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.10
com.susu
testSimpleSpringBoot
0.0.1-SNAPSHOT
testSimpleSpringBoot
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.3.0
com.mysql
mysql-connector-j
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
(3) 实体类
UserLogin
package com.susu.testsimplespringboot.pojo;
/**
* @ClassName UserLogin
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:25
* @Version 1.0
*/
public class UserLogin {
private String userAccount;
private String userPwd;
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public String toString() {
return "UserLogin{" +
"userAccount='" + userAccount + '\'' +
", userPwd='" + userPwd + '\'' +
'}';
}
}
UserLoginDTO (本质也是实体类,这里使用的原因是:数据库三个字段,实体类pojo中只有两个,无法映射,所以根据数据表添加一个三个属性的实体类DTO接收查询结果)
package com.susu.testsimplespringboot.DTO;
/**
* @ClassName UserLoginDTO
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:49
* @Version 1.0
*/
public class UserLoginDTO {
private int userNo;
private String userAccount;
private String userPwd;
public int getUserNo() {
return userNo;
}
public void setUserNo(int userNo) {
this.userNo = userNo;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
(4)mapper接口
package com.susu.testsimplespringboot.mapper;
import com.susu.testsimplespringboot.DTO.UserLoginDTO;
import org.apache.ibatis.annotations.Mapper;
/**
* @ClassName UserMapper
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:27
* @Version 1.0
*/
@Mapper
public interface UserMapper {
public abstract UserLoginDTO selectUserInfo(String userAccount, String userPwd);
}
(5)mapper映射文件(这个文件可以设置成idea自动提示的模板,避免每次都要自己写,方法如下图)
namespace、id的属性值
namespace:mapper接口名;
id:mapper接口中抽象方法的名字
mybatis映射文件的一些技术点可参考下面的博文
https://blog.csdn.net/zxdspaopao/article/details/112919320
(6)service接口
package com.susu.testsimplespringboot.service;
import com.susu.testsimplespringboot.pojo.UserLogin;
/**
* @ClassName UserService
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:27
* @Version 1.0
*/
public interface UserService {
public abstract UserLogin loginCheck(String userAccount, String userPwd);
}
(6)service实现类
package com.susu.testsimplespringboot.serviceImpl;
import com.susu.testsimplespringboot.DTO.UserLoginDTO;
import com.susu.testsimplespringboot.service.UserService;
import com.susu.testsimplespringboot.mapper.UserMapper;
import com.susu.testsimplespringboot.pojo.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @ClassName UserServiceImpl
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:37
* @Version 1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public UserLogin loginCheck(String userAccount, String userPwd) {
UserLoginDTO userLoginDTO = userMapper.selectUserInfo(userAccount, userPwd);
UserLogin userLogin = new UserLogin();
if (userLoginDTO != null) {
userLogin.setUserAccount(userLoginDTO.getUserAccount());
userLogin.setUserPwd(userLoginDTO.getUserPwd());
}
return userLogin;
}
}
(7)到此时,可以在测试类测试下Mapper层是否可以正常获取数据
package com.susu.testsimplespringboot;
import com.susu.testsimplespringboot.pojo.UserLogin;
import com.susu.testsimplespringboot.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class TestSimpleSpringBootApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
UserLogin userLogin = userService.loginCheck("huahua", "123");
System.out.println(userLogin);
}
}
(8)controller
package com.susu.testsimplespringboot.controller;
import com.susu.testsimplespringboot.DTO.UserLoginDTO;
import com.susu.testsimplespringboot.pojo.UserLogin;
import com.susu.testsimplespringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @ClassName LoginController
* @Description TODO
* @Author susu
* @Date 2023/4/9 16:27
* @Version 1.0
*/
@Controller
//@ResponseBody
public class LoginController {
@Autowired
UserService userService;
/**
* @param testGetStaticSource
* @param index
* @return
*/
@RequestMapping("{testGetStaticSource}/{index}")
public String getStaticSource(@PathVariable String testGetStaticSource, @PathVariable String index) {
return testGetStaticSource + "/" + index;
}
@GetMapping(value = "login")
public String login() {
return "login";
}
@GetMapping(value = "loginIn")
public String loginGet(String userAccount, String userPwd) {
UserLogin user1 = userService.loginCheck(userAccount, userPwd);
if (user1 != null) {
return "success";
} else {
return "error";
}
}
@PostMapping(value = "loginIn")
public String loginPost(UserLoginDTO userLoginDTO) {
UserLogin user1 = userService.loginCheck(userLoginDTO.getUserAccount(), userLoginDTO.getUserPwd());
if (user1 != null) {
return "success";
} else {
return "error";
}
}
}
注意:
1.@RequestMapping,任何请求方式都可以
2.@GetMapping,只能Get请求(前端请求方式如下所示)
3.@PostMapping,只能Post请求
3.1 用postman发送post请求参考博文
3.2前端json格式报文:
{
"userAccount":"huahua",
"userPwd":"123"
}
4.测试了获取templates文件下的静态资源
@RequestMapping("{testGetStaticSource}/{index}")
public String getStaticSource(@PathVariable String testGetStaticSource, @PathVariable String index) {
return testGetStaticSource + "/" + index;
}
细节可以参考如下两篇博文
https://blog.csdn.net/zhuzicc/article/details/105465814#%E9%97%AE%E9%A2%98%E5%8E%9F%E5%9B%A0
https://blog.csdn.net/weixin_53106424/article/details/123502419
此处涉及springmvc的原理:
可以参考博文:
https://blog.csdn.net/weixin_53106424/article/details/115309018?spm=1001.2014.3001.5502
(9)几个静态文件
9.1 login.html
login
9.2 success.html
success
登录成功
9.3 error.html
eoor
登录失败