• Springboot-data-jpa增删改查


    导入依赖

    <?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类

    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类

    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类

    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类

    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(){
            //Hibernate:瞬时状态 游离状态 持久状态
            List<Student> students = Arrays.asList(new Student("利亚东哥", 1, 24),
                    new Student("李先玉", 0, 23),
                    new Student("雄哥", 1, 24));
            dao.saveAll(students);
            //save(T t):向数据库中增加单条记录
            //saveAll(Iterable i):增加多条记录
            //saveAndFlush():增加并刷新缓存
            return "success";
        }
    
        @RequestMapping("/deleteYeShou")
        public String deleteYeShou(){
            //dao.deleteById(4); 通过ID删除单条记录
            Student yeshou1=new Student();
            yeshou1.setId(1);
            Student yeshou2=new Student();
            yeshou2.setId(3);
            dao.deleteAll(Arrays.asList(yeshou1,yeshou2)); //批量删除
            return "success";
        }
    
        @RequestMapping("/modifyBeantifulGirl")
        public String modifyStudent(){
            Student one = dao.getOne(9); //检索单条记录的方法
            one.setAge(18);
            one.setStuname("玉姐");
            dao.save(one); //修改数据的作用
            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>
        加入成功 美女与野兽
    </body>
    </html>

    application.properties

    #配置数据库的四个连接参数
    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的配置
    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() {
        }
    }
  • 相关阅读:
    Jocke的IOT之路--raspberrypi更换国内镜像
    利用PostMan 模拟上传/下载文件
    Java中的Lambda表达式
    设计模式之Jdk动态代理
    设计模式之代理模式
    Java内存模型及Java关键字 volatile的作用和使用说明
    JVM GC-----4、finalize()方法
    JVM GC-----3、垃圾对象的标记思路(二)
    JVM GC-----2、垃圾对象的标记思路(一)
    JVM GC-----1、垃圾回收算法
  • 原文地址:https://www.cnblogs.com/dabrk/p/11996392.html
Copyright © 2020-2023  润新知