Springboot前后端分离开发
.1.springboot前后端分离开发之前要配置好很多东西,这周会详细补充博客内容和遇到的问题的解析
2,按照下面流程走一遍
此时会加载稍等一下
pom.xml显示中加上阿里云镜像可以加速下载配置文件等,或者直接复制我的所有代码
- <!-- druid数据库连接池-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.10</version>
- </dependency>
- <?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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.4.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.kude.stu</groupId>
- <artifactId>kudestu</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>kudestu</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <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-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.45</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- druid数据库连接池-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.10</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
记得手动导入配置,如果自动导入以后一旦更改以后会遇到很多麻烦
resources加入配置文件的代码
- #数据源配置
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
- spring.datasource.driverClassName=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
- spring.datasource.username=root
- spring.datasource.password=root
- spring.datasource.initialSize=20
- spring.datasource.minIdle=50
- spring.datasource.maxActive=500
- #上下文配置
- server.port=8888
- server.servlet.context-path=/stu
- #配置jpa
- #帮我们自动生成表结构
- spring.jpa.properties.hibernate.hbm2ddl.auto=update
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- spring.jpa.show-sql= true
- spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
一定要按照这个来定义类
下面是具体的代码
StudentController
- package com.kude.stu.kudestu.stu.controller;
- import com.kude.stu.kudestu.stu.entity.Student;
- import com.kude.stu.kudestu.stu.service.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.util.List;
- @RestController
- @RequestMapping("/s")
- public class StudentController {
- @Autowired
- private StudentService studentService;
- /**
- * 添加学生
- * @param student 要添加的学生对象
- * @return
- */
- @PostMapping("/add")
- public Student save(Student student){
- return studentService.save(student);
- }
- /**
- * 修改学生
- * @param student
- * @return
- */
- @PostMapping("/update")
- public Student update(Student student){
- return studentService.save(student);
- }
- /**
- * 删除学生
- * @param id 要删除的学生id
- * @return
- */
- @GetMapping("/del/{id}")
- public String del(@PathVariable int id){
- studentService.delete(id);
- return "yes";
- }
- @GetMapping("/findByName/{name}")
- public List<Student> findByName(@PathVariable String name){
- return studentService.findStuByName(name);
- }
- @GetMapping("/query")
- public Page<Student> findByPage(Integer page, HttpServletResponse response){
- response.setHeader("Access-Control-Allow-Origin","*");
- if(page==null || page<=0){
- page = 0;
- }else{
- page -= 1;
- }
- return studentService.findAll(page,5);
- }
- }
UserController
- package com.kude.stu.kudestu.stu.controller;
- import com.kude.stu.kudestu.stu.entity.User;
- import com.kude.stu.kudestu.stu.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.util.List;
- /**
- * 处理用户信息的控制层
- * @author cxf
- */
- @RestController
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- private UserService userService;
- /**
- * 查询用户的方法
- */
- @RequestMapping("/findAll")
- public List<User> findAll(){
- return userService.findAll();
- }
- /**
- * 根据ID查询用户
- */
- @RequestMapping("/query")
- public User findById(int id){
- return userService.findUserById(id);
- }
- /**
- * 注册用户
- */
- @RequestMapping(value = "/reg",method = RequestMethod.POST)
- public User reg(User user){
- return userService.save(user);
- }
- /**
- * 用户登陆
- */
- //@RequestMapping(value = "/login",method = RequestMethod.POST)
- //public User login(String username,String password){
- //return userService.login(username,password);
- // }
- /**
- * 用户登陆
- */
- @PostMapping("/login")
- public User login(String username,String password){
- return userService.login(username,password);
- }
- //这个分页还是没用判断好,如果页数p超过3以后,回返回全部是空值
- //应对超越页码数的进行限制一下
- /**
- * 分页查询
- */
- @RequestMapping("/page")
- public Page<User> findByPage(Integer p, HttpServletResponse response){
- response.setHeader("Access-Control-Allow-Origin","*");
- if(p==null || p<=0){
- p = 0;
- }else{
- p-=1;
- }
- return userService.findByPage(p,2);
- }
- /**
- * 修改
- */
- @RequestMapping("/updata")
- public User update(User user){
- return userService.update(user);
- }
- /**
- * 删除
- */
- @RequestMapping("/del")
- public String del(int id){
- userService.delete(id);
- return "yes";
- }
- }
StudentDao
- package com.kude.stu.kudestu.stu.dao;
- import com.kude.stu.kudestu.stu.entity.Student;
- 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 StudentDao extends JpaRepository<Student,Integer> {
- Student findStudentById(Integer id);
- @Query(name = "findStuByName",nativeQuery = true,value =
- "select * from student where name=:name")
- List<Student> findStuByName(@Param("name") String name);
- }
UserDao
- package com.kude.stu.kudestu.stu.dao;
- import com.kude.stu.kudestu.stu.entity.User;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.query.Param;
- public interface UserDao extends JpaRepository<User,Integer> {
- User findUserById(Integer id);
- //@Query("select id,username,password from user where username=?1 and password=?2")
- // User login(@Param("username") String username, @Param("password") String password);
- //如何两个usernam或者password一样的User用户,回出现500错误
- //这种方法不能用username或者password登入
- @Query(name="login",nativeQuery = true,value =
- "select * from user where username=:username and password=:password")
- User login(@Param("username") String username, @Param("password") String password);
- }
Student
- package com.kude.stu.kudestu.stu.dao;
- import com.kude.stu.kudestu.stu.entity.User;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.query.Param;
- public interface UserDao extends JpaRepository<User,Integer> {
- User findUserById(Integer id);
- //@Query("select id,username,password from user where username=?1 and password=?2")
- // User login(@Param("username") String username, @Param("password") String password);
- //如何两个usernam或者password一样的User用户,回出现500错误
- //这种方法不能用username或者password登入
- @Query(name="login",nativeQuery = true,value =
- "select * from user where username=:username and password=:password")
- User login(@Param("username") String username, @Param("password") String password);
- }
User
- package com.kude.stu.kudestu.stu.entity;
- import javax.persistence.*;
- @Entity
- @Table(name = "user")
- public class User {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- private String username;
- private String password;
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public User() {
- }
- }
StudentService
- package com.kude.stu.kudestu.stu.service;
- import com.kude.stu.kudestu.stu.entity.Student;
- import org.springframework.data.domain.Page;
- import java.util.List;
- public interface StudentService {
- Student save(Student student);
- Student update(Student student);
- void delete(Integer sid);
- Student findStuById(Integer id);
- List<Student> findStuByName(String name);
- /**
- * 分页查询所有数据
- * @param page 当前页
- * @param pageSize 每页记录数
- * @return
- */
- Page<Student> findAll(int page,int pageSize);
- }
StudentServiceImpl
- package com.kude.stu.kudestu.stu.service;
- import com.kude.stu.kudestu.stu.dao.StudentDao;
- import com.kude.stu.kudestu.stu.entity.Student;
- 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;
- @Service
- public class StudentServiceImpl implements StudentService {
- @Autowired
- private StudentDao studentDao;
- @Override
- public Student save(Student student) {
- return studentDao.save(student);
- }
- @Override
- public Student update(Student student) {
- return studentDao.save(student);
- }
- @Override
- public void delete(Integer sid) {
- studentDao.deleteById(sid);
- }
- @Override
- public Student findStuById(Integer sid) {
- return studentDao.findStudentById(sid);
- }
- @Override
- public List<Student> findStuByName(String name) {
- return studentDao.findStuByName(name);
- }
- @Override
- public Page<Student> findAll(int page,int pageSize) {
- Pageable pageable = PageRequest.of(page,pageSize);
- return studentDao.findAll(pageable);
- }
- }
UserService
- package com.kude.stu.kudestu.stu.service;
- import com.kude.stu.kudestu.stu.entity.User;
- import org.springframework.data.domain.Page;
- import java.util.List;
- public interface UserService {
- List<User> findAll();
- User findUserById(Integer id);
- User save(User user);
- User update(User user);
- void delete(int id);
- User login(String username,String password);
- Page<User> findByPage(int page, int limit);
- }
UserServiceImpl
- package com.kude.stu.kudestu.stu.service;
- import com.kude.stu.kudestu.stu.dao.UserDao;
- import com.kude.stu.kudestu.stu.entity.User;
- 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;
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- @Override
- public List<User> findAll() {
- return userDao.findAll();
- }
- @Override
- public User findUserById(Integer id) {
- return userDao.findUserById(id);
- }
- @Override
- public User save(User user) {
- return userDao.save(user);
- }
- @Override
- public User update(User user) {
- return userDao.save(user);
- }
- @Override
- public void delete(int id) {
- userDao.deleteById(id);
- }
- @Override
- public User login(String username, String password) {
- return userDao.login(username,password);
- }
- @Override
- public Page<User> findByPage(int page, int pageSize) {
- Pageable pageable = PageRequest.of(page,pageSize,new Sort(Sort.Direction.ASC,"id"));
- return userDao.findAll(pageable);
- }
- }
KudestuApplication是自动生成的
- package com.kude.stu.kudestu;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class KudestuApplication {
- public static void main(String[] args) {
- SpringApplication.run(KudestuApplication.class, args);
- }
- }
在写entity的User和Student的时候,要与要连接的数据库对应数据类型和名字相同
运行
其他内容一并等修改此博客时好好完成
总结:
1.要用到JDK和对应版本的IDEA,Maven,需要在系统中配置
2.用到Navicat premium数据库,Hbuilder前端开发,http://www.bejson.com/在线测试,Postman测试
Springboot前后端分离开发的更多相关文章
- springboot 前后端分离开发 从零到整(一、环境的搭建)
第一次写文章,有什么错误地方请大家指正,也请大家见谅. 这次为大家分享我做毕业设计的一个过程,之前没有接触过springboot,一直做的都是Javaweb和前端,做了几个前后端分离的项目.现在听说s ...
- springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)
今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...
- springboot 前后端分离开发 从零到整(二、邮箱注册)
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...
- springboot 前后端分离开发解决跨域访问
最近新学习了Java EE开发框架springboot,我在使用springboot前后台分离开发的过程中遇到了跨域求问题.在网上寻找答案的过程中发现网上的解决方案大多比较零散,我在这里整理一个解决方 ...
- springboot 前后端分离开发 从零到整(四、更改密码操作)
前端发送更改密码请求,头部携带token,服务端拦截器拦截头部token并解析,根据token中的信息来查询用户信息.需要登录才能进行的操作是由自己定的,有些操作可以直接放行.具体实现是: 上一章写到 ...
- SpringBoot,Vue前后端分离开发首秀
需求:读取数据库的数据展现到前端页面 技术栈:后端有主要有SpringBoot,lombok,SpringData JPA,Swagger,跨域,前端有Vue和axios 不了解这些技术的可以去入门一 ...
- vue+springboot前后端分离实现单点登录跨域问题处理
最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...
- 基于SpringBoot前后端分离的点餐系统
基于SpringBoot前后端分离的点餐系统 开发环境:主要采用Spring boot框架和小程序开发 项目简介:点餐系统,分成卖家端和买家端.买家端使用微信小程序开发,实现扫码点餐.浏览菜单.下单. ...
- vue+mockjs 模拟数据,实现前后端分离开发
在项目中尝试了mockjs,mock数据,实现前后端分离开发. 关于mockjs,官网描述的是 1.前后端分离 2.不需要修改既有代码,就可以拦截 Ajax 请求,返回模拟的响应数据. 3.数据类型丰 ...
随机推荐
- js 时间戳转yyyy-MM-dd HH-mm-ss工具类
转载自:https://blog.csdn.net/shan1774965666/article/details/55049819 在web开发中,我们经常需要用js将时间戳转yyyy-MM-dd H ...
- 04-align-content 它对于当单行是没有效果的
/* 运用在父级元素上 align-content: 它通产与子元素的div{margin:10px 一起联合使用 }*/ ps==>用在子项出现换行的情况下,并是多行的情况下哦.运用在子 ...
- 高频Python面试题分享
一.Python语言中你用过哪些方式来实现进程间通信1.队列Queue 2.Pipe管道 只适用于两个进程之间的通信, pipe的效率高于queue 3.共享内存 4.socket套接字(UDP即可) ...
- c# WF 第5节 窗体的控件
本节内容: 1:控件在哪里 2:控件怎么添加 3:窗口的显示与隐藏 4:实例单击窗体,出现另一个窗体 1:控件在哪里 视图 --> 工具箱 2:控件怎么添加 第一种:从工具箱直接拉 第二种:在代 ...
- 201871010111-刘佳华《面向对象程序设计(java)》第七周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第七周学习总结 实验时间 2019-10-11 1.实验目的与要求 1) 掌握四种访问权限修饰符的使用特点: (1)进一步理 ...
- HTML与CSS学习笔记(7)
1.响应式布局 利用媒体查询,即media queries,可以针对不同的媒体类型定义不同的样式,从而实现响应式布局. 常见选项: 媒体类型 and.not min-width.max-width: ...
- day4_7.2
流程语句 1.if判断语句 在python中if语句可以依据判断的条件,决定执行哪个语句.其格式如下: if 条件: 代码1 else: 代码2 当满足条件1时,执行代码1,否则执行代码2.所以条件语 ...
- angular 新建命令
新建项目:ng new my-app 新建组件 ng g c name //组件名称(深层次参考:https://www.cnblogs.com/mary-123/p/10484648.html) 默 ...
- html行级元素与块级元素以及meta标签的使用
块级元素的特性: 永远都会占满父级元素的宽度(块级元素的宽度永远都等于它父级元素的宽度) 行级元素的特性: 所占的空间刚好等于内容的大小 常见的块级元素: h1~h6.p.ul.div.li.form ...
- lincense更新失败
用户的证书到期,页面无法访问, 1 #/dashboard/systemindex在这里面上传证书文件,一分钟后会恢复正常 2 在后台直接配置license字段,将license文件的内容直接拷贝过去 ...