什么是Spring Data:
Spring Data是一个用于简化数据库访问,支持云服务的开源框架,目标是使数据库访问变得方便快捷
SpringData是数据库开源框架,包含关系数据库和非关系数据库
什么是Jpa:
JPA是java的标准,不是Spring标准
JPA其实就是java实体对象和关系型数据库建立起映射关系,通过面向对象编程的思想操作关系型数据库的规范
一、 默认整合
在Springboot默认配置下添加新的Maven依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.21</version>
</dependency>
配置数据库:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
url: jdbc:mysql://localhost:3306/test_dev?autoReconnect=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: 123456
jpa:
hibernate:
# 实体类发生改变,数据表会更新
ddl-auto: update
# 操作实体对象的时候,会跟我们生成sql语句: false 不生成sql语句:
show-sql: true
实体层:
Book实体默认下创建的数据库表名字就是Book,也可以指定表名
注意: 通过这种方式创建的表必须要指定主键
package com.example.demo.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
//表示为主键
@Id
//自增长
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String author;
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 String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book() {
}
}
BookDao
package com.example.demo.dao;
import com.example.demo.bean.Book;
import org.springframework.data.jpa.repository.JpaRepository;
//@Repository
//第一个参数表示要操作实体的类型,第二个参数表示id的参数类型
public interface BookDao extends JpaRepository<Book, Integer> {
}
测试类:
@SpringBootTest
class DemoApplicationTests {
@Autowired
BookDao bookDao;
@Test
void contextLoads() {
Book book=new Book();
book.setName("三演义");
book.setAuthor("罗贯中");
bookDao.save(book);
}
@Test
public void update(){
Book book=new Book();
book.setAuthor("luozhuanzhong");
book.setName("sanguoyanli");
book.setId(1);
bookDao.saveAndFlush(book);
}
@Test
public void delete(){
bookDao.deleteById(1);
}
@Test
public void find(){
Optional<Book> byId=bookDao.findById(1);
System.out.println(byId.get());
List<Book> all=bookDao.findAll();
System.out.println(all);
}
}
二:springData jpa自定义方法:
自定义查询sql语句:
public interface BookDao extends JpaRepository<Book, Integer> {
//自定义sql语句
// 注:nativeQuery=true代表使用sql语句查询,默认使用JPA ql查询
@Query(value="select * from book where id=(select max(id) from book)",nativeQuery = true)
Book getMaxIdBook();
}
自定义数据修改sql语句:
public interface BookDao extends JpaRepository<Book, Integer> {
@Query(value="insert into book(name,author) values (?1,?2)",nativeQuery = true)
//因为是修改的sql语句,所以要加上Modifying注解
@Modifying
//因为是修改所以要加上事务
@Transactional
Integer addBook1(String name,String author);
@Query(value="insert into book(name,author) values (:name ,:author)",nativeQuery = true)
//因为是修改的sql语句,所以要加上Modifying注解
@Modifying
//因为是修改所以要加上事务
@Transactional
Integer addBook2(@Param("name") String name, @Param("author")String author);
}