springboot整合mybatis
1. 整体架构展示:
2. pom.xml-需要的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
3. 新建application.yml文件
在resources目录下新建application.yml文件,用于存放数据库连接、服务端口配置等,也可以直接使用application.properties文件。
4. 新建数据库,名为learn(名字可以随意),application.yml文件中的数据库名与数据库中的一样就行
application.yml文件:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/learn?serverTimezone=GMT%2B8&useSSL=true
username: root
password: "123456"
创建数据库表:
create table userLogin(
username varchar(50) primary key,
password varchar(50)
);
5. 在pojo下的新建类UserLogin
package com.example.learn.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author zhaoxiang
* @date 2023/9/19
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserLogin {
private String username;
private String password;
}
6. mapper层(与数据库表字段进行映射)
创建UserLoginMapper接口
package com.example.learn.mapper;
import com.example.learn.pojo.UserLogin;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author zhaoxiang
* @date 2023/9/19
**/
@Mapper
@Repository
public interface UserLoginMapper {
//查询数据
@Select("select * from userLogin")
public List<UserLogin> queryAll();
//添加数据
@Insert("insert into userLogin values (#{username},#{password})")
public int add(UserLogin userLogin);
//根据用户名查询数据
@Select("select * from userLogin where username = #{username}")
public UserLogin queryByName(String username);
}
7. services层
在services包下创建建接口UserLoginServicesI和类UserLoginServicesImpl
- UserLoginServicesI接口:
package com.example.learn.services;
import com.example.learn.pojo.UserLogin;
import java.util.List;
/**
* @author zhaoxiang
* @date 2023/9/19
**/
public interface UserLoginServicesI {
//查询
public List<UserLogin> queryAll();
//添加数据
public int add(UserLogin userLogin);
//根据用户名查询数据
public UserLogin queryByName(String username);
}
- UserLoginServicesImpl类:
package com.example.learn.services;
import com.example.learn.mapper.UserLoginMapper;
import com.example.learn.pojo.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author zhaoxiang
* @date 2023/9/19
**/
@Service
public class UserLoginServicesImpl implements UserLoginServicesI {
@Autowired
UserLoginMapper userLoginMapper;
@Override
public List<UserLogin> queryAll() {
return userLoginMapper.queryAll();
}
@Override
public int add(UserLogin userLogin) {
return userLoginMapper.add(userLogin);
}
@Override
public UserLogin queryByName(String username) {
return userLoginMapper.queryByName(username);
}
}
8. conteoller层
创建UserLoginController类:
package com.example.learn.controller;
import com.example.learn.pojo.UserLogin;
import com.example.learn.services.UserLoginServicesImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author zhaoxiang
* @date 2023/9/19
**/
@Controller
public class UserLoginController {
@Autowired
UserLoginServicesImpl userLoginServicesImpl;
@RequestMapping("/toLogin")
public String toLogin(){
return "login";
}
@RequestMapping("/LoginSuccess")
public String LoginSuccess(Model model, UserLogin userLogin){
// 通过username查询username和password
UserLogin userLogin1 = userLoginServicesImpl.queryByName(userLogin.getUsername());
//先查询看该用户名是否存在
if (userLogin1!=null){// 如果查询的用户不为空
if(userLogin1.getPassword().equals(userLogin.getPassword())){ //判断密码是否正确
System.out.println(userLogin1.toString());
return "success";
}
else{
//返回到登录页面
model.addAttribute("data","用户名或密码错误");
return "login";
}
}
else{
//返回到登录页面
model.addAttribute("data","该用户不存在,请先注册");
return "login";
}
}
//登录界面
@RequestMapping("/toRegister")
public String toRegister(){
return "register";
}
@RequestMapping("/RegisterSuccess")
public String toRegisterSuccess(Model model,UserLogin userLogin){
//将账号密码加入到数据库中
int add = userLoginServicesImpl.add(userLogin);
System.out.println("数据插入成功!");
model.addAttribute("data","注册成功,请登录!");
return "login";
}
}
9. 创建前端页面(将这些页面放在template目录下)
- login.html:用户登录页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body style="background: #ffffff">
<div align="center">
<br><br><h2>登录界面</h2><br><br>
<span th:text="${data}" style="text-color:red;font-size: 10px"></span>
<form method="get" action="/LoginSuccess">
用户名:<input type="text" name="username" placeholder="请输入用户名" required/><br><br>
密码:<input type="text" name="password" placeholder="请输入密码" required/><br><br>
<input type="submit" value="登录">
</form>
<br>
<form method="get" action="/toRegister">
<input type="submit" value="注册">
</form>
</div>
</body>
</html>
- register.html:用户注册页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户注册页面</title>
</head>
<body style="background: #ffffff">
<div align="center">
<br><br>注册界面<br><br>
<form method="get" action="/RegisterSuccess">
用户名:<input type="text" name="username" placeholder="请输入用户名" required/><br><br>
密码:<input type="text" name="password" placeholder="请输入密码" required/><br><br>
确认密码:<input type="text" name="password2" placeholder="请输入密码" required/><br><br>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>
- success.html:登录成功界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户信息页面</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
10.效果展示
在搜索栏中输入 http://localhost:8080/toLogin
输入用户名和密码,点击登录,如果账号不存在,则显示用户名不存在,请先注册。如果数据库中存在该账号,则跳转到success页面。