.1.springboot前后端分离开发之前要配置好很多东西,这周会详细补充博客内容和遇到的问题的解析

2,按照下面流程走一遍

此时会加载稍等一下

pom.xml显示中加上阿里云镜像可以加速下载配置文件等,或者直接复制我的所有代码

  1. <!-- druid数据库连接池-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid</artifactId>
  5. <version>1.1.10</version>
  6. </dependency>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.4.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.kude.stu</groupId>
  12. <artifactId>kudestu</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>kudestu</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-jpa</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30.  
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-devtools</artifactId>
  34. <scope>runtime</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>mysql</groupId>
  38. <artifactId>mysql-connector-java</artifactId>
  39. <version>5.1.45</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. <!-- druid数据库连接池-->
  47. <dependency>
  48. <groupId>com.alibaba</groupId>
  49. <artifactId>druid</artifactId>
  50. <version>1.1.10</version>
  51. </dependency>
  52. </dependencies>
  53.  
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62.  
  63. </project>

记得手动导入配置,如果自动导入以后一旦更改以后会遇到很多麻烦

resources加入配置文件的代码

  1. #数据源配置
  2. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  3. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  4. spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
  5. spring.datasource.username=root
  6. spring.datasource.password=root
  7. spring.datasource.initialSize=20
  8. spring.datasource.minIdle=50
  9. spring.datasource.maxActive=500
  10.  
  11. #上下文配置
  12. server.port=8888
  13. server.servlet.context-path=/stu
  14.  
  15. #配置jpa
  16. #帮我们自动生成表结构
  17. spring.jpa.properties.hibernate.hbm2ddl.auto=update
  18. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  19. spring.jpa.show-sql= true
  20. spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

一定要按照这个来定义类

下面是具体的代码

StudentController

  1. package com.kude.stu.kudestu.stu.controller;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.Student;
  4. import com.kude.stu.kudestu.stu.service.StudentService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.web.bind.annotation.*;
  8.  
  9. import javax.servlet.http.Cookie;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;
  13. import java.util.List;
  14.  
  15. @RestController
  16. @RequestMapping("/s")
  17. public class StudentController {
  18.  
  19. @Autowired
  20. private StudentService studentService;
  21.  
  22. /**
  23. * 添加学生
  24. * @param student 要添加的学生对象
  25. * @return
  26. */
  27. @PostMapping("/add")
  28. public Student save(Student student){
  29. return studentService.save(student);
  30. }
  31.  
  32. /**
  33. * 修改学生
  34. * @param student
  35. * @return
  36. */
  37. @PostMapping("/update")
  38. public Student update(Student student){
  39. return studentService.save(student);
  40. }
  41.  
  42. /**
  43. * 删除学生
  44. * @param id 要删除的学生id
  45. * @return
  46. */
  47. @GetMapping("/del/{id}")
  48. public String del(@PathVariable int id){
  49. studentService.delete(id);
  50. return "yes";
  51. }
  52.  
  53. @GetMapping("/findByName/{name}")
  54. public List<Student> findByName(@PathVariable String name){
  55. return studentService.findStuByName(name);
  56. }
  57.  
  58. @GetMapping("/query")
  59. public Page<Student> findByPage(Integer page, HttpServletResponse response){
  60.  
  61. response.setHeader("Access-Control-Allow-Origin","*");
  62.  
  63. if(page==null || page<=0){
  64. page = 0;
  65. }else{
  66. page -= 1;
  67. }
  68.  
  69. return studentService.findAll(page,5);
  70. }
  71.  
  72. }

UserController

  1. package com.kude.stu.kudestu.stu.controller;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.User;
  4. import com.kude.stu.kudestu.stu.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7.  
  8. import org.springframework.web.bind.annotation.*;
  9.  
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.util.List;
  12.  
  13. /**
  14. * 处理用户信息的控制层
  15. * @author cxf
  16. */
  17. @RestController
  18. @RequestMapping("/user")
  19. public class UserController {
  20.  
  21. @Autowired
  22. private UserService userService;
  23.  
  24. /**
  25. * 查询用户的方法
  26. */
  27. @RequestMapping("/findAll")
  28. public List<User> findAll(){
  29. return userService.findAll();
  30. }
  31.  
  32. /**
  33. * 根据ID查询用户
  34. */
  35. @RequestMapping("/query")
  36. public User findById(int id){
  37. return userService.findUserById(id);
  38. }
  39.  
  40. /**
  41. * 注册用户
  42. */
  43. @RequestMapping(value = "/reg",method = RequestMethod.POST)
  44. public User reg(User user){
  45. return userService.save(user);
  46. }
  47.  
  48. /**
  49. * 用户登陆
  50. */
  51. //@RequestMapping(value = "/login",method = RequestMethod.POST)
  52. //public User login(String username,String password){
  53. //return userService.login(username,password);
  54. // }
  55.  
  56. /**
  57. * 用户登陆
  58. */
  59. @PostMapping("/login")
  60. public User login(String username,String password){
  61. return userService.login(username,password);
  62. }
  63.  
  64. //这个分页还是没用判断好,如果页数p超过3以后,回返回全部是空值
  65. //应对超越页码数的进行限制一下
  66. /**
  67. * 分页查询
  68. */
  69. @RequestMapping("/page")
  70. public Page<User> findByPage(Integer p, HttpServletResponse response){
  71. response.setHeader("Access-Control-Allow-Origin","*");
  72. if(p==null || p<=0){
  73. p = 0;
  74. }else{
  75. p-=1;
  76. }
  77. return userService.findByPage(p,2);
  78. }
  79.  
  80. /**
  81. * 修改
  82. */
  83. @RequestMapping("/updata")
  84. public User update(User user){
  85. return userService.update(user);
  86. }
  87.  
  88. /**
  89. * 删除
  90. */
  91. @RequestMapping("/del")
  92. public String del(int id){
  93. userService.delete(id);
  94. return "yes";
  95. }
  96. }

StudentDao

  1. package com.kude.stu.kudestu.stu.dao;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.Student;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. import org.springframework.data.jpa.repository.Query;
  6. import org.springframework.data.repository.query.Param;
  7.  
  8. import java.util.List;
  9.  
  10. public interface StudentDao extends JpaRepository<Student,Integer> {
  11.  
  12. Student findStudentById(Integer id);
  13.  
  14. @Query(name = "findStuByName",nativeQuery = true,value =
  15. "select * from student where name=:name")
  16. List<Student> findStuByName(@Param("name") String name);
  17.  
  18. }

UserDao

  1. package com.kude.stu.kudestu.stu.dao;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.User;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. import org.springframework.data.jpa.repository.Query;
  6. import org.springframework.data.repository.query.Param;
  7.  
  8. public interface UserDao extends JpaRepository<User,Integer> {
  9.  
  10. User findUserById(Integer id);
  11.  
  12. //@Query("select id,username,password from user where username=?1 and password=?2")
  13. // User login(@Param("username") String username, @Param("password") String password);
  14.  
  15. //如何两个usernam或者password一样的User用户,回出现500错误
  16. //这种方法不能用username或者password登入
  17.  
  18. @Query(name="login",nativeQuery = true,value =
  19. "select * from user where username=:username and password=:password")
  20. User login(@Param("username") String username, @Param("password") String password);
  21.  
  22. }

Student

  1. package com.kude.stu.kudestu.stu.dao;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.User;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. import org.springframework.data.jpa.repository.Query;
  6. import org.springframework.data.repository.query.Param;
  7.  
  8. public interface UserDao extends JpaRepository<User,Integer> {
  9.  
  10. User findUserById(Integer id);
  11.  
  12. //@Query("select id,username,password from user where username=?1 and password=?2")
  13. // User login(@Param("username") String username, @Param("password") String password);
  14.  
  15. //如何两个usernam或者password一样的User用户,回出现500错误
  16. //这种方法不能用username或者password登入
  17.  
  18. @Query(name="login",nativeQuery = true,value =
  19. "select * from user where username=:username and password=:password")
  20. User login(@Param("username") String username, @Param("password") String password);
  21.  
  22. }

User

  1. package com.kude.stu.kudestu.stu.entity;
  2.  
  3. import javax.persistence.*;
  4.  
  5. @Entity
  6. @Table(name = "user")
  7. public class User {
  8.  
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. private Integer id;
  12. private String username;
  13. private String password;
  14.  
  15. @Override
  16. public String toString() {
  17. return "User{" +
  18. "id=" + id +
  19. ", username='" + username + '\'' +
  20. ", password='" + password + '\'' +
  21. '}';
  22. }
  23.  
  24. public Integer getId() {
  25. return id;
  26. }
  27.  
  28. public void setId(Integer id) {
  29. this.id = id;
  30. }
  31.  
  32. public String getUsername() {
  33. return username;
  34. }
  35.  
  36. public void setUsername(String username) {
  37. this.username = username;
  38. }
  39.  
  40. public String getPassword() {
  41. return password;
  42. }
  43.  
  44. public void setPassword(String password) {
  45. this.password = password;
  46. }
  47.  
  48. public User() {
  49. }
  50. }

StudentService

  1. package com.kude.stu.kudestu.stu.service;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.Student;
  4. import org.springframework.data.domain.Page;
  5.  
  6. import java.util.List;
  7.  
  8. public interface StudentService {
  9.  
  10. Student save(Student student);
  11. Student update(Student student);
  12. void delete(Integer sid);
  13. Student findStuById(Integer id);
  14. List<Student> findStuByName(String name);
  15.  
  16. /**
  17. * 分页查询所有数据
  18. * @param page 当前页
  19. * @param pageSize 每页记录数
  20. * @return
  21. */
  22. Page<Student> findAll(int page,int pageSize);
  23.  
  24. }

StudentServiceImpl

  1. package com.kude.stu.kudestu.stu.service;
  2.  
  3. import com.kude.stu.kudestu.stu.dao.StudentDao;
  4. import com.kude.stu.kudestu.stu.entity.Student;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.stereotype.Service;
  10.  
  11. import java.util.List;
  12.  
  13. @Service
  14. public class StudentServiceImpl implements StudentService {
  15.  
  16. @Autowired
  17. private StudentDao studentDao;
  18.  
  19. @Override
  20. public Student save(Student student) {
  21. return studentDao.save(student);
  22. }
  23.  
  24. @Override
  25. public Student update(Student student) {
  26. return studentDao.save(student);
  27. }
  28.  
  29. @Override
  30. public void delete(Integer sid) {
  31. studentDao.deleteById(sid);
  32. }
  33.  
  34. @Override
  35. public Student findStuById(Integer sid) {
  36. return studentDao.findStudentById(sid);
  37. }
  38.  
  39. @Override
  40. public List<Student> findStuByName(String name) {
  41. return studentDao.findStuByName(name);
  42. }
  43.  
  44. @Override
  45. public Page<Student> findAll(int page,int pageSize) {
  46. Pageable pageable = PageRequest.of(page,pageSize);
  47. return studentDao.findAll(pageable);
  48. }
  49. }

UserService

  1. package com.kude.stu.kudestu.stu.service;
  2.  
  3. import com.kude.stu.kudestu.stu.entity.User;
  4. import org.springframework.data.domain.Page;
  5.  
  6. import java.util.List;
  7.  
  8. public interface UserService {
  9.  
  10. List<User> findAll();
  11. User findUserById(Integer id);
  12. User save(User user);
  13. User update(User user);
  14. void delete(int id);
  15. User login(String username,String password);
  16. Page<User> findByPage(int page, int limit);
  17.  
  18. }

UserServiceImpl

  1. package com.kude.stu.kudestu.stu.service;
  2.  
  3. import com.kude.stu.kudestu.stu.dao.UserDao;
  4. import com.kude.stu.kudestu.stu.entity.User;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.domain.Page;
  7. import org.springframework.data.domain.PageRequest;
  8. import org.springframework.data.domain.Pageable;
  9. import org.springframework.data.domain.Sort;
  10. import org.springframework.stereotype.Service;
  11.  
  12. import java.util.List;
  13.  
  14. @Service
  15. public class UserServiceImpl implements UserService {
  16.  
  17. @Autowired
  18. private UserDao userDao;
  19.  
  20. @Override
  21. public List<User> findAll() {
  22. return userDao.findAll();
  23. }
  24.  
  25. @Override
  26. public User findUserById(Integer id) {
  27. return userDao.findUserById(id);
  28. }
  29.  
  30. @Override
  31. public User save(User user) {
  32. return userDao.save(user);
  33. }
  34.  
  35. @Override
  36. public User update(User user) {
  37. return userDao.save(user);
  38. }
  39.  
  40. @Override
  41. public void delete(int id) {
  42. userDao.deleteById(id);
  43. }
  44.  
  45. @Override
  46. public User login(String username, String password) {
  47. return userDao.login(username,password);
  48. }
  49.  
  50. @Override
  51. public Page<User> findByPage(int page, int pageSize) {
  52. Pageable pageable = PageRequest.of(page,pageSize,new Sort(Sort.Direction.ASC,"id"));
  53. return userDao.findAll(pageable);
  54. }
  55. }

KudestuApplication是自动生成的

  1. package com.kude.stu.kudestu;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class KudestuApplication {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(KudestuApplication.class, args);
  11. }
  12.  
  13. }

在写entity的User和Student的时候,要与要连接的数据库对应数据类型和名字相同

运行

其他内容一并等修改此博客时好好完成

总结:

1.要用到JDK和对应版本的IDEA,Maven,需要在系统中配置

2.用到Navicat premium数据库,Hbuilder前端开发,http://www.bejson.com/在线测试,Postman测试

Springboot前后端分离开发的更多相关文章

  1. springboot 前后端分离开发 从零到整(一、环境的搭建)

    第一次写文章,有什么错误地方请大家指正,也请大家见谅. 这次为大家分享我做毕业设计的一个过程,之前没有接触过springboot,一直做的都是Javaweb和前端,做了几个前后端分离的项目.现在听说s ...

  2. springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)

    今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...

  3. springboot 前后端分离开发 从零到整(二、邮箱注册)

    spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...

  4. springboot 前后端分离开发解决跨域访问

    最近新学习了Java EE开发框架springboot,我在使用springboot前后台分离开发的过程中遇到了跨域求问题.在网上寻找答案的过程中发现网上的解决方案大多比较零散,我在这里整理一个解决方 ...

  5. springboot 前后端分离开发 从零到整(四、更改密码操作)

    前端发送更改密码请求,头部携带token,服务端拦截器拦截头部token并解析,根据token中的信息来查询用户信息.需要登录才能进行的操作是由自己定的,有些操作可以直接放行.具体实现是: 上一章写到 ...

  6. SpringBoot,Vue前后端分离开发首秀

    需求:读取数据库的数据展现到前端页面 技术栈:后端有主要有SpringBoot,lombok,SpringData JPA,Swagger,跨域,前端有Vue和axios 不了解这些技术的可以去入门一 ...

  7. vue+springboot前后端分离实现单点登录跨域问题处理

    最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...

  8. 基于SpringBoot前后端分离的点餐系统

    基于SpringBoot前后端分离的点餐系统 开发环境:主要采用Spring boot框架和小程序开发 项目简介:点餐系统,分成卖家端和买家端.买家端使用微信小程序开发,实现扫码点餐.浏览菜单.下单. ...

  9. vue+mockjs 模拟数据,实现前后端分离开发

    在项目中尝试了mockjs,mock数据,实现前后端分离开发. 关于mockjs,官网描述的是 1.前后端分离 2.不需要修改既有代码,就可以拦截 Ajax 请求,返回模拟的响应数据. 3.数据类型丰 ...

随机推荐

  1. js 时间戳转yyyy-MM-dd HH-mm-ss工具类

    转载自:https://blog.csdn.net/shan1774965666/article/details/55049819 在web开发中,我们经常需要用js将时间戳转yyyy-MM-dd H ...

  2. 04-align-content 它对于当单行是没有效果的

    /* 运用在父级元素上  align-content:   它通产与子元素的div{margin:10px 一起联合使用 }*/ ps==>用在子项出现换行的情况下,并是多行的情况下哦.运用在子 ...

  3. 高频Python面试题分享

    一.Python语言中你用过哪些方式来实现进程间通信1.队列Queue 2.Pipe管道 只适用于两个进程之间的通信, pipe的效率高于queue 3.共享内存 4.socket套接字(UDP即可) ...

  4. c# WF 第5节 窗体的控件

    本节内容: 1:控件在哪里 2:控件怎么添加 3:窗口的显示与隐藏 4:实例单击窗体,出现另一个窗体 1:控件在哪里 视图 --> 工具箱 2:控件怎么添加 第一种:从工具箱直接拉 第二种:在代 ...

  5. 201871010111-刘佳华《面向对象程序设计(java)》第七周学习总结

    201871010111-刘佳华<面向对象程序设计(java)>第七周学习总结 实验时间 2019-10-11 1.实验目的与要求 1) 掌握四种访问权限修饰符的使用特点: (1)进一步理 ...

  6. HTML与CSS学习笔记(7)

    1.响应式布局 利用媒体查询,即media queries,可以针对不同的媒体类型定义不同的样式,从而实现响应式布局. 常见选项: 媒体类型 and.not min-width.max-width: ...

  7. day4_7.2

    流程语句 1.if判断语句 在python中if语句可以依据判断的条件,决定执行哪个语句.其格式如下: if 条件: 代码1 else: 代码2 当满足条件1时,执行代码1,否则执行代码2.所以条件语 ...

  8. angular 新建命令

    新建项目:ng new my-app 新建组件 ng g c name //组件名称(深层次参考:https://www.cnblogs.com/mary-123/p/10484648.html) 默 ...

  9. html行级元素与块级元素以及meta标签的使用

    块级元素的特性: 永远都会占满父级元素的宽度(块级元素的宽度永远都等于它父级元素的宽度) 行级元素的特性: 所占的空间刚好等于内容的大小 常见的块级元素: h1~h6.p.ul.div.li.form ...

  10. lincense更新失败

    用户的证书到期,页面无法访问, 1 #/dashboard/systemindex在这里面上传证书文件,一分钟后会恢复正常 2 在后台直接配置license字段,将license文件的内容直接拷贝过去 ...