• String data jpa执行的增删改查


    entity实体类:

    package com.qzy.entity;

    import javax.persistence.*;

    /**
    * Created by cmy on 2019/12/5.
    */
    @Entity
    @Table(name="studentinfo")
    public class Student {
    @Id
    @GeneratedValue //默认让它自增
    private Integer id;

    @Column(name="stu_name")
    private String stuname;

    private Integer sex;

    private Integer age;

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getStuname() {
    return stuname;
    }

    public void setStuname(String stuname) {
    this.stuname = stuname;
    }

    public Integer getSex() {
    return sex;
    }

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

    public Integer getAge() {
    return age;
    }

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

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

    public Student() {
    }
    }


    dao接口:
    package com.qzy.dao;

    import com.qzy.entity.Student;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Query;

    import java.util.List;

    /**
    * Created by cmy on 2019/12/5.
    */
    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);

    }
    application.properties配置:

    #配置数据库的四个连接参数
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3307/account
    spring.datasource.username=root
    spring.datasource.password=root


    #Spring Data JPA的配置
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    spring.jackson.serialization.indent-output=true
    spring.jpa.database=mysql
    Contorller层:
    package com.qzy.controller;

    import com.qzy.dao.StudentDao;
    import com.qzy.entity.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

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

    /**
    * Created by cmy on 2019/12/5.
    */
    @Controller //@Controller+@ResponseBody
    public class StudentController {

    @Autowired
    StudentDao dao;

    @RequestMapping("/saveAllStudent")
    public String saveAllStudent(){
    //Hibernate:瞬时状态 游离状态 持久状态
    List<Student> students = Arrays.asList(new Student("张三", 1, 4),
    new Student("李四", 0, 3),
    new Student("王五", 1, 4));
    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+"%");
    }

    }
    pom.xml:
    <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>
    目录:















































  • 相关阅读:
    Python pip – error: invalid command ‘bdist_wheel’
    css实现文字两端对齐(兼容所有浏览器)
    webpack中的require.context
    node 的path模块中 path.resolve()和path.join()的区别
    react-native项目中遇到的问题
    react-native针对android改变状态栏样式
    createBottomTabNavigator: 怎么在切换tab的时候让页面重新渲染
    当vue页面异步加载的数据想在页面上渲染怎么办
    git分布式版本控制系统
    $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别
  • 原文地址:https://www.cnblogs.com/qinzhenyu/p/11990795.html
Copyright © 2020-2023  润新知