SpringBoot + Mybatis 快速入门实战

一.创建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中添加依赖

  
  <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>

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);
    }
}

4.7前端请求即可

你可能感兴趣的:(spring,boot,mybatis,后端)