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

项目整体结构:

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结合小项目(增,删,查,模糊查询,分页,排序)的更多相关文章

  1. Spring boot+mybatis+thymeleaf 实现登录注册,增删改查

    本文重在实现理解,过滤器,业务,逻辑需求,样式请无视.. 项目结构如下 1.idea新建Spring boot项目,在pom中加上thymeleaf和mybatis支持.pom.xml代码如下 < ...

  2. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  3. 分享spring、spring boot、spring cloud一些学习资源,从基础知识到项目实战

    1.spring注解驱动开发,学习spring boot和spring cloud必备知识 链接: https://pan.baidu.com/s/1xhULzLlpkERhoMi1G5Lgfg 密码 ...

  4. Spring Boot -- Idea搭建下搭建web项目

    最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...

  5. spring boot系列(五)spring boot 配置spring data jpa (查询方法)

    接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...

  6. 用Spring Boot零配置快速创建web项目(1)

    一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  7. maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...

  8. Spring Boot 学习(一) 快速搭建SpringBoot 项目

    快速搭建一个 Spring Boot 项目 部分参考于<深入实践Spring Boot>.<Spring实战 第四版>与程序猿DD的有关博客. 参考(嘟嘟独立博客):http: ...

  9. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

随机推荐

  1. 【vue】在VS Code中调试Jest单元测试

    在VS Code中调试Jest单元测试 添加调试任务 打开 vscode launch.json 文件,在 configurations 内加入下面代码 "configurations&qu ...

  2. 一分钟带你学会利用mybatis-generator自动生成代码!

    目录 一.MyBatis Generator简介 二.使用方式 三.实战 之前的文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介 ...

  3. keeplived离线安装openssl-devel依赖包

    转载自素文宅博客:https://blog.yoodb.com/yoodb/article/detail/1434 由于公司业务并发比较高需要高可用使用LVS keeplived.在linux系统ce ...

  4. 使用VSCode调试Egret项目中的ts代码

    发布一次Android项目后,会在代码里,生成对应的.map文件.这样就可以在编辑器里或是Chrome里面对相应的TS文件进行断点调试了. 实际只要在tsconfig.json里面配置一下," ...

  5. T-SQL, Part I: LIKE Pattern

    The basic usage of LIKE pattern: %: it would be placed at the end and/or the beginning of a string. ...

  6. CSS如何修改tr边框属性

    有很多时候,我们都要自定义为表格合并边框,这个只要 table{ border-collapse:collapse; } 就可以了 参数: separate 默认值.边框会被分开.不会忽略border ...

  7. nyoj 4 ASCII码排序

    ASCII码排序 时间限制:3000 ms  |  内存限制:65535 KB | 难度:2   描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符.   输入 第一 ...

  8. mysql数据库如何赋予远程某个IP 访问权限

    1.授权用户root使用密码jb51从任意主机连接到mysql服务器: 代码如下: GRANT ALL PRIVILEGES ON *.* TO 'ROOT'@'%' IDENTIFIED BY 'j ...

  9. Nginx下HTTP强制重定向至HTTPS

    Nginx下HTTP强制重定向至HTTPS 对于nginx来说,配置http强制重定向至https有多种多样的写法.可以直接rewrite,也可以用301重定向.但是直接拷贝网上的配置往往会出现问题, ...

  10. ThreadLocal线程局部变量的使用

    ThreadLocal: 线程局部变量 一).ThreadLocal的引入 用途:是解决多线程间并发访问的方案,不是解决数据共享的方案. 特点:每个线程提供变量的独立副本,所有的线程使用同一个Thre ...