什么是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
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql://localhost:3306/ssmbuild?userUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC#jpa额外的
#把sql打印出来
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.database-platform=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
实体层:
Book实体默认下创建的数据库表名字就是Book,也可以指定表名
注意: 通过这种方式创建的表必须要指定主键
@Id
主键自增长
@GeneratedValue
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 {@AutowiredBookDao bookDao;@Testvoid contextLoads() {Book book=new Book();book.setName("三演义");book.setAuthor("罗贯中");bookDao.save(book);}@Testpublic void update(){Book book=new Book();book.setAuthor("luozhuanzhong");book.setName("sanguoyanli");book.setId(1);bookDao.saveAndFlush(book);}@Testpublic void delete(){bookDao.deleteById(1);}@Testpublic void find(){Optional<Book> byId=bookDao.findById(1);System.out.println(byId.get());List<Book> all=bookDao.findAll();System.out.println(all);}}
二:springData jpa自定义方法:
有一定的规则
public interface BookDao extends JpaRepository<Book, Integer> {//自定义方法Book findBookById(Integer id);
}
三:自定义查询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//因为是修改所以要加上事务@TransactionalInteger addBook1(String name,String author);@Query(value="insert into book(name,author) values (:name ,:author)",nativeQuery = true)//因为是修改的sql语句,所以要加上Modifying注解@Modifying//因为是修改所以要加上事务@TransactionalInteger addBook2(@Param("name") String name, @Param("author")String author);
}