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>
目录: