黑马2023JavaWeb的B站视频,还可以,学的大部分都是有用的东西。没有一上来还JDBC。
新建项目、更改application.properties
配置:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/employee_manager
spring.datasource.username=root
spring.datasource.password=
# the log of mybatis
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
项目结构:
GET
路径:/depts
响应数据:
{
"code": 1,
"msg": "success",
"data":[
{"id":1,"name":"学工部","createTime":"2023-06-16T18:12:36","updateTime":"2023-06-16T18:12:36"},
{"id":2,"name":"教研部","createTime":"2023-06-16T18:12:36","updateTime":"2023-06-16T18:12:36"},
{"id":3,"name":"咨询部","createTime":"2023-06-16T18:12:36","updateTime":"2023-06-16T18:12:36"},
{"id":4,"name":"就业部","createTime":"2023-06-16T18:12:36","updateTime":"2023-06-16T18:12:36"},
{"id":5,"name":"人事部","createTime":"2023-06-16T18:12:36","updateTime":"2023-06-16T18:12:36"}]
}
依次编写Controller
、Service
和Mapper
。
启动前端程序,双击nginx.exe
即可。
端口为90:
DELETE
路径:/depts/{id}
根据ID删除部门数据
POST
路径:/depts
参数:json形式
{
"name": "教研部"
}
添加部门数据
数据库表中已经设置了主键 i d id id是自增的。
GET
路径:/depts/{id}
返回结果:
{
"code": 1,
"msg": "success",
"data": [
{
"id": 1,
"name": "计算机学院",
"createTime": "2022-09-01T23:06:29",
"updateTime": "2022-01-01T23:06:29"
}
]
}
请求路径:/depts
请求方式:PUT
参数:
{
"id": 1,
"name": "教研部"
}
更改编号id的部门名称为name
请求路径:/emps
请求方式:GET
参数:
【必须】page=1&pageSize=10
【可选】name=张&gender=1&begin=2007-09-01&end=2022-09-01
接口描述:该接口用于员工列表数据的条件分页查询,并返回总记录数
【有一说一我觉得这个接口设计得很烂,应该把两个功能拆开的,明明是独立的功能,但为了教学也可以理解】
请求示例
/emps?name=张&gender=1&begin=2007-09-01&end=2022-09-01&page=1&pageSize=10
返回示例:
{
"code": 1,
"msg": "success",
"data": {
"total": 2,
"rows": [
{
"id": 1,
"username": "jinyong",
"password": "123456",
"name": "金庸",
"gender": 1,
"image": "https://web-framework.oss-cn-hangzhou.aliyuncs.com/2022-09-02-00-27-53B.jpg",
"job": 2,
"entrydate": "2015-01-01",
"deptId": 2,
"createTime": "2022-09-01T23:06:30",
"updateTime": "2022-09-02T00:29:04"
},
{
"id": 2,
"username": "zhangwuji",
"password": "123456",
"name": "张无忌",
"gender": 1,
"image": "https://web-framework.oss-cn-hangzhou.aliyuncs.com/2022-09-02-00-27-53B.jpg",
"job": 2,
"entrydate": "2015-01-01",
"deptId": 2,
"createTime": "2022-09-01T23:06:30",
"updateTime": "2022-09-02T00:29:04"
}
]
}
}
分析:
# 从0开始查找5条数据
select * from emp limit 0, 5
# 查找第page页的数据
select * from emp limit (page - 1) * page, pageSize
获取总记录数:
select count(*) from emp
调用Mapper
的两个函数来实现功能。
【分页查询插件 PageHelper】
maven依赖:
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.4.2version>
dependency>
更改后:
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
EmpMapper empMapper;
@Override
public PageBean pageList(Integer page, Integer pageSize) {
// 1、设置分页参数
PageHelper.startPage(page, pageSize);
// 2、执行查询,封装为PageHelper中的page
List<Emp> list = empMapper.selectAll();
Page<Emp> pageHelper = (Page<Emp>) list;
// 3、封装PageBean对象
return new PageBean(pageHelper.getTotal(), pageHelper.getResult());
}
}
package com.koukou.manager.mapper;
import com.koukou.manager.model.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
/**
* 查询Emp表的所有记录
* @return
*/
@Select("select * from emp")
public List<Emp> selectAll();
}
注意这里的类型,不是LocalDateTime
:
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
/**
* @param page 页面
* @param pageSize 页面大小
* @param name 搜索关键字
* @param gender 性别
* @param begin 最早入职时间
* @param end 最晚入职时间
* @return Page{记录数, 本页员工列表}
*/
@GetMapping("/emps")
public Result pageList(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
String name, Short gender,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
log.info("按要求查询某页员工");
PageBean data = empService.pageList(page, pageSize, name, gender, begin, end);
return Result.success(data);
}
}
Service
先进行分页设置,然后按常规进行条件查询,封装为Page
,返回。
@Override
public PageBean pageList(Integer page, Integer pageSize,
String name, Short gender,
LocalDate begin, LocalDate end) {
// 1、设置分页参数
PageHelper.startPage(page, pageSize);
// 2、执行查询,封装为PageHelper中的page
List<Emp> list = empMapper.select(name, gender, begin, end);
Page<Emp> pageHelper = (Page<Emp>) list;
// 3、封装PageBean对象
return new PageBean(pageHelper.getTotal(), pageHelper.getResult());
}
Mapper,xml文件中<需要进行转义,SQL语句末尾没有分号:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.koukou.manager.mapper.EmpMapper">
<select id="select" resultType="com.koukou.manager.model.Emp">
select * from emp
<where>
<if test="name != null">name like concat('%', #{name}, '%')if>
<if test="gender != null">and gender = #{gender}if>
<if test="begin != null">and entrydate >= #{begin}if>
<if test="end != null">and entrydate <= #{end}if>
where>
order by update_time desc
select>
mapper>
DELETE
路径:/emps/{ids},参数为数组
根据ID批量删除员工
例子:/emps/1,2,3
接收数组参数:
@DeleteMapping("/emps/{ids}")
public Result delete(@PathVariable List<Integer> ids) {
empService.delete(ids);
return Result.success();
}
xml文件中删除,注意是#{id}
:
<delete id="delete">
delete from emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
foreach>
delete>