Springboot-data-jpa CRUD

Import dependence

<?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 https://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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cmy</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-data-jpa</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-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

DataJpaApplication class

package com.cmy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DataJpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(DataJpaApplication.class, args);
    }
}

StudentDao class

package com.cmy.dao;

import com.cmy.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface StudentDao extends JpaRepository<Student,Integer>{
    @Query(value="select * from studentinfo where stu_name=?1",nativeQuery = true)
    Student findByStuname(String stuname);
    List<Student> findByStunameLike(String stuname);

}

Student category

import javax.persistence.*;

@Entity
@Table(name = "studentinfo")
public class Student {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
    @Column(name = "stu_name")

    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 Integer getSex() {
        return sex;
    }

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

    public Student(Integer id, String name, Integer age, Integer sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

StudentController class

import com.cmy.dao.StudentDao;
import com.cmy.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Controller //@Controller+@ResponseBody
public class StudentController {

    @Autowired
    StudentDao dao;

    @RequestMapping ( " / saveAllStudent " )
     public String saveAllStudent () {
         // the Hibernate: transient state free state persistent state 
        List <Student> = Students. Arrays.asList ( new new Student ( " Seville East Columbia " , . 1 , 24 ),
                 new new Student ( " Lixian Yu " , 0 , 23 is ),
                 new new Student ( " Xiongge " , 1 , 24 ));
        dao.saveAll(students);
        // Save (T T): increase in a single record to the database
         // saveAll (the Iterable I): increasing the plurality of recording
         // saveAndFlush (): the cache is refreshed and increase 
        return  " Success " ;
    }

    @RequestMapping ( " / deleteYeShou " )
     public String deleteYeShou () {
         // dao.deleteById (. 4); a single recorded by deleting the ID 
        Student yeshou1 = new new Student ();
        yeshou1.setId ( . 1 );
        Student yeshou2=new Student();
        yeshou2.setId ( . 3 );
        dao.deleteAll (Arrays.asList (yeshou1, yeshou2)); // batch delete 
        return  " Success " ;
    }

    @RequestMapping("/modifyBeantifulGirl")
    public String modifyStudent(){
        One Student = dao.getOne ( . 9 ); // retrieve a single record method 
        one.setAge ( 18 is );
        one.setStuname("玉姐");
        dao.save (One); // modify the action data 
        return  " Success " ;
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public Object getAll(){
        return dao.findAll();
    }
    
    @RequestMapping("/getByName")
    @ResponseBody
    public Object getByName(){
        return dao.findByStuname("玉姐");
    }

    @RequestMapping("/getLike")
    @ResponseBody
    public Object getByName(String name){
        return dao.findByStunameLike("%"+name+"%");
    }

    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPage(@RequestParam(required = false,defaultValue = "1")Integer pageindex){
        PageRequest pageRequest=new PageRequest(pageindex-1,2, Sort.Direction.DESC,"id");

        return dao.findAll(pageRequest);
    }

}

success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript"></script>
</head>
<body>
    Join the success of Beauty and the Beast
</body>
</html>

application.properties

# Set the four parameters of the database connection
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8080/account
spring.datasource.username=root
spring.datasource.password=123

#Spring Data JPA configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true
spring.jpa.database=mysql

DataJpaApplicationTests

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DataJpaApplicationTests {
    @Test
    public void contextLoads() {
    }
}

 

Guess you like

Origin www.cnblogs.com/dabrk/p/11996392.html