springjpa(一)普通增刪改查


springjpa如何搭建,前面的文章已經介紹過了。這一篇介紹下如何進行簡單的增刪改查。

student表ddl如下

CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`school_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11123 DEFAULT CHARSET=utf8

 

Student.class

package com.vincent.model;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "student")
@Entity
public class Student {

    @Id
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    private long id;

    private String name;

    private int age;

    private long schoolId;

    public long getId() {
        return id;
    }

    public void setId(long 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 long getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Long schoolId) {
        this.schoolId = schoolId;
    }

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

  

StudentRepository.class

package com.vincent.dao;

import com.vincent.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author 
 * @date 2018/8/17 下午2:09
 */
public interface StudentRepository extends JpaRepository<Student, Long> {

    List<Student> findByNameLike(String name);
   List<Student> findByAgeGreaterThanAndNameEquals(Integer age, String name);
}

  新增

  @Test
    public void testAdd() {
        Student student = new Student();
        student.setAge(12);
        student.setName("abc");
        student.setSchoolId(1L);
        //如果Student在mysql中是自增,但沒有加上@GeneratedValue的注解,那么在新增完成后的student1中的id是0,不是自增后的id
        Student student1 = studentRepository.save(student);
        Assert.assertTrue(student1.getId() >= 0);
    }

  正如代碼中的注釋,新增時有個小注意點:如果entity的id並不是自增,或者id的自增策略並沒有體現在entity的id屬性上,即沒有加@GeneratedValue的注解,那么新增后返回的值的主鍵是沒有新增完后的id的值的。 

   修改

@Test
    public void testUpdate() {
        Student student = new Student();
        student.setAge(121);
        student.setName("cba");
        student.setSchoolId(1L);
        student.setId(111L);
        studentRepository.save(student);
    }

  查詢

@Test
    public void testSelect() {
        //like需要在參數中加"%"
        List<Student> studentList = studentRepository.findByNameLike("%a%");
        System.out.println(studentList);
    }

  查詢的時候如果用了like,那么參數上記得加上“%”,不然sql查詢時是  select 。。。 from 。。。 where  aaa like 'a' 而不是like'%a%'

多條件查詢

    @Test
    public void testSelect1(){
        List<Student> studentList = studentRepository.findByAgeGreaterThanAndNameEquals(22,"cba");
        System.out.println(studentList);
    }

  這種方式優點很明顯,缺點也很明顯。優點就是代碼簡單,只需要在Repository類中新增一條List<Student> findByAgeGreaterThanAndNameEquals(Integer age, String name);即可。缺點也很致命,這種方式的多條件,必須傳多個參數,比如前台頁面只傳了個name,並不需要age的相關條件。那么他就不滿足了。

  所以為了應對這種查詢,下一篇中介紹一個可靈活應對多條件查詢的方法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM