Day42(12)-F:\硕士阶段\Java\课程代码\后端\web-ai-code\web-ai-project02\tlias-web-management
每次重启之后都得重启一下nginx
启动监听
Mybatis
新增参数
package com.itheima.controller;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptSerive;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("/depts")
@RestController
public class DeptController {
@Autowired
private DeptSerive deptSerive;
@Autowired
private DeptMapper deptMapper;
//@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来
/**
* 查询部门
* @return
*/
@GetMapping//调用方式不一致: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
public Result delete(Integer id){
System.out.println("根据ID删除部门:"+id);//不传递参数的话,这个就是null
deptSerive.deleteById(id);
return Result.success();
}
/**
* 新增部门
* @return
*/
@PostMapping
public Result add(@RequestBody Dept dept){
System.out.println("新增部门:"+dept);
deptSerive.add(dept);
return Result.success();
}
/**
* 根据id查询部门
*/
@GetMapping("/{id}")
//public Result getInfo(@PathVariable("id") Integer deptId){
public Result getInfo(@PathVariable Integer id){
//形参名称与路径参数参数名字一致,@PathVariable("id")后面的("id")可以省略
System.out.println("根据id查询部门:"+id);
Dept info = deptSerive.getInfo(id);
return Result.success(info);
}
/**
* 修改部门
*/
@PutMapping
public Result update(@RequestBody Dept dept){
System.out.println("更新部门数据");
deptSerive.update(dept);
return Result.success();
}
}
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.time.LocalDateTime;
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);
}
@Override
public void add(Dept dept) {
//1.补全基础属性
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.add(dept);
}
@Override
public Dept getInfo(Integer id) {
Dept info = deptMapper.getInfo(id);
return info;
}
@Override
public void update(Dept dept) {
//补全基础属性
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
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);
/**
* 新增部门
* @param dept
*/
void add(Dept dept);
/**
* 跟着id查询
* @param id
*/
Dept getInfo(Integer id);
/**
* 修改部门
*/
void update(Dept dept);
}
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);
@Insert("insert into dept(name,create_time,update_time) values (#{name},#{createTime},#{updateTime})")
void add(Dept dept);
@Select("select id, name, create_time, update_time from dept where id =#{id}")
Dept getInfo(Integer id);
@Update("update dept set name = #{name}, update_time = #{updateTime} where id = #{id}")
void update(Dept dept);
}
日志技术
package com.itheima.controller;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptSerive;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j//Lombok提供的
@RequestMapping("/depts")
@RestController
public class DeptController {
//private static final Logger log = LoggerFactory.getLogger(DeptController.class);//固定的
@Autowired
private DeptSerive deptSerive;
@Autowired
private DeptMapper deptMapper;
//@RequestMapping(value = "/depts",method = RequestMethod.GET)//请求路径,就是浏览器要怎么搜出来
/**
* 查询部门
* @return
*/
@GetMapping//调用方式不一致:405
public Result List(){
//System.out.println("查询全部部门数据");
log.info("查询全部部门数据");
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
public Result delete(Integer id){
//System.out.println("根据ID删除部门:"+id);//不传递参数的话,这个就是null
log.info("根据ID删除部门:{}", id);
deptSerive.deleteById(id);
return Result.success();
}
/**
* 新增部门
* @return
*/
@PostMapping
public Result add(@RequestBody Dept dept){
//System.out.println("新增部门:"+dept);
log.info("新增部门:{}", dept);
deptSerive.add(dept);
return Result.success();
}
/**
* 根据id查询部门
*/
@GetMapping("/{id}")
//public Result getInfo(@PathVariable("id") Integer deptId){
public Result getInfo(@PathVariable Integer id){
//形参名称与路径参数参数名字一致,@PathVariable("id")后面的("id")可以省略
//System.out.println("根据id查询部门:"+id);
log.info("根据id查询部门:{}", id);
Dept info = deptSerive.getInfo(id);
return Result.success(info);
}
/**
* 修改部门
*/
@PutMapping
public Result update(@RequestBody Dept dept){
//System.out.println("更新部门数据");
log.info("更新部门数据:{}", dept);
deptSerive.update(dept);
return Result.success();
}
}
-- 添加外键约束(为emp表的dept_id -----> dept 的主键id)
alter table emp add constraint fk_emo_dept_id foreign key (dept_id) references dept(id);
alter table emp
drop foreign key fk_emo_dept_id;
alter table emp
drop foreign key fk_emo_dept_id;
alter table emp
add constraint fk_emp_dept_id
foreign key (dept_id) references dept (id);
CREATE TABLE dept (
id int unsigned PRIMARY KEY AUTO_INCREMENT COMMENT 'ID, 主键',
name varchar(10) NOT NULL UNIQUE COMMENT '部门名称',
create_time datetime DEFAULT NULL COMMENT '创建时间',
update_time datetime DEFAULT NULL COMMENT '修改时间'
) COMMENT '部门表';
INSERT INTO dept VALUES (1,'学工部','2023-09-25 09:47:40','2023-09-25 09:47:40'),
(2,'教研部','2023-09-25 09:47:40','2023-10-09 15:17:04'),
(3,'咨询部2','2023-09-25 09:47:40','2023-11-30 21:26:24'),
(4,'就业部','2023-09-25 09:47:40','2023-09-25 09:47:40'),
(5,'人事部','2023-09-25 09:47:40','2023-09-25 09:47:40'),
(15,'行政部','2023-11-30 20:56:37','2023-11-30 20:56:37');
create table emp(
id int unsigned primary key auto_increment comment 'ID,主键',
username varchar(20) not null unique comment '用户名',
password varchar(32) default '123456' comment '密码',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 1:男, 2:女',
phone char(11) not null unique comment '手机号',
job tinyint unsigned comment '职位, 1 班主任, 2 讲师 , 3 学工主管, 4 教研主管, 5 咨询师',
salary int unsigned comment '薪资',
image varchar(255) comment '头像',
entry_date date comment '入职日期',
dept_id int unsigned comment '部门ID',
create_time datetime comment '创建时间',
update_time datetime comment '修改时间'
) comment '员工表';
INSERT INTO emp VALUES
(1,'shinaian','123456','施耐庵',1,'13309090001',4,15000,'5.png','2000-01-01',2,'2023-10-20 16:35:33','2023-11-16 16:11:26'),
(2,'songjiang','123456','宋江',1,'13309090002',2,8600,'01.png','2015-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:35:37'),
(3,'lujunyi','123456','卢俊义',1,'13309090003',2,8900,'01.png','2008-05-01',2,'2023-10-20 16:35:33','2023-10-20 16:35:39'),
(4,'wuyong','123456','吴用',1,'13309090004',2,9200,'01.png','2007-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:35:41'),
(5,'gongsunsheng','123456','公孙胜',1,'13309090005',2,9500,'01.png','2012-12-05',2,'2023-10-20 16:35:33','2023-10-20 16:35:43'),
(6,'huosanniang','123456','扈三娘',2,'13309090006',3,6500,'01.png','2013-09-05',1,'2023-10-20 16:35:33','2023-10-20 16:35:45'),
(7,'chaijin','123456','柴进',1,'13309090007',1,4700,'01.png','2005-08-01',1,'2023-10-20 16:35:33','2023-10-20 16:35:47'),
(8,'likui','123456','李逵',1,'13309090008',1,4800,'01.png','2014-11-09',1,'2023-10-20 16:35:33','2023-10-20 16:35:49'),
(9,'wusong','123456','武松',1,'13309090009',1,4900,'01.png','2011-03-11',1,'2023-10-20 16:35:33','2023-10-20 16:35:51'),
(10,'linchong','123456','林冲',1,'13309090010',1,5000,'01.png','2013-09-05',1,'2023-10-20 16:35:33','2023-10-20 16:35:53'),
(11,'huyanzhuo','123456','呼延灼',1,'13309090011',2,9700,'01.png','2007-02-01',2,'2023-10-20 16:35:33','2023-10-20 16:35:55'),
(12,'xiaoliguang','123456','小李广',1,'13309090012',2,10000,'01.png','2008-08-18',2,'2023-10-20 16:35:33','2023-10-20 16:35:57'),
(13,'yangzhi','123456','杨志',1,'13309090013',1,5300,'01.png','2012-11-01',1,'2023-10-20 16:35:33','2023-10-20 16:35:59'),
(14,'shijin','123456','史进',1,'13309090014',2,10600,'01.png','2002-08-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:01'),
(15,'sunerniang','123456','孙二娘',2,'13309090015',2,10900,'01.png','2011-05-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:03'),
(16,'luzhishen','123456','鲁智深',1,'13309090016',2,9600,'01.png','2010-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:05'),
(17,'liying','12345678','李应',1,'13309090017',1,5800,'01.png','2015-03-21',1,'2023-10-20 16:35:33','2023-10-20 16:36:07'),
(18,'shiqian','123456','时迁',1,'13309090018',2,10200,'01.png','2015-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:09'),
(19,'gudasao','123456','顾大嫂',2,'13309090019',2,10500,'01.png','2008-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:11'),
(20,'ruanxiaoer','123456','阮小二',1,'13309090020',2,10800,'01.png','2018-01-01',2,'2023-10-20 16:35:33','2023-10-20 16:36:13'),
(21,'ruanxiaowu','123456','阮小五',1,'13309090021',5,5200,'01.png','2015-01-01',3,'2023-10-20 16:35:33','2023-10-20 16:36:15'),
(22,'ruanxiaoqi','123456','阮小七',1,'13309090022',5,5500,'01.png','2016-01-01',3,'2023-10-20 16:35:33','2023-10-20 16:36:17'),
(23,'ruanji','123456','阮籍',1,'13309090023',5,5800,'01.png','2012-01-01',3,'2023-10-20 16:35:33','2023-10-20 16:36:19'),
(24,'tongwei','123456','童威',1,'13309090024',5,5000,'01.png','2006-01-01',3,'2023-10-20 16:35:33','2023-10-20 16:36:21'),
(25,'tongmeng','123456','童猛',1,'13309090025',5,4800,'01.png','2002-01-01',3,'2023-10-20 16:35:33','2023-10-20 16:36:23'),
(26,'yanshun','123456','燕顺',1,'13309090026',5,5400,'01.png','2011-01-01',3,'2023-10-20 16:35:33','2023-11-08 22:12:46'),
(27,'lijun','123456','李俊',1,'13309090027',2,6600,'8.png','2004-01-01',2,'2023-10-20 16:35:33','2023-11-16 17:56:59'),
(28,'lizhong','123456','李忠',1,'13309090028',5,5000,'6.png','2007-01-01',3,'2023-10-20 16:35:33','2023-11-17 16:34:22'),
(30,'liyun','123456','李云',1,'13309090030',NULL,NULL,'01.png','2020-03-01',NULL,'2023-10-20 16:35:33','2023-10-20 16:36:31'),
(36,'linghuchong','123456','令狐冲',1,'18809091212',2,6800,'1.png','2023-10-19',2,'2023-10-20 20:44:54','2023-11-09 09:41:04');
-- 添加外键约束(为emp表的dept_id -----> dept 的主键id)
alter table emp add constraint fk_emo_dept_id foreign key (dept_id) references dept(id);
-- ===========================================一对一=====================================
create table tb_user(
id int unsigned primary key auto_increment comment 'ID',
name varchar(10) not null comment '姓名',
gender tinyint unsigned not null comment '性别, 1 男 2 女',
phone char(11) comment '手机号',
degree varchar(10) comment '学历'
) comment '用户信息表';
insert into tb_user values (1,'白眉鹰王',1,'18812340001','初中'),
(2,'青翼蝠王',1,'18812340002','大专'),
(3,'金毛狮王',1,'18812340003','初中'),
(4,'紫衫龙王',2,'18812340004','硕士');
create table tb_user_card(
id int unsigned primary key auto_increment comment 'ID',
nationality varchar(10) not null comment '民族',
birthday date not null comment '生日',
idcard char(18) not null comment '身份证号',
issued varchar(20) not null comment '签发机关',
expire_begin date not null comment '有效期限-开始',
expire_end date comment '有效期限-结束',
user_id int unsigned not null unique comment '用户ID',
constraint fk_user_id foreign key (user_id) references tb_user(id)
) comment '用户信息表';
insert into tb_user_card values (1,'汉','1960-11-06','100000100000100001','朝阳区公安局','2000-06-10',null,1),
(2,'汉','1971-11-06','100000100000100002','静安区公安局','2005-06-10','2025-06-10',2),
(3,'汉','1963-11-06','100000100000100003','昌平区公安局','2006-06-10',null,3),
(4,'回','1980-11-06','100000100000100004','海淀区公安局','2008-06-10','2028-06-10',4);
-- ======================================多对多=============================
create table tb_student(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '姓名',
no varchar(10) comment '学号'
) comment '学生表';
insert into tb_student(name, no) values ('黛绮丝', '2000100101'),('谢逊', '2000100102'),('殷天正', '2000100103'),('韦一笑', '2000100104');
create table tb_course(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '课程名称'
) comment '课程表';
insert into tb_course (name) values ('Java'), ('PHP'), ('MySQL') , ('Hadoop');
create table tb_student_course(
id int auto_increment comment '主键' primary key,
student_id int not null comment '学生ID',
course_id int not null comment '课程ID',
constraint fk_courseid foreign key (course_id) references tb_course (id),
constraint fk_studentid foreign key (student_id) references tb_student (id)
)comment '学生课程中间表';
insert into tb_student_course(student_id, course_id) values (1,1),(1,2),(1,3),(2,2),(2,3),(3,4);

浙公网安备 33010602011771号