出现报错的话,换一个jdk创建,一般java 8
选择Spring Web
测试一下
默认端口8080,但是Vue也需要8080,因此,后端设置一个端口3000
再次测试,成功
此处,如何kill一个端口占用
1.找到135端口的进程
netstat -ano | findstr "135"
2.杀死pid76号的进程
taskkill /pid 76 /f
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
<version>3.0.4version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.30version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
(可以省略)
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
<include>**/*.ymlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
修改为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
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() {
}
}
1.controller:
@RestController
@RequestMapping("/login")
2.mapper
@Mapper
3.pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
4.service
无
5.serviceimpl
@Service
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;
}
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);
}
<?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>
package com.coursework.backend.service;
import java.util.Map;
public interface LoginService {
Map<String,String> login_systemadmin(String username, String password);
}
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;
}
}
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);
}
}