Day41(11)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project02\tlias-web-management

Mybatis 配置文件

image-20251117124148172

image-20251117124300043

spring:
  application:
    name: springboot-mybatis-quickstart
  #数据库的连接信息
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/web01
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 1234
#配置Mybatis的相关配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml

image-20251117131000963

Web进阶

image-20251117131316391

image-20251117132410043

image-20251117132503273

image-20251117133156436

image-20251117133256614

image-20251117133330624

image-20251117133826044

image-20251117133839249

image-20251117134921977

image-20251117134952390

image-20251117145030000

image-20251117145221810

新建项目步骤

开始工程搭建

每一个空项目创建好之后都需要去检查JDK版本

项目结构里面检查maven路径

image-20251117145441169

更改字符集

image-20251117145614125

安装依赖

image-20251117145745425

image-20251117145833767

创建,准备对应封装类和架构

image-20251117151937380

接口开发(查询部门)

image-20251117153348408

第77集 框架构建开始

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptSerive deptSerive;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来
    @GetMapping("/depts")
    public Result List(){
        System.out.println("查询全部部门数据");
        List<Dept> deptList = deptSerive.findAll();
        return Result.success(deptList);
    }
}
package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptSerive;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceimpl implements DeptSerive {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }
}
package com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

public interface DeptSerive {
    /**
     * 查询所有的部门数据
     * @return
     */
    List<Dept> findAll();
}
package com.itheima.mapper;


import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface DeptMapper {
    /**
     * 查询所有的部门数据
     * @return
     */
    @Select("select id, name, create_time, update_time from dept order by update_time desc ;")
    List<Dept> findAll();
}

下面是实体类

package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    private Integer id;
    private String name;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}
package com.itheima.pojo;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 */
@Data
public class Result {

    private Integer code; //编码:1成功,0为失败
    private String msg; //错误信息
    private Object data; //数据

    public static Result success() {
        Result result = new Result();
        result.code = 1;
        result.msg = "success";
        return result;
    }

    public static Result success(Object object) {
        Result result = new Result();
        result.data = object;
        result.code = 1;
        result.msg = "success";
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

}

image-20251117160238763

image-20251117160600509

image-20251117162450625

(推荐)

spring:
  application:
    name: tilas-web-management
  #数据库的连接信息
  datasource:
    url: jdbc:mysql://localhost:3306/tilas
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 1234
#配置Mybatis的相关配置
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #配置映射,开启了驼峰命名映射的开关
    map-underscore-to-camel-case: true
package com.itheima.mapper;


import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface DeptMapper {
    /**
     * 查询所有的部门数据
     * @return
     */
    //方式一:手动结果映射
//    @Results({
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
    //方式二:起别名
    //@Select("select id, name, create_time createTime, update_time updateTime from dept order by update_time desc ;")
    @Select("select id, name, create_time , update_time  from dept order by update_time desc ;")
    List<Dept> findAll();
}

前后端联调

image-20251117164254032

image-20251117170121429

location ^~ /api/ {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://localhost:8080;
     }

1. location ^~ /api/

  • ^~ 是 Nginx 的匹配优先级标记,表示 “当路径以 /api/ 开头时,优先匹配该 location,不再检查后续的正则 location(如 ~~* 开头的规则)”。
  • 作用:确保所有以 /api/ 开头的请求(如 http://localhost:90/api/depts)都会进入该配置块处理。

2. rewrite ^/api/(.*)$ /$1 break;

这是核心的正则重写规则,用于修改请求路径

  • 正则部分 ^/api/(.\*)$

    • ^ 表示匹配字符串的开头
    • /api/ 是固定匹配的路径前缀;
    • (.*)捕获组,表示 “匹配任意字符(除换行外)任意次数”,并将匹配到的内容保存为变量 $1
    • $表示匹配字符串的结尾整体含义:“匹配以/api/开头,后面跟着任意内容的路径”。
  • 替换部分 /$1

    表示将原路径替换为 “/ 加上捕获组 $1 中的内容”。

image-20251117170744390

image-20251117172231560

image-20251117173118621

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptSerive;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptSerive deptSerive;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来

    /**
     * 查询部门
     * @return
     */
    @GetMapping("/depts")//调用方式不一致:405
    public Result List(){
        System.out.println("查询全部部门数据");
        List<Dept> deptList = deptSerive.findAll();
        return Result.success(deptList);
    }

//    /**
//     * 删除部门-方式1:HttpServletRequest 获取请求参数
//     * @param request
//     * @return
//     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//        System.out.println("根据ID删除部门:"+id);
//        return Result.success();
//    }
//    /**
//     * 删除部门-方式2:@RequestParam
//     * 一旦我们声明了@RequestParam,该参数在请求是必须;如果不传递会报错,(默认required为true)
//     */
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam(value = "id",required = false) Integer deptId){
//        System.out.println("根据ID删除部门:"+deptId);//不传递参数的话,这个就是null
//        return Result.success();
//    }
    /**
     * 删除部门-方式3:省略@RequestParam(前端传递的请求参数名和服务端方法形参名字一致)
     */
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        System.out.println("根据ID删除部门:"+id);//不传递参数的话,这个就是null
        return Result.success();
    }
}

image-20251117173735728

image-20251117174920997

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptSerive;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptSerive deptSerive;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来

    /**
     * 查询部门
     * @return
     */
    @GetMapping("/depts")//调用方式不一致:405
    public Result List(){
        System.out.println("查询全部部门数据");
        List<Dept> deptList = deptSerive.findAll();
        return Result.success(deptList);
    }

//    /**
//     * 删除部门-方式1:HttpServletRequest 获取请求参数
//     * @param request
//     * @return
//     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//        System.out.println("根据ID删除部门:"+id);
//        return Result.success();
//    }
//    /**
//     * 删除部门-方式2:@RequestParam
//     * 一旦我们声明了@RequestParam,该参数在请求是必须;如果不传递会报错,(默认required为true)
//     */
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam(value = "id",required = false) Integer deptId){
//        System.out.println("根据ID删除部门:"+deptId);//不传递参数的话,这个就是null
//        return Result.success();
//    }
    /**
     * 删除部门-方式3:省略@RequestParam(前端传递的请求参数名和服务端方法形参名字一致)
     */
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        System.out.println("根据ID删除部门:"+id);//不传递参数的话,这个就是null
        deptSerive.deleteById(id);
        return Result.success();
    }
}
package com.itheima.mapper;


import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface DeptMapper {
    /**
     * 查询所有的部门数据
     * @return
     */
    //方式一:手动结果映射
//    @Results({
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime")
//    })
    //方式二:起别名
    //@Select("select id, name, create_time createTime, update_time updateTime from dept order by update_time desc ;")
    @Select("select id, name, create_time , update_time  from dept order by update_time desc ;")
    List<Dept> findAll();

    /**
     * 根据id删除部门
     * @param id
     */
    @Delete("delete from dept where id = #{id}")
    void deteleById(Integer id);
}
package com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

public interface DeptSerive {
    /**
     * 查询所有的部门数据
     * @return
     */
    List<Dept> findAll();

    /**
     * 根据id删除部门的方法
     * @param id
     */
    void deleteById(Integer id);
}
package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptSerive;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceimpl implements DeptSerive {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deteleById(id);
    }
}
posted @ 2025-11-17 20:39  David大胃  阅读(2)  评论(0)    收藏  举报