当前位置: 首页 > news >正文

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.DruidDataSourceurl: jdbc:mysql://localhost:3306/web01driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 1234
#配置Mybatis的相关配置
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-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 {@Autowiredprivate 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 {@Autowiredprivate DeptMapper deptMapper;@Overridepublic 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/tilasdriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 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 {@Autowiredprivate DeptSerive deptSerive;//@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来/*** 查询部门* @return*/@GetMapping("/depts")//调用方式不一致:405public 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);//不传递参数的话,这个就是nullreturn 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 {@Autowiredprivate DeptSerive deptSerive;//@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来/*** 查询部门* @return*/@GetMapping("/depts")//调用方式不一致:405public 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);//不传递参数的话,这个就是nulldeptSerive.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 {@Autowiredprivate DeptMapper deptMapper;@Overridepublic List<Dept> findAll() {return deptMapper.findAll();}@Overridepublic void deleteById(Integer id) {deptMapper.deteleById(id);}
}
http://www.jsqmd.com/news/43002/

相关文章:

  • vue2和vue3声明式和命令时的区别
  • WPS office 2023专业增强版 无限用v12.8 永久激活下载及安装使用教程
  • 3D Dynamic Scene Graph - MKT
  • 3D 文件类型,怎么在线查看编辑STL/AMF/OBJ/stp/fbx/ply转换
  • 022304105叶骋恺数据采集第三次作业
  • AI故事生成平台 -
  • nginx rewrite 状态码区别
  • GS4:首个泛化高斯溅射语义SLAM框架,十倍效率三维建图 - MKT
  • 2025 ICPC 南京区域赛 CFGIJ
  • 关于一种滚动数组的错误实现方式
  • React中Class组件和Function组件有何区别
  • 【数学】组合数学(更新中)
  • Metasfresh的历史
  • QQ流量分析
  • mac上如何用fvm设置全局Flutter SDK?
  • React面试/讨论中可能深入的问题
  • 20232404 2025-2026-1 《网络与系统攻防技术》实验六实验报告
  • 周作业 44
  • 白嫖AI的API中转服务MegaLLM–175刀免费额度教程
  • 周作业 43
  • 不是插件,这款公众号排版让你的文章颜值翻倍
  • 白嫖MegaLLM–175刀免费额度,使用各种AI模型
  • 二维前缀和与二维差分数组
  • 复合剩余问题
  • CF2165D Path Split 题解
  • gdb 安装linux
  • g for linux
  • 人工智能之编程基础 Python 入门:第十章 文件读写
  • 二分图的判定
  • 连续段 DP