SpringBoot+JPA学生管理系统项目开发(Java后台)

1:创建SpringBoot项目,在IDEA上进行项目的创建。创建过程和maven项目是不一样的。

SpringBoot+JPA学生管理系统项目开发(Java后台)_第1张图片

SpringBoot+JPA学生管理系统项目开发(Java后台)_第2张图片

 
 

这一步就和maven项目创建时要填写的项目的名称就一样啦,名称随便你定。我是在Group:com.kude.stu    Artifact:kudestu      这样写的,你也可以和我的一样。

SpringBoot+JPA学生管理系统项目开发(Java后台)_第3张图片

SpringBoot+JPA学生管理系统项目开发(Java后台)_第4张图片

SpringBoot+JPA学生管理系统项目开发(Java后台)_第5张图片

这个项目不需要太多配置,嘿嘿,是因为比较简单。看着我上面的配置,其他的在这个项目中一律不用配置。

SpringBoot+JPA学生管理系统项目开发(Java后台)_第6张图片

Java代码项目文件中这样写,一定要和我一样。

SpringBoot+JPA学生管理系统项目开发(Java后台)_第7张图片

resources和java目录创建好之时,它们的颜色都已经变啦!

项目最主要的就是pom.xml和application.properties文件的配置,接下来我把配置好的文件写在下面,你们把我的复制过去就OJ8K啦!!!

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.kude.stu
    kudestu
    0.0.1-SNAPSHOT
    kudestu
    Demo project for Spring Boot

    
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.alibaba
            druid
            1.1.10
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.10
        



    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.application.properties


#数据源配置

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/wowoblog?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai

spring.datasource.username=root

spring.datasource.password=

spring.datasource.initialSize=20

spring.datasource.minIdle=50

spring.datasource.maxActive=500



#上下文配置

server.port=8888

server.servlet.context-path=/kude



#配置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

这里有你们要连接的数据库名,和连接数据库的账号的密码,你要自己参考我的修改一下欧。

3.StudentContriller

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 findByName(@PathVariable String name){
        return studentService.findStuByName(name);
    }

    @GetMapping("/query")
    public Page 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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 处理用户信息的控制层
 * @author 潘亚
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户的方法
     */
    @RequestMapping("/findAll")
    public List 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 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 findStudentById(Integer id);

    @Query(name = "findStuByName",nativeQuery = true,value =
            "select * from student where name=:name")
    List 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 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.entity;

import javax.persistence.*;

@Entity
@Table(name = "student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private int age;
    private String sex;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    public Student() {
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

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 findStuByName(String name);

    /**
     * 分页查询所有数据
     * @param page 当前页
     * @param pageSize 每页记录数
     * @return
     */
    Page findAll(int page,int pageSize);

}

StudentServicelmpl

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 findStuByName(String name) {
        return studentDao.findStuByName(name);
    }

    @Override
    public Page 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 findAll();
    User findUserById(Integer id);
    User save(User user);
    User update(User user);
    void delete(int id);
    User login(String username,String password);
    Page findByPage(int page, int limit);


}

UserServicelmpl

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 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 findByPage(int page, int pageSize) {
        Pageable pageable = PageRequest.of(page,pageSize,new Sort(Sort.Direction.ASC,"id"));
        return userDao.findAll(pageable);
    }
}

想看前台代码,下篇博客见

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(RJ)