Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

时间:2022-01-02 17:26:03

本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系

项目整体结构:

Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

Spring Boot和Spring Data结合的资源文件

application.properties

#项目端口配置
server.port=8080
server.address=0.0.0.0
#Mysql数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sys?characterEncoding=UTF8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root #JPA相关配置
#项目启动生成数据库
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#josn数据格式
spring.jackson.serialization.indent-output=true

Spring Boot启动类

com.bjsxt.ApplicationRun

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ApplicationRun {
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class,args);
}
}

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.bjsxt</groupId>
<artifactId>spring-boot-data</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.10.RELEASE</version>
<scope>compile</scope>
</dependency> <!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

因为用的是正向工程,先看俩个实体类(一对多的关系)

com.bjsxt.pojo.Company

package com.bjsxt.pojo;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set; /**
* 实体类 公司信息
*/
@Table(name = "company")
@Entity
public class Company { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cid")
private int cid; @Column(name = "cname")
private String cname; @Column(name = "location")
private String location; @OneToMany(mappedBy = "company",cascade = CascadeType.PERSIST)
private Set<Position> positions=new HashSet<>();
public Company(){} public Set<Position> getPositions() {
return positions;
} public void setPositions(Set<Position> positions) {
this.positions = positions;
} @Override
public String toString() {
return "Company{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", location='" + location + '\'' +
'}';
} public Company(String cname, String location) {
this.cname = cname;
this.location = location;
} public int getCid() {
return cid;
} public void setCid(int cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} public String getLocation() {
return location;
} public void setLocation(String location) {
this.location = location;
}
}

com.bjsxt.pojo.Position

package com.bjsxt.pojo;

import javax.persistence.*;

/**
* 实体类 公司发布的招聘信息
*/
@Entity
@Table(name = "position")
public class Position { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pid")
private int pid; @Column(name = "pname")
private String pname; @Column(name = "minsal")
private double minsal; @Column(name = "maxsal")
private double maxsal; @Column(name = "releasedate")
private String releasedate; @ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "cid")
private Company company; public Position(){} @Override
public String toString() {
return "Position{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", minsal=" + minsal +
", maxsal=" + maxsal +
", releasedate='" + releasedate + '\'' +
", company=" + company +
'}';
} public int getPid() {
return pid;
} public void setPid(int pid) {
this.pid = pid;
} public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public double getMinsal() {
return minsal;
} public void setMinsal(double minsal) {
this.minsal = minsal;
} public double getMaxsal() {
return maxsal;
} public void setMaxsal(double maxsal) {
this.maxsal = maxsal;
} public String getReleasedate() {
return releasedate;
} public void setReleasedate(String releasedate) {
this.releasedate = releasedate;
} public Company getCompany() {
return company;
} public void setCompany(Company company) {
this.company = company;
} public Position(String pname, double minsal, double maxsal, String releasedate, Company company) {
this.pname = pname;
this.minsal = minsal;
this.maxsal = maxsal;
this.releasedate = releasedate;
this.company = company;
}
}

