-
项目结构
-
MVC三层架构
/** * * 1.Controller从前端请求获取User数据 * 2.Controller传到UserService * 3.UserService传到最底层的Mapper提供数据接口,用于底层连接数据库,进行增删改查 * 4.其中sql语句一般写在resouce下的mapper文件夹xml文件里面 */
-
Bean文件夹下放实体类,一般一个类对应数据库中的一张表
目录
2.Controller文件夹 UserController类
5.Resouce目录下的添加一个mapper文件夹,在mapper文件夹添加一个Usermapping.xml文件用于写sql语句
8.ResultUtil用于编写Result实体类的返回函数
1.User类
package com.example.sport.Bean;
import lombok.Data;
@Data
public class User {
private int id;//编号
private String username;//用户名
private String password;//密码
private String email;//邮箱
private String role;//角色
private boolean state ;//状态
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
}
2.Controller文件夹 UserController类
package com.example.sport.controller;
import cn.hutool.core.util.StrUtil;
import com.example.sport.Bean.Result;
import com.example.sport.Bean.User;
import com.example.sport.Service.UserService;
import com.example.sport.Util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
/**
* @Autowired 从spring容器中拿UserService
* 1.Controller从前端请求获取User数据
* 2.Controller传到UserService
* 3.UserService传到最底层的Dao执行增删改查
*/
public class UserController {
@Autowired
UserService userService;
@PostMapping("/login")
public Result login(@RequestBody User user){
try {
// 判断输入的用户名和密码是否为空
if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
return ResultUtil.error(0, "出错");
}
System.out.println(user.getUsername()+user.getPassword());
return userService.LoginService(user);
} catch (Exception e) {
e.printStackTrace();
return ResultUtil.error(-1, "未知异常");
}
}
}
3.Service文件夹下放UserService类
package com.example.sport.Service;
import com.example.sport.Bean.Result;
import com.example.sport.Bean.User;
import com.example.sport.Mapper.UserMapper;
import com.example.sport.Util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
/**
* @Service 将UserService注册到spring容器中
*/
public class UserService {
// properties属性
@Autowired
UserMapper userMapper;
// method方法
public Result LoginService(User user) {
//查询用户是否存在
User LoginUser = userMapper.getUserByMessage(user.getUsername(),user.getPassword());
if (LoginUser == null)
return ResultUtil.success(0, "用户名或者密码错误");
else
return ResultUtil.success(1, "登录成功");
}
}
4.Mpper文件夹下添加一个数据接口UserMapper
package com.example.sport.Mapper;
import com.example.sport.Bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
/**
* 根据用户名和密码查询用户信息
*/
public User getUserByMessage(@Param("username") String username, @Param("password") String password);
}
5.Resouce目录下的添加一个mapper文件夹,在mapper文件夹添加一个Usermapping.xml文件用于写sql语句
<?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">
<!--
**在这里写数据库的增删改查语句**
1.mapper里面加一个命名空间,表示用到的接口
select ,update,delect,inser标签
2.select id选择该接口中的方法名 resultType结果集
-->
<mapper namespace="com.example.sport.Mapper.UserMapper">
<select id="getUserByMessage" resultType="com.example.sport.Bean.User">
SELECT * FROM easyuser WHERE username=#{username} AND password=#{password};
</select>
</mapper>
7.优化,用一个写Result实体类作为返回值
package com.example.sport.Bean;
import java.io.Serializable;
public class Result<T> implements Serializable {
//字段
private String msg;//message提示信息
private int code;//状态码
private T data;//数据
//属性getter和setter
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
8.ResultUtil用于编写Result实体类的返回函数
package com.example.sport.Util;
import com.example.sport.Bean.Result;
public class ResultUtil {
//properties
public static Result success(Object object) {
Result result = new Result();
result.setCode(0);
result.setMsg("成功");
result.setData(object);
return result;
}
public static Result success(Integer code,String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public static Result success(Integer code,String msg,Object object) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
result.setData(object);
return result;
}
public static Result success() {
return success(null);
}
public static Result error(Integer code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
9.跨域请求配置
package com.example.sport.Util;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
//全局配置类,配置跨域请求
@Configuration
@CrossOrigin
public class WebConfig implements WebMvcConfigurer {
@Override
// 解决跨域问题
public void addCorsMappings(CorsRegistry registry) {
/**
* 跨域访问配置
* 1.域名http://localhost:8080
* 2.请求来源类型null
* 3.方法
* 4.允许携带token
* 5.响应最大时间
* **/
registry.addMapping("/**")
.allowedOrigins("http://localhost:8080", "null")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}