Use springboot + MySQL + JPA to add, delete, change, query and page the database

mb60f93b6c4c5e3 2021-07-28 21:31:20
use springboot mysql jpa add


Use SpringBoot+Mysql+JPA Add, delete, modify, query and paging the database

       JPA yes Java Persistence API For short , Chinese name Java Persistence layer API, yes JDK 5.0 Note or XML Describe objects - Mapping of relational tables , And persist the running entity object into the database .
Use Springboot and jpa When operating the database , Can greatly reduce our workload , stay jpa in , The function of adding, deleting and checking has been encapsulated in the bottom layer sql sentence , It can make us develop quickly

Project process

One 、 Create a new project

 

 

 

 

 

 

 

  Two 、 The configuration file

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Data source configuration
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.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
 
# Context configuration
server.port= 8888
server.servlet.context-path=/stu
 
# To configure jpa
# Help us automatically generate table structure
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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
 
         <!-- druid Database connection pool -->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid</artifactId>
             <version> 1.1 . 10 </version>
         </dependency>
         <!-- https: //mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
         <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper-spring-boot-starter</artifactId>
             <version> 1.2 . 10 </version>
         </dependency>
 
 
     </dependencies>
 
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
</project>

3、 ... and 、Mysql database

Create a new database stu, establish student Table and user surface

 

 

 

Four 、 The backend implementation

 

 

 

 

 student Entity class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package  com.kude.stu.kudestu.stu.entity;
 
import  javax.persistence.*;
import  javax.xml.soap.Name;
 
@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  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;
     }
 
     public  Student() {
     }
}

user Entity class

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

 

StudentDao Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 findStuById(Integer id);
         @Query (name =  "findStuByName" ,nativeQuery =  true ,value =  "select * from student where name=:name " )
         List<Student> findStuByName( @Param ( "name" ) String name);
}

UserDao Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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> {
     /**
      * according to id Inquire about
      */
     User findUserById( int  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);
}

 

StudentService Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 id);
     Student findStuById(Integer id);
     List<Student> findStuByName(String name);
 
     /**
      * Paging all data
      * @param page  The current page
      * @param pageSize  Records per page
      * @return
      */
     Page<Student> findAll( int  page, int  pageSize);
 
}

StudentServiceImpl Implementation class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 id) {
         studentDao.deleteById(id);
     }
 
     @Override
     public  Student findStuById(Integer id) {
         return  studentDao.findStuById(id);
     }
 
     @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 Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
package  com.kude.stu.kudestu.stu.service;
 
import  com.kude.stu.kudestu.stu.entity.User;
 
import  java.util.List;
 
public  interface  UserService {
     List<User> findAll();
     User findUserById( int  id);
     User save(User user);
     User update(User user);
     User login(String username,String password);
}

UserServiceImpl Implementation class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.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( int  id) {
         return  userDao.findUserById(id);
     }
 
     @Override
     public  User save(User user) {
         return  userDao.save(user);
     }
 
     @Override
     public  User login(String username, String password) {
         return  userDao.login(username,password);
     }
 
     @Override
     public  User update(User user) {
         return  null ;
     }
}

 

StudentController class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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.HttpServletResponse;
import  java.util.List;
 
@RestController
@RequestMapping ( "/s" )
public  class  StudentController {
     @Autowired
     private  StudentService studentService;
 
     /**
      * Add student
      * @param student  Student objects to add
      * @return
      */
     @PostMapping ( "/add" )
     public  Student save(Student student){
         return  studentService.save(student);
     }
 
     /**
      * Revise students
      * @param student  The student object to be modified
      * @return
      */
     @PostMapping ( "/update" )
     public  Student update(Student student){
         return  studentService.save(student);
     }
 
     /**
      * Delete students
      * @param id To delete the id
      */
     @GetMapping ( "/del/{id}" )
     public  String del( @PathVariable  Integer id){
         studentService.delete(id);
         return  "yes" ;
     }
 
     /**
      * Search for student information by name
      * @param name Name of the student to be inquired
      * @return
      */
     @GetMapping ( "/findByName/{name}" )
     public  List<Student> findStuByName( @PathVariable  String name){
         return  studentService.findStuByName(name);
     }
 
     /**
      * Paging query
      * @param page
      *
      * @return
      */
     @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 class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.bind.annotation.RestController;
 
import  java.util.List;
 
/**
  * The control layer that processes user information
  * @author Zhang Yuxian
  * */
@RestController
@RequestMapping ( "/user" )
public  class  UserController {
 
     @Autowired
     private  UserService userService;
 
     /**
      * How to query users
      *
      */
     @RequestMapping (value =  "/findAll" ,method = RequestMethod.POST)
     public  List<User> findAll(){
         return  userService.findAll();
     }
 
     /**
      * according to id Query the user
      */
 
     @RequestMapping (value =  "/findById" )
     public  User findUserById( int  id){
         return  userService.findUserById(id);
     }
 
     /**
      * Registered users
      */
     @RequestMapping (value =  "/reg" ,method = RequestMethod.POST)
     public  User reg(User user){
         return  userService.save(user);
     }
 
     /**
      * The user login
      */
     @PostMapping ( "/login" )
     public  User login(String username,String password){
         return  userService.login(username,password);
     }
 
}

5、 ... and 、 Front end paging implementation

 

 

  Create a new project

 

Baidu search rookie tutorial and open

 

 

 

 

stay js Create a new one in the directory jquery.js

 

Copy and paste everything into js In the catalog jquery.js Save in

 

 index.html

add.html

6、 ... and 、 Page shows

 

 

 

7、 ... and 、Postman test

 

User test

 

 

 

 

 

 

 

 

 

 

Student test

 

 

 

 

 

 

 

 

 

 

 
 

 

 

 

 

 

All the tests passed , Very happy , Very excited !!!!

 

版权声明
本文为[mb60f93b6c4c5e3]所创,转载请带上原文链接,感谢
https://javamana.com/2021/07/20210723173317438U.html

  1. redis 监控的一些构思
  2. Some ideas of redis monitoring
  3. Spring中获取request的几种方法,及其线程安全性分析
  4. Several methods of getting request in spring and its thread security analysis
  5. 20分钟带你掌握JavaScript Promise和 Async/Await
  6. 20 minutes with async and async
  7. Mybatis - introduction, simple entry program
  8. Java Concurrent Programming: concurrent HashMap
  9. Mybatis: one to one association query
  10. Java IO
  11. Object oriented [day07]: review of knowledge points (11)
  12. Chapter 2 spring
  13. Process of building minicube in local k8s environment
  14. Interpretation of jQuery source code
  15. Making web full screen effect with jquery
  16. Java 7 exception handling new features - address suppressed () method
  17. Spring 4 uses websocket
  18. Using openldap to answer questions and using java to complete LDAP authentication
  19. Implementation of MySQL high availability cluster with corosync + pacemaker and DRBD
  20. Java production of a simple tank war
  21. Design pattern day02
  22. Hadoop pseudo Distributed installation and running test example
  23. Java Concurrent Programming: callable, future and futuretask
  24. Understanding the three characteristics of Java encapsulation
  25. Understanding closure of JavaScript
  26. Java set excel worksheet to read only
  27. Distributed Java service platform baratine
  28. Java barcode generation technology barcode4j
  29. Distributed services framework Dubbo / dubbox getting started example
  30. Zookeeper notes (1) installation, deployment and hello world
  31. Oracle database trigger Usage Summary
  32. MySQL master replication with slave server