目录
要求:设计并完成一个数据驱动的管理系统。例如设备管理系统,或其他自己感兴趣的管理系统。
背景:设计一个笔记本电脑管理系统,供用户方便查看并管理笔记本电脑
相关源码我放在gitee内:https://gitee.com/sutiao77/Laptops_system.git
JAVA的学习可以说让我们正式认识了JAVA这门语言的魅力,从最初几行代码变为现如今的几十行到几百行,一点点的学习一点点的进步,漫长的过程也让我们对知识更深刻的记忆。
要求:设计并完成一个数据驱动的管理系统。例如设备管理系统,或其他自己感兴趣的管理系统。
功能实现:
1.设备:属性至少3个以上,包括并不限于设备类型、设备名称、初始状态……
2.添加设备
3.删除设备
4.更新设备
5.查找设备(自定义查询依据),显示查询结果。
6.分页
7.排序
背景:设计一个笔记本电脑管理系统,供用户方便查看并管理笔记本电脑
文件准备:
项目依赖:Lombok+Mysql+thmyeleaf+JPA+Spring Web
业务类(Laptops):属性,如品牌、型号、价格等等
数据访问层(Repository):负责与数据库进行交互,包括插入、更新、删除和查询信息的方法。
业务逻辑层(Service):处理业务逻辑,对信息的验证。
控制层(Controller):接收用户的请求,调用相应的服务方法,并返回处理结果。
HTML部分:主页面以及新增和更新部分页面设置
主页面(index)、新增页面(NewLaptops)、更新页面(UpdateLaptops)
开始写入:
业务类(Laptops):
package com.example.laptops_system.model;
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "genie")
public class Laptops {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "brand")
private String brand;
@Column(name = "model")
private String model;
@Column(name = "price")
private String price;
@Column(name = "monthly_sales")
private String monthly_sales;
@Column(name = "recommend")
private String recommend;
}
数据访问层(Repository):
package com.example.laptops_system.repository;
import com.example.laptops_system.model.Laptops;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface LaptopsRepository extends JpaRepository<Laptops, Long> {
@Query("select s from Laptops s where s.brand like %:brand%")
List<Laptops> findAllByGenieName(@Param("brand") String brand);
}
业务逻辑层(Service):
package com.example.laptops_system.service;
import com.example.laptops_system.model.Laptops;
import org.springframework.data.domain.Page;
import java.util.List;
public interface LaptopsService {
List<Laptops> getAllStaff();
// 新增/更新一个信息
void saveGenie(Laptops laptops);
// 根据品牌名字去查询
List<Laptops>findByGeniesName(String brand);
//删除信息
void deleteGenieById(long id);
//分页
Page<Laptops> findPaginated(int pageNo, int pageSize, String sortField, String sortDirection);
//获取指定ID的笔记本
Laptops getGenieById(long id);
}
Impl:
package com.example.laptops_system.service;
import com.example.laptops_system.model.Laptops;
import com.example.laptops_system.repository.LaptopsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class LaptopsServiceImpl implements LaptopsService {
@Autowired
private LaptopsRepository laptopsRepository;
@Override
public List<Laptops> getAllStaff() {
return laptopsRepository.findAll();
}
@Override
public void saveG