抛弃JSP,只做纯粹的前后端分离项目。
写在前面
学习基础知识是枯燥无味的,之所以会这样,多数是因为心不静,对于如何运用它,感到茫然。所以建议大家在学习Java基础知识的时候,一定要自己写实例,这样才能理解透彻,才能知道在以后的项目中如何运用所学到的知识。
正文
接上一篇《spring boot练习篇之用户登录系统【接口篇】》,此次将要接入本地MySQL数据库。至于本地如何安装MySQL数据库,小编会单独更新一篇文章讲解。
此项目使用的Spring + MyBatis。在开始写代码前,需要将项目所需的依赖包引进来。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
接下来配好数据库连接。在application.properties文件中配置。
#数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/rpm_sql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
首先在MySQL数据库中新建一个项目数据库【rpm_sql】,并新建一张用户表【table_users】。
use rpm_sql
create table table_users
(
userId int auto_increment
primary key,
name varchar(255) null,
password varchar(255) null
);
在项目中新建一个entity包,并在该包下新建一个UserEntity类。
package com.example.rpm.entity;
import lombok.Data;
@Data
public class UserEntity {
private int userId;
private String name;
private String password;
}
在项目中新建一个Mapper包,并在该包下新建一个UserMapper类。
package com.example.rpm.mapper;
import com.example.rpm.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM table_users WHERE name = #{name}")
UserEntity queryByName(@Param("name") String name);
}
知识点
@Mapper 是 Mybatis 的注解,和 Spring 没有关系;添加了@Mapper注解之后这个接口在编译时会生成相应的实现类;
接下来就是要将上一篇文章的项目工程完善一下。
修改业务逻辑层service的实现类UserServiceImpl。
package com.example.rpm.service;
import com.example.rpm.entity.UserEntity;
import com.example.rpm.mapper.UserMapper;
import com.example.rpm.result.ExceptionMsg;
import com.example.rpm.result.Response;
import com.example.rpm.result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Autowired
public void setUserMapper (UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public Response login(String name,String password) {
UserEntity userEntity = userMapper.queryByName(name);
if(userEntity == null) {
return new ResponseData(ExceptionMsg.FAILED);
}
String userDaoPassword = userEntity.getPassword();
if (password.equals(userDaoPassword)) {
return new ResponseData(ExceptionMsg.SUCCESS, userEntity);
}
return new ResponseData(ExceptionMsg.FAILED);
}
}
到此,项目已经成功接入数据库。
总结
在纯粹的前后端分离项目中,后端服务只负责提供相应的接口。页面跳转、数据展示效果以及部分逻辑都会交给前端来完成。
在使用spring的后端服务中,数据流程大致是controller负责接收用户输入的参数【即浏览器调用接口的入参】,在controller中引入service层,调用service层的方法,数据就进入service层,在service层进行业务逻辑处理【即数据的增、删、改、查】。在进行业务逻辑处理时,是以数据库的数据为凭证的,而在使用MyBatis的项目中,执行相应的SQL语句是在Mapper层,所以要在service层中引入Mapper层中的方法,这样就可以操作数据库了。而实体层【entity】以及封装的response类只是数据的载体,哪里需要,直接在那里引入使用即可。
原创文章,作者:ZERO,如若转载,请注明出处:https://www.edu24.cn/course/java/spring-boot-login-mysql.html