1、系统目录结构
2、创建数据库
数据库名称:student,数据表:user,创建命令如下:
//创建数据库
create database student;
//选择创建好的数据库,建立user表
use student;
create table user(
id int primary key auto_increment,
username varchar(32) unique not null,
password varchar(32) not null
);
//想创建好的数据库中添加数据
insert into user values(1,"xioming","000");
//查看自己添加的数据
select * from user;
3、创建前端页面
3.1 登录页面:login.html
login
3.2 注册页面:regist.html
regist
4、Java代码
4.1 pom树
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
com.example
studentsys
0.0.1-SNAPSHOT
studentsys
Demo project for Spring Boot
11
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.projectlombok
lombok
com.alibaba
fastjson
1.2.73
org.springframework.boot
spring-boot-maven-plugin
4.2 数据配置信息:application.properties
#配置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#数据库连接池
#mapper映射
mybatis.type-aliases-package=com.example.studentsys.entiy
mybatis.mapper-locations=classpath:mapper/*.xml
4.3 实体类:User.java
package com.example.studentsys.entiy;
public class User {
private Integer id;
private String username;
private String password;
public void setId(Integer id){
this.id = id;
}
public Integer getId(){
return id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
}
4.4 mapper接口:UserMapper.java
package com.example.studentsys.mapper;
import com.example.studentsys.entiy.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//@Repository将类标识为bean
@Repository
@Mapper
public interface UserMapper {
List findAll();
User findByName(String name);
String findPswByName(String UserName);
void save(User user);
}
4.5 UserMapper.xml
insert into user(username,password) value (#{username},#{password})
4.6 service类:UserService.java
package com.example.studentsys.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.studentsys.entiy.User;
import com.example.studentsys.mapper.UserMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
@Service
public class UserService {
//该注解可以对类成员变量、方法以及构造函数进行标注,完成自动装配工作
@Autowired
UserMapper userMapper;
public String login(User user){
//登录逻辑函数
try{
User userExistN = userMapper.findByName(user.getUsername());
if(userExistN!=null){
String userExistP = userMapper.findPswByName(user.getUsername());
if(userExistP.equals(user.getPassword())){
return user.getUsername()+"登录成功,欢迎您!";
}else {
return "登录失败,密码错误!";
}
}else {
return "登录失败,用户不存在!";
}
}catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
public String regist(User user){
//注册逻辑函数
try {
User userExist = userMapper.findByName(user.getUsername());
if (user.getUsername().equals("")){
return "用户名不能为空";
}else if (user.getPassword().equals("")){
return "密码不能为空";
}else if (userExist!=null){
return "账户已经存在";
}else{
userMapper.save(user);
return "注册成功";
}
}catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
public List findAll(){
List list = userMapper.findAll();
return list;
}
}
4.7 控制类:UserController.java
package com.example.studentsys.controller;
import com.example.studentsys.entiy.User;
import com.example.studentsys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
@PostMapping("/login")
public String login(User user){
return userService.login(user);
}
@PostMapping("/regist")
public String regist(User user){
return userService.regist(user);
}
/**
* 解决查询数据库中文出现乱码问题
* @return
*/
@RequestMapping(value = "/alluser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public List findAll(){
return userService.findAll();
}
}
5、运行项目
http://localhost:8080/login.html 进入到登录页面
http://localhost:8080/regist.html 进入到注册页面
http://localhost:8080/user/alluser 查看当前所有用户信息
6、总结
MySQL常见管理命令
@RestController注解
业务逻辑:
Mapper负责连接数据并进行相关的CRUD操作
Service借助Mapper来完成登录注册的业务逻辑
Controller接待浏览器访问,借助Service提供登录注册服务