赞
踩
这篇主要展示该案例的后端代码和功能实现,后端实现数据的操作功能后返回Json数组。
1. User.java
- package com.example.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;
-
- public User() {
- }
-
- @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;
- }
- }
2. Student.java
- package com.example.stu.kudestu.stu.entity;
-
- import javax.persistence.*;
-
- @Entity
- @Table(name = "student")
- public class Student {
-
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- private String name;
- private Integer age;
- private String sex;
-
-
- public Student() {
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- ", sex='" + sex + '\'' +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
1. UserDao
- package com.example.stu.kudestu.stu.dao;
-
- import com.example.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(name="login",nativeQuery = true,value = "select * from user where username=:username and password=:password")
- User login(@Param("username") String username, @Param("password") String password);
- }
2. StudentDao
- package com.example.stu.kudestu.stu.dao;
-
- import com.example.stu.kudestu.stu.entity.Student;
- import com.example.stu.kudestu.stu.entity.User;
- import org.springframework.data.domain.Page;
- 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 findStuById(Integer id);
-
- @Query(name = "findStuByName",nativeQuery = true,value =
- "select * from student where name=:name")
- List<Student> findStuByName(@Param("name") String name);
-
- }
1. UserService
- package com.example.stu.kudestu.stu.service;
-
- import com.example.stu.kudestu.stu.entity.User;
- import org.springframework.data.domain.Page;
-
- import java.util.List;
-
- public interface UserService {
-
- List<User> findAll();
-
- User findUserById(int 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 pagesize);
-
- }
2. UserServiceImpl
- package com.example.stu.kudestu.stu.service;
-
- import com.example.stu.kudestu.stu.dao.UserDao;
- import com.example.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.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class UserServiceImpl implements UserService{
-
-
-
- @Autowired
- UserDao userDao;
-
- @Override
- public List<User> findAll() {
- return userDao.findAll();
- }
-
- @Override
- public User findUserById(int 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);
- return userDao.findAll(pageable);
- }
- }
3. StudentService
- package com.example.stu.kudestu.stu.service;
-
- import com.example.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 id);
- //根据id查询
- Student findStuById(Integer id);
- //根据名字查询
- List<Student> fingStuByName(String name);
-
- /**
- * 分页查询所有
- * @param page
- * @param pagesize
- * @return
- */
- Page<Student> findAll(int page,int pagesize);
-
- }
4. StudentServiceImol
- package com.example.stu.kudestu.stu.service;
-
- import com.example.stu.kudestu.stu.dao.StudentDao;
- import com.example.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
- 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 id) {
- studentDao.deleteById(id);
- }
-
- @Override
- public Student findStuById(Integer id) {
- return studentDao.findStuById(id);
- }
-
- @Override
- public List<Student> fingStuByName(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);
- }
- }
1. UserController
- package com.example.stu.kudestu.stu.controller;
-
-
- import com.example.stu.kudestu.stu.entity.User;
- import com.example.stu.kudestu.stu.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.List;
-
-
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @Autowired
- private UserService userService;
-
- /**
- * 查询所有用户
- * @return
- */
- @RequestMapping("/findAll")
- public List<User> findAll(){
-
- return userService.findAll();
- }
-
- /**
- * 根据id查找用户
- * @param id
- * @return
- */
- @RequestMapping("/findById")
- public User findById(int id){
- return userService.findUserById(id);
- }
-
- /**
- * 用户注册
- * @param user
- * @return
- */
- @RequestMapping(value = "/reg",method = RequestMethod.POST)
- public User reg(User user){
-
- return userService.save(user);
- }
-
- /**
- * 用户登录
- * @param username
- * @param password
- * @return
- */
- @RequestMapping(value = "/login",method = RequestMethod.POST)
- public User login(String username,String password){
- return userService.login(username,password);
- }
-
- /**
- * 用户修改
- * @param user
- * @return
- */
- @RequestMapping(value = "/update",method = RequestMethod.POST)
- public User update(User user){
- return userService.save(user);
- }
-
- /**
- * 用户删除
- * @param id
- */
- @RequestMapping("/delete")
- public void delete(int id){
- userService.delete(id);
- }
-
- /**
- * 分页查询
- * @param page
- * @param response
- * @return
- */
- @RequestMapping("/page")
- public Page<User> findByPage(Integer page, HttpServletResponse response){
-
- //解决跨域请求
- response.setHeader("Access-Control-Allow-Origin","*");
-
- if(page==null || page<=0){
- page = 0;
- }else{
- page -= 1;
- }
-
- return userService.findByPage(page,5);
-
- }
-
- }
2. StudentController
- package com.example.stu.kudestu.stu.controller;
-
-
- import com.example.stu.kudestu.stu.entity.Student;
- import com.example.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.HttpServletResponse;
- import java.util.List;
-
- @RestController
- @RequestMapping("/stu")
- public class StudentController {
-
- @Autowired
- StudentService studentService;
-
- @PostMapping("/add")
- public Student save(Student student){
- return studentService.save(student);
- }
-
- @PostMapping("/update")
- public Student update(Student student){
- return studentService.save(student);
- }
-
- @GetMapping("/delete/{id}")
- public String delete(@PathVariable int id){
- studentService.delete(id);
- return "delete success";
- }
-
-
- @GetMapping("/findById")
- public Student findById(int id){
- return studentService.findStuById(id);
- }
-
- @GetMapping("/findByName/{name}")
- public List<Student> findByName(@PathVariable String name){
-
- return studentService.fingStuByName(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);
-
- }
-
-
-
-
- }
这里使用Postman来测试(前后端分离为了测试Post请求和查看Json数据)下载Postman百度云盘链接 提取码:qe9r
测试StudentControllar,功能已经全部实现和测试过了,下面选一些来展示。下面是一些请求Mapping:
- //学生注册
- @PostMapping("/add")
- //学生修改
- @PostMapping("/update")
- //学生删除
- @GetMapping("/delete/{id}")
- //根据Id查找学生
- @GetMapping("/findById")
- //根据姓名查找学生
- @GetMapping("/findByName/{name}")
- //分页查询所有
- @GetMapping("/query")
1. 测试注册
地址栏输入 http://localhost:8888/kude/stu/add 然后在下面添加参数后点击Send
添加后数据库信息:
2. 测试修改
成功后返回结果
3. 分页查询所有数据
此时表中共有14条数据,按照设定的一页五条数据的话,一共能分为三页。
输入请求地址
返回的所有Json数据如下:
- {
- "content": [
- {
- "id": 1,
- "name": "杜炮",
- "age": 20,
- "sex": "男"
- },
- {
- "id": 2,
- "name": "Lux",
- "age": 16,
- "sex": "女"
- },
- {
- "id": 3,
- "name": "Lux",
- "age": 17,
- "sex": "女"
- },
- {
- "id": 4,
- "name": "Lux",
- "age": 18,
- "sex": "女"
- },
- {
- "id": 5,
- "name": "Klua",
- "age": 20,
- "sex": "男"
- }
- ],
- "pageable": {
- "sort": {
- "sorted": false,
- "unsorted": true,
- "empty": true
- },
- "offset": 0,
- "pageNumber": 0,
- "pageSize": 5,
- "unpaged": false,
- "paged": true
- },
- "totalPages": 3, //分页数
- "totalElements": 14, //数据总条数
- "last": false,
- "number": 0, //当前页数(从零开始)0就是第一页
- "size": 5, //分页大小
- "sort": {
- "sorted": false,
- "unsorted": true,
- "empty": true
- },
- "numberOfElements": 5, //该页的数据条数
- "first": true,
- "empty": false
- }
测试完成,接下来编写前台代码,前后端链接再进行测试,请看下一篇:《从零开始完成一个SpringBoot+JPA的Web案例((三)前端实现)》
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。