Spring Boot + Layui + Spring Data JPA 实现前后端分离下简单增删改查分页操作
2021-03-09 17:29
阅读:603
YPE html>
标签:依赖 interface pre 文章 全选 === 表单 ddl title
Spring Boot + Layui + Spring Data JPA 实现前后端分离下简单增删改查分页操作
1.项目背景
自己搭着玩,写个小demo
2.项目构成
后端:Spring boot 2.3 JDK1.8
数据库:Spring Data JPA MySQL 5.7.31
前端: Layui2.5.7
3.添加依赖
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime org.projectlombok
lombok
true org.springframework.boot
spring-boot-starter-test
test junit
junit
4.12
4.配置文件application.yml
#服务端容器的配置
server:
port: 8888
#数据库配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/kaitao?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
#jpa配置
jpa:
show-sql: true
hibernate:
ddl-auto: update
5.导入数据库
CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`reader` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
创建数据库:kaitao 编码:UTF-8
6.entity包
package com.godwin.entity;
import lombok.Data;
import javax.persistence.*;
/**
* 文章信息实体类
* Created by admin on 2020/12/17.
*/
@Entity
@Data
@Table(name = "article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String title;
private String type;
private String reader;
private String content;
}
package com.godwin.entity;
/**
* @Description: 响应消息体
* Created by admin on 2020/12/17.
*/
public class ResultBean {
/**响应编码*/
private int code;
/**响应消息*/
private String msg;
/**数据总量*/
private int count;
/**数据*/
private T data;
public ResultBean() {
}
public ResultBean(int code, String msg, int count, T data) {
super();
this.code = code;
this.msg = msg;
this.count = count;
this.data = data;
}
@Override
public String toString() {
return "R [code=" + code + ", msg=" + msg + ", count=" + count + ", data=" + data + "]";
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
7.repository包(操作数据库)
package com.godwin.repository;
import com.godwin.entity.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
/**
* 操作数据库
* Created by admin on 2020/12/17.
*/
public interface ArticleRepository extends JpaRepository {
//批量删除
@Transactional
@Modifying
@Query(value="delete from article where id in ?1 ",nativeQuery=true)
int deletes(Integer[] ids);
}
8.service包
package com.godwin.service;
import com.godwin.entity.Article;
import org.springframework.data.domain.Page;
import java.util.List;
/**
* Created by admin on 2020/12/17.
*/
public interface ArticleService {
Article save(Article article);
Article edit(Article article);
void deletes(Integer[] ids);
List findAll();
Page getPager(Integer page, Integer limit);
}
package com.godwin.service;
import com.godwin.entity.Article;
import com.godwin.repository.ArticleRepository;
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.stereotype.Service;
import java.util.List;
/**
* Created by admin on 2020/12/17.
*/
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository repository;
@Override
public Article save(Article article) {
return repository.save(article);
}
@Override
public Article edit(Article article) {
return repository.save(article);
}
@Override
public void deletes(Integer[] ids) {
repository.deletes(ids);
}
@Override
public List findAll() {
return repository.findAll();
}
@Override
public Page getPager(Integer page, Integer limit) {
Pageable pageable = PageRequest.of(page-1,10);
return repository.findAll(pageable);
}
}
9.controller包
package com.godwin.controller;
import com.godwin.entity.Article;
import com.godwin.entity.ResultBean;
import com.godwin.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Created by admin on 2020/12/17.
*/
@RestController
@RequestMapping("/kaitao")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("/list")
public ResultBean list1(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer limit){
List data = articleService.getPager(page,limit).getContent();
int count = articleService.findAll().size();
ResultBean result = new ResultBean(0,"",count,data);
return result;
}
@PostMapping("/save")
public ResultBean save(@RequestBody Article article){
// 判断是新增还是修改
if(article.getId()!=null){
articleService.edit(article);
}else{
articleService.save(article);
}
return new ResultBean(200,"",0,"");
}
@PostMapping("/remove")
public ResultBean remove(@RequestBody Integer[] ids){
articleService.deletes(ids);
return new ResultBean(200,"",0,"");
}
}
10.config(配置全局跨域)
package com.godwin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 添加全局跨域配置
* Created by admin on 2020/12/18.
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}
11.前端页面
开淘网后台管理系统
开淘网后台管理系统
-
控制台
-
商品管理
-
用户
-
其它系统
-
邮件管理
-
消息管理
-
授权管理
-
admin
-
基本资料
-
安全设置
-
退出
-
文章管理
-
文章信息
-
文章评论
-
广告管理
-
广告信息
-
广告评论
总结:过程中遇到的问题。
- 前后端分离,前后端用JSON交互,所以要写一个工具类,用来放“code,msg,count,data”,方便后端传JSON数据给前端。
- 要配置全局跨域,否者前端拿不到后端数据
- 要写一个批量删除,所以自己在repository写了一个sql语句
- 用的layui框架,要遵循layui的规则,前端页面复制前记得导入layui静态文件
- 记得改yml数据库名称.用户和密码
有什么问题可以后台留言
Spring Boot + Layui + Spring Data JPA 实现前后端分离下简单增删改查分页操作
标签:依赖 interface pre 文章 全选 === 表单 ddl title
原文地址:https://www.cnblogs.com/XING-ZHI-JI-DA-XUE/p/14169437.html
上一篇:如何优雅的使用和理解线程池
下一篇:Java web学习
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:Spring Boot + Layui + Spring Data JPA 实现前后端分离下简单增删改查分页操作
文章链接:http://soscw.com/index.php/essay/62378.html
文章标题:Spring Boot + Layui + Spring Data JPA 实现前后端分离下简单增删改查分页操作
文章链接:http://soscw.com/index.php/essay/62378.html
评论
亲,登录后才可以留言!