SpringBoot:实战项目TLIAS智能学习辅助系统1.1

SpringBootWeb项目

TILAS智能学习辅助系统

需求

部门管理

查询部门列表
删除部门
新增部门
修改部门

员工管理

查询员工列表(分页)
删除员工
新增员工
修改员工

准备工作

导入依赖

web(2.7.6)

mybatis

mysql驱动

lombok

准备好包结构

Controller->Service->Mapper->Pojo

controller

java 复制代码
@Controller
public interface DeptController{
}

@Controller
public interface EmpController {
}

mapper

java 复制代码
@Mapper
public interface DeptMapper {
}
@Mapper
public interface EmpMapper {
}

service/serviceImpl

java 复制代码
@Service
public interface DeptService {
}
@Service
public interface EmpService {
}
java 复制代码
@Slf4j
@Service
public class DeptServiceImpl {
}
@Slf4j
@Service
public class EmpServiceImpl {
}
创建数据库表和对应的实体类
java 复制代码
@Data
public class Dept {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Short gender;
    private String image;
    private Short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

@Data
public class Emp {
    private Integer id;
    private String name;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}
在配置文件中引入数据库连接
properties 复制代码
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.tlias.mybatis.entity

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Tlias_db
spring.datasource.username=root
spring.datasource.password=ljymysqlpwd
#开启日志
mybatis.configuration.logimpl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启字段到实体类的驼峰映射
mybatis.configuration.map-underscore-to-camel-case=true

开发规范

基于前后端分离模式进行开发

定义主流的REST风格API接口进行交互

REST:(Representational State Transfer)表现形式状态转换,一种软件架构风格
复制代码
url/aaa/1 GET:查询
url/aaa   POST:新增
url/aaa	  PUT:修改
url/aaa/1 DELETE

通过请求方式的不同进行不同操作

在@RequestMapping()中设置method = {RequestMethod.请求方式} RequestMethod是一个枚举类型

或者直接使用

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

开发流程

根据如下流程进行开发

查看页面原型明确需求

阅读接口文档

思路分析

接口开发:开发后台业务功能(功能按接口划分)

接口测试;通过Postman进行接口测试

前后端联调:和前端工程一起进行测试

功能实现

1,部门管理

查询部门

无需分页

请求路径:/depts

请求方式:GET

java 复制代码
//控制层
@GetMapping("/depts")
    Result getDept();

@Autowired
    DeptService deptService;
    @Override
    public Result getDept() {
        return Result.success(deptService.selectDept());
    }

//业务层
List<Dept> selectDept();

@Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> selectDept(){
        List<Dept> deptList = deptMapper.selectDept();
        return deptList;
    }

//持久层
@Select("select * from dept")
    List<Dept> selectDept();
删除部门

前端传递ID,后端删除对应数据

请求路径:/depts/{id}

请求方式:DELETE

java 复制代码
//控制层
//接收路径参数
@DeleteMapping("/depts/{id}")
    Result DeleteDept(@PathVariable Integer id);

@Autowired
    DeptService deptService;
    @Override
    public Result DeleteDept(Integer id) {
        deptService.deleteDept(id);
        return Result.success();
    }

//业务层
void deleteDept(Integer id);

@Autowired
    private DeptMapper deptMapper;

    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteDept(id);
    }

//持久层
@Delete("delete from dept where id = #{id}")
    void deleteDept(@Param("id") Integer id);
新增部门

前端输入部门名称,后端创建部门保存到数据库

请求路径:/depts

请求方式:POST

java 复制代码
//控制层
//接收JSON参数,使用@RequestBody进行映射
@PostMapping("/depts")
    Result PostDept(@RequestBody Dept dept);

@Autowired
    DeptService deptService;
   	@Override
    public Result PostDept(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return Result.success();
    }

//业务层
void insertDept(Dept dept);

@Autowired
    private DeptMapper deptMapper;
    @Override
    public void insertDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);
    }

//持久层
@Insert("insert into dept (id, name, create_time, update_time) values (null,#{name},#{createTime},#{updateTime})")
    void insertDept(Dept dept);
修改部门

先通过selectById进行数据回填,再通过updateDept进行数据修改

java 复制代码
//控制层
	@PutMapping("/depts")
    Result PutDept(@RequestBody Dept dept);

    @GetMapping("/depts/{id}")
    Result getDeptByID(@PathVariable Integer id);

@Autowired
    DeptService deptService;
   	@Override
    public Result PutDept(Dept dept) {
        deptService.putDept(dept);
        return Result.success();
    }

    @Override
    public Result getDeptByID(Integer id) {
        return Result.success(deptService.selectDeptByID(id));
    }

//业务层
	void putDept(Dept dept);

    Dept selectDeptByID(Integer id);

@Autowired
    private DeptMapper deptMapper;
    @Override
    public void putDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.putDept(dept);
    }

    @Override
    public Dept selectDeptByID(Integer id) {
        return deptMapper.selectDeptByID(id);
    }


//持久层
 //修改部门
    @Update("update dept set name = #{name} where id = #{id}")
    void putDept(Dept dept);

    //查询部门数据byID
    @Select("select * from dept where id = #{id}")
    Dept selectDeptByID(Integer id);

员工管理

分页查询

使用sql中的LIMIT语句

前端发送查询第几页,后端根据前端返回的页码进行计算对应显示的数据

参数传递:页码page,每页展示数pageSize

后端响应:当前页展示的数据,总共的记录数,封装到对象中以json格式数据进行响应回复

java 复制代码
//控制层
@GetMapping("/emps")
Result pageSelect(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer num);

@Override
public Result pageSelect(@RequestParam(defaultValue = "1") Integer 		page, @RequestParam(defaultValue = "10") Integer num) {
	PageBean bean = empService.selectPage(page,num);
	return Result.success(bean);
}

//服务层
PageBean selectPage(Integer page, Integer num);
//返回一个pagebean对象,封装了数据总条数和数据列表

@Override
    public PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer num) {
		List<Emp> empList = empMapper.selectEmp();
        List<Emp> empList = empMapper.selectEmi(name,gender,begin,end);
        return new pageBean(count,emplist);
        //在持久层中计算总数据条数
    }

//持久层
@Select("select * from emp limit #{index},#{num}")
    List<Emp> selectPage(@Param("index") Integer index, @Param("num") Integer num);
分页插件

PageHelper,Mybatis的一款分页插件,简化了在Mapper层手动分页的操作,直接使用列表查询,在Service层调用mapper方法设置分页参数.在查询之后解析分页结果,最后封装到PageBean对象中返回即可.

仅在服务层中不同,可以

java 复制代码
//服务层
PageBean selectPage(Integer page, Integer num);
//返回一个pagebean对象,封装了数据总条数和数据列表

@Override
    public PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer num) {
        PageHelper.startPage(page,num);
//        List<Emp> empList = empMapper.selectEmp();
        List<Emp> empList = empMapper.selectEmi(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
条件分页查询

使用动态SQL

请求路径/emps

请求方式:GET

xml 复制代码
<select id="selectEmi" resultType="com.example.tlias.pojo.Emp">
        select * from emp
        <where>
        <if test="name != null and name != ''">
            name like concat('%',#{name},'%')
        </if>
        <if test="gender != null">
            and gender = #{gender}
        </if>
        <if test="begin != null and end != null">
            and entrydate between #{begin} and #{end}
        </if>
        </where>
    </select>

删除员工

请求路径:/emps/{ids}

请求方式:DELETE

在路径参数中传递多个id,在后端springboot中对其封装到集合中,在Mybatis中通过动态sql来完成批量删除操作

java 复制代码
//控制层
	@DeleteMapping("/emps/{ids}")
    Result delete(@PathVariable List<Integer> ids);

    @Override
    public Result delete(List<Integer> ids) {
        empService.delete(ids);
        return Result.success();
    }

//业务层

    void delete(List<Integer> id);

    @Override
    public void delete(List<Integer> id) {
       empMapper.delete(id);
        System.out.println(id);
    }


//持久层
    void delete(@Param("id") List<Integer> id);
   
xml 复制代码
<delete id="delete">
        delete from emp where id in
        <foreach collection="id"
                item = "item"
                open = "("
                separator=","
                close=")">
                #{item}
        </foreach>
    </delete>

遍历集合进行sql判断

相关推荐
曹牧4 小时前
Spring:@RequestMapping注解,匹配的顺序与上下文无关
java·后端·spring
daixin88484 小时前
cursor无法正常使用gpt5.5等模型解决方案
java·redis·cursor
小短腿的代码世界4 小时前
Qt文件系统与IO深度解析:从QFile到异步文件操作
开发语言·qt
zhoutongsheng4 小时前
C#怎么实现Swagger文档 C#如何在ASP.NET Core中集成Swagger自动生成API文档【框架】
jvm·数据库·python
韦禾水5 小时前
记录一次项目部署到tomcat的异常
java·tomcat
曦月合一5 小时前
树莓派安装jdk、tomcat、vnc、谷歌浏览器开机自启等环境配置
java·tomcat·树莓派
harder3215 小时前
RMP模式的创新突破
开发语言·学习·ios·swift·策略模式
jinanwuhuaguo5 小时前
OpenClaw工程解剖——RAG、向量织构与“记忆宫殿”的索引拓扑学(第十三篇)
android·开发语言·人工智能·kotlin·拓扑学·openclaw
Rust研习社5 小时前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
此剑之势丶愈斩愈烈5 小时前
openssl 自建证书
java