Dao层(俩个接口都继承了JpaRepository<Company,Integer>

com.bjsxt.dao.CompanyDao

package com.bjsxt.dao;

import com.bjsxt.pojo.Company;
import org.springframework.data.jpa.repository.JpaRepository; public interface CompanyDao extends JpaRepository<Company,Integer> {
}

com.bjsxt.dao.PositionDao

package com.bjsxt.dao;

import com.bjsxt.pojo.Position;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface PositionDao extends JpaRepository<Position,Integer>, JpaSpecificationExecutor<Position>{ }

service层

由于主要是用来测试boot和data的结合,因此在在对于公司表上面的操作,仅仅就是查询,所以,公司的接口以及实现类只有一种查询所有的方法。

com.bjsxt.service.CompanyService

package com.bjsxt.service;

import com.bjsxt.pojo.Company;

import java.util.List;

public interface CompanyService {
/**
* 查询所有的公司信息
* @return
*/
public List<Company> findAll();
}

com.bjsxt.service.impl.CompanyServiceImpl

package com.bjsxt.service.impl;

import com.bjsxt.ApplicationRun;
import com.bjsxt.dao.CompanyDao;
import com.bjsxt.pojo.Company;
import com.bjsxt.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration; import java.util.List; @ContextConfiguration(classes = ApplicationRun.class)
@Service
public class CompanyServiceImpl implements CompanyService { @Autowired
private CompanyDao companyDao; /**
* 查询所有的公司信息
* @return
*/
@Override
public List<Company> findAll() {
List<Company> company = companyDao.findAll();
return company;
}
}

接下来是主要对于招聘信息表的service操作,主要有,查询所有招聘信息,模糊查询招聘信息,这俩种方法因为需要和分页结合在一起,因此在查询的操作上面有一些不同,还有就是发布招聘信息,就是增加,然后就是删除,没有做修改。

com.bjsxt.service.PositionService

package com.bjsxt.service;

import com.bjsxt.pojo.Position;
import javafx.geometry.Pos;
import org.springframework.data.domain.Page; import java.util.List; public interface PositionService {
/**
* 添加发布信息
* @param position
*/
void addPos(Position position); /**
* 查询所有招聘信息
* @return
*/
public Page<Position> findAll(Integer pageNum,Integer sizeNum); /**
* 模糊查询
* @param pname
* @return
*/
public Page<Position> findLike(String pname,Integer pageNum,Integer sizeNum); /**
* 删除一条记录
* @param pid
*/
public void deleteByid(Integer pid); }

com.bjsxt.service.impl.PositionServiceImpl

package com.bjsxt.service.impl;

import com.bjsxt.ApplicationRun;
import com.bjsxt.dao.PositionDao;
import com.bjsxt.pojo.Position;
import com.bjsxt.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; @Service
@ContextConfiguration(classes = ApplicationRun.class)
public class PositionServiceImpl implements PositionService { @Autowired
private PositionDao positionDao; /**
* 添加招聘信息
* @param position
*/
@Override
public void addPos(Position position) {
Date now =new Date();
SimpleDateFormat spdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String releasedate = spdf.format(now);
position.setReleasedate(releasedate);
positionDao.save(position);
} /**
* 查询所有招聘信息
* @return
*/
@Override
public Page<Position> findAll(Integer pageNum,Integer sizeNum) {
Sort sort=new Sort(Sort.Direction.DESC,"minsal");
Pageable pageable=new PageRequest(pageNum,sizeNum,sort);
Page<Position> pos = positionDao.findAll(pageable);
return pos;
} /**
* 模糊查询
* @param pname
* @return
*/
@Override
public Page<Position> findLike(String pname,Integer pageNum,Integer sizeNum) {
String finalPname = pname;
Specification<Position> specification=new Specification<Position>() {
@Override
public Predicate toPredicate(Root<Position> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.like(root.get("pname"),"%"+ finalPname +"%"); }
};
Sort sor=new Sort(Sort.Direction.DESC,"minsal");
Pageable pageable=new PageRequest(pageNum,sizeNum,sor);
Page<Position> pagepos = positionDao.findAll(specification,pageable);
return pagepos;
} /**
* 删除指定的招聘信息
* @param pid
*/
@Override
public void deleteByid(Integer pid) {
Position position=new Position();
position.setPid(pid);
positionDao.delete(position);
}
}

后面就是控制层,控制层我同样写了俩个类

com.bjsxt.cotroller.CompanyController

package com.bjsxt.cotroller;

import com.bjsxt.pojo.Company;
import com.bjsxt.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller
public class CompanyController {
@Autowired
private CompanyService cs; @RequestMapping("/fabu")
public String FaBuZhaoPing(Model model){
List<Company> comList = cs.findAll();
model.addAttribute("company",comList);
return "fabu.jsp";
}
}

com.bjsxt.cotroller.PositionController

package com.bjsxt.cotroller;

import com.bjsxt.pojo.Position;
import com.bjsxt.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller
public class PositionController { @Autowired
private PositionService positionService; /**
* 添加招聘信息
* @param position
* @return
*/
@RequestMapping("/addPos")
public String addPos(Position position){
positionService.addPos(position);
return "/findall";
} /**
* 查询所有
* @param model
* @return
*/
@RequestMapping("/findall")
public String findPosAll(Model model,@RequestParam(defaultValue = "0")Integer pageNum,
@RequestParam(defaultValue = "3")Integer sizeNum){
Page<Position> page = positionService.findAll(pageNum,sizeNum);
//总条数
long elements = page.getTotalElements(); //总页数
int pages = page.getTotalPages()-1; //记录当前页有多少条数据
int numberOfElements = page.getNumberOfElements(); //当前页数
int number = page.getNumber(); List<Position> posList = page.getContent();
model.addAttribute("pos",posList);
model.addAttribute("pages",pages);
model.addAttribute("elements",elements);
model.addAttribute("numberOfElements",numberOfElements);
model.addAttribute("number",number);
return "index.jsp";
} /**
* 模糊查询
* @param pname
* @param model
* @return
*/
@RequestMapping("/findlike")
public String findLike(String pname,@RequestParam(defaultValue = "0")Integer pageNum,
@RequestParam(defaultValue = "3")Integer sizeNum,Model model){
Page<Position> pagePos;
if (pname!=""){
pagePos =positionService.findLike(pname,pageNum,sizeNum);
}else {
pagePos=positionService.findAll(pageNum,sizeNum);
}
List<Position> posList = pagePos.getContent();
int pages = pagePos.getTotalPages()-1;
long elements = pagePos.getTotalElements();
int numberOfElements = pagePos.getNumberOfElements();
int number = pagePos.getNumber();
model.addAttribute("pages",pages);
model.addAttribute("elements",elements);
model.addAttribute("numberOfElements",numberOfElements);
model.addAttribute("number",number);
model.addAttribute("pos",posList);
return "index.jsp";
} /**
* 删除单一记录
* @param pid
* @return
*/
@RequestMapping("/deleteById")
public String deleteById(Integer pid){
positionService.deleteByid(pid);
return "/findall";
}
}

俩个前台页面(用的是jsp,没有用thymeleaf,因为自己还不熟悉)

主页面(包含查询的表格,跳转增加招聘信息的连接)

src/main/webapp/index.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/11/16
Time: 21:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>招聘系统企业版</h1>
<hr/>
<form action="/findlike" method="post">
按照职位:<input type="text" name="pname">&nbsp;
跳往<input type="number" name="pageNum">&nbsp;
每页<input type="number" name="sizeNum">条数据&nbsp;
<input type="submit" value="查询">
</form> <hr/>
<a href="/fabu">发布新的招聘信息</a>
<hr/>
<table border="1" align="center" width="50%">
<tr>
<th>职位名称</th>
<th>公司名称</th>
<th>职位月薪</th>
<th>工作地点</th>
<th>发布日期</th>
<th>操作</th>
</tr>
<c:forEach items="${pos}" var="pos">
<tr>
<th>${pos.pname}</th>
<th>${pos.company.cname}</th>
<th>${pos.minsal}--${pos.maxsal}</th>
<th>${pos.company.location}</th>
<th>${pos.releasedate}</th>
<th><a href="/deleteById?pid=${pos.pid}">删除</a></th>
</tr>
</c:forEach>
当前位于${number}页&nbsp;&nbsp;
当前页有${numberOfElements}条招聘信息&nbsp;&nbsp;
一共${elements}条招聘信息&nbsp;&nbsp;
一共${pages}页
</table>
</body>
</html>

发布招聘信息的表单页面

src/main/webapp/fabu.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/11/16
Time: 22:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>发布招聘信息</title>
</head>
<body>
<h1 align="center">发布招聘信息</h1>
<hr/>
<form method="post" action="/addPos">
<p>
职位名称:<input type="text" name="pname">
</p>
<p>
最低薪水:<input type="number" name="minsal">
</p>
<p>
最高薪水:<input type="number" name="maxsal">
</p>
<p>
发布公司:
<select name="company">
<option value="0">=请选择=</option>
<c:forEach items="${company}" var="com">
<option value="${com.cid}">${com.cname}</option>
</c:forEach>
</select>
</p>
<p>
<input type="submit" value="发布">
</p>
</form>
</body>
</html>

项目成果展示

查询所有数据

从图中可以看出是按照最低薪水从高到低排列

Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

分页显示

Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

发布招聘信息的表单

Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)