第11章—使⽤对象关系映射持久化数据—
SpringBoot+SpringData+Jpa进。。。
SpringBoot+SpringData+Jpa进⾏查询修改数据库
JPA由EJB 3.0软件专家组开发,作为JSR-220实现的⼀部分。但它⼜不限于EJB 3.0,你可以在Web应⽤、甚⾄桌⾯应⽤中使⽤。JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这⼏年的实践探索,能够脱离容器独⽴运⾏,⽅便开发和测试的理念已经深⼊⼈⼼了。Hibernate3.2+、TopLink 10.1.3以及OpenJPA都提供了JPA的实现。
JPA的总体思想和现有Hibernate、TopLink、JDO等ORM框架⼤体⼀致。总的来说,JPA包括以下3⽅⾯的技术:
ORM映射元数据
JPA⽀持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;
API
⽤来操作实体对象,执⾏CRUD操作,框架在后台替代我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。
查询语⾔
这是持久化操作中很重要的⼀个⽅⾯,通过⾯向对象⽽⾮⾯向数据库的查询语⾔查询数据,避免程序的SQL语句紧密耦合。
例⼦:SpringBoot+SpringData+Jpa
这⾥我的Demo的结构图如下:
相关的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.beacon</groupId>
<artifactId>springdata-jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--数据库组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--数据库组件-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--//添加AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Spring boot 引⽤Thymeleaf模板依赖包(Thymeleaf模板如果不适⽤,这⾥也可以不添加这段配置,Thymeleaf模板使⽤在下⾯会讲到) -->        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</project>
StudentController:
ller;
ity.Student;
ample.springdatajpa.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/getAllStu")
public List<Student> getAllStu(){
return studentService.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id")Integer id){
return studentService.findById(id);
}
//根据money查询再根据age排序
@GetMapping("/findByMoney/{money}")
public List<Student> findByMoney(@PathVariable("money")Integer money,Integer age){        return studentService.findByMoneyOrAgeOrderByAgeDesc(money,age);
}
@GetMapping("/findByName/{name}")
public Student findByNameIgnoresCase(@PathVariable("name")String name){
return studentService.findByName(name);
}
}
StudentDao:
ample.springdatajpa.dao;
ity.Student;
import org.springframework.pository.JpaRepository;
import java.util.List;
public interface StudentDao extends JpaRepository<Student,Object> {
Student findById(Integer id);
public List<Student> findByMoneyOrAgeOrderByAgeDesc(Integer money, Integer age) ;
Student findByName(String name);
}
Student:
ity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
private Integer id;
private String name;
private Integer age;
private String password;
private Integer money;
//注意:这⾥⼀定要在主键的get⽅法上加上这两个注解
//  @Id
//  @GeneratedValue
// 不然会报错不到
@Id
@GeneratedValue
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;
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
springboot aop
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
< = money;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", password='" + password + '\'' +
", money=" + money +
'}';
}
}
StudentService
ample.springdatajpa.service;
ity.Student;
import java.util.List;
public interface StudentService {
List<Student> findAll();
Student findById(Integer id);
List<Student> findByMoneyOrAgeOrderByAgeDesc(Integer money, Integer age);
Student findByName(String name);
}
StudentServiceImpl

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。