一.创建Spring项目
出现报错的话,换一个jdk创建,一般java 8
选择Spring Web
测试一下
默认端口8080,但是Vue也需要8080,因此,后端设置一个端口3000
再次测试,成功
此处,如何kill一个端口占用
1.找到135端口的进程
netstat -ano | findstr "135"
2.杀死pid76号的进程
taskkill /pid 76 /f
二.Spring Boot + Mybatis整合开发
A:项目结构
B:具体流程实现
1.pom.xml中添加依赖
<!-- 1.实现对数据源的装配JDBC(必须) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>3.0.4</version>
</dependency>
<!-- 2.集成mybatis所有相关的(必须) -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--3.JDBC驱动(必须)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- 4.注解(必须) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
(可以省略)
<!--资源插件: 处理src/main/java目录中的xml-->
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
2.数据库相关
2.1先使用navcat创建创建一个数据库,Idea进行链接
2.2修改application.properties文件
修改为yml文件
server:
port: 3000
spring:
datasource:
username: root
password: XXXXX
url: jdbc:mysql://localhost:3306/dms?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
# mybatis sql语句文件[resources/mybatis下的xml文件]
mapper-locations: classpath:mybatis/*.xml
# 实体别名
type-aliases-package: com.example.test.pojo
#show Sql
logging:
level:
com.example.test: debug
3.解决跨域问题
package com.coursework.backend.config;
import jakarta.servlet.*;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class CorsConfig implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin");
if(origin!=null) {
response.setHeader("Access-Control-Allow-Origin", origin);
}
String headers = request.getHeader("Access-Control-Request-Headers");
if(headers!=null) {
response.setHeader("Access-Control-Allow-Headers", headers);
response.setHeader("Access-Control-Expose-Headers", headers);
}
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
4.各种层的实现+逻辑分析
1.controller:
@RestController
@RequestMapping("/login")
2.mapper
@Mapper
3.pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
4.service
无
5.serviceimpl
@Service
4.1 pojo层
package com.coursework.backend.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SystemAdminUser {
private Integer id ;
private String username;
private String password;
}
4.2 mapper层
package com.coursework.backend.mapper;
import com.coursework.backend.pojo.SystemAdminUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Mapper
public interface SystemAdminUserMapper {
SystemAdminUser systemAdminUser(@Param("myusername") String username,
@Param("mypassword") String password);
}
4.3 resource/mybatis层
<?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">
<mapper namespace="com.coursework.backend.mapper.SystemAdminUserMapper">
<!--
dao接口是一个简单类型的参数
mapper文件,获取这个参数值,使用#{任意字符}
-->
<select id="systemAdminUser" resultType="SystemAdminUser">
select * from systemadmin where username = #{myusername} and password = #{mypassword}
</select>
</mapper>
4.4 service层
package com.coursework.backend.service;
import java.util.Map;
public interface LoginService {
Map<String,String> login_systemadmin(String username, String password);
}
4.5 serviceimpl层
package com.coursework.backend.service.impl;
import com.coursework.backend.mapper.SystemAdminUserMapper;
import com.coursework.backend.pojo.SystemAdminUser;
import com.coursework.backend.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private SystemAdminUserMapper systemAdminUserMapper ;
//系统管理员
@Override
public Map<String, String> login_systemadmin(String username, String password)
{
Map<String,String> map = new HashMap<>();
SystemAdminUser sau = systemAdminUserMapper .systemAdminUser(username,password);
if(sau==null){
map.put("message","账号或密码错误");
} else {
map.put("message", "success");
}
return map;
}
}
4.6 controller层
package com.coursework.backend.controller;
import com.coursework.backend.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private LoginService loginService;
//系统用户登录验证
@RequestMapping("/systemadmin")
public Map<String,String> login(@RequestParam Map<String, String> map)
{
String username = map.get("username");
String password = map.get("password");
return loginService.login_systemadmin(username,password);
}
}