当前位置: 首页 > 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.gsyq.cn/news/52426.html

相关文章:

  • nginx rewrite 状态码区别
  • QQ流量分析
  • React面试/讨论中可能深入的问题
  • CF2165D Path Split 题解
  • 连续段 DP
  • 人工智能之编程基础 Python 入门:第八章 函数与装饰器
  • 邻项交换
  • 2025-11-17 ZYZ28-NOIP模拟赛-Round7 hetao1733837的record
  • markdown格式绘制各种图
  • 计算机网络第六章---应用层(基于谢希仁老师第八版)
  • 第一次接触 JSAPIThree(百度地图 JSAPI Three)学习笔记
  • vulkan学习笔记第一篇_环境部署
  • 25.11.17随笔联考总结
  • web代码模板
  • 2025-11-17 早报新闻
  • V8的浏览器运行时环境
  • http https
  • 使用 LLM + Atlassian MCP 1小时生成年终总结
  • 25.11.17
  • 在线升级
  • javascript类型
  • ftp工具linux
  • 2025年东莞厂房装修公司最新榜单:聚焦仓储物流厂房装修/恒温恒湿厂房装修定制化解决方案
  • 执行上下文
  • 版本号
  • 13. 安全上下文
  • JavaScript手写函数
  • 2025 最新冷库建造厂家推荐!医药 / 食品 / 物流 / 小型 / 大型 / 自动化冷库建造厂家企业品牌权威排行榜
  • 2025年南京高功率密度电源公司推荐,高功率密度电源/电源模块/军用电源/全国产化电源/氢能源车载直流转换器生产直销有哪些
  • 2025 最新推荐!保定篮球俱乐部培训中心实力榜单:揭秘行业顶尖机构服务与教学优势权威指南