spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化

时间:2023-03-08 18:07:31

SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的)

1在pom.xml引入mysql, spring-data-jpa依赖

2.在src/main/resource/下新建applicatoin.properties配置文件,并配置数据库连接

3.在application.properties配置jpa配置信息

4.编写实例

热部署pom.xml配置

<!-- spring boot devtools 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>

  

<!-- spring boot devtools的plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin>

  

1.加入依赖

 <!-- mysql数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 添加spring-boot-data-jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

  

2.applicaton.properties配置mysql

########################################################
###datasource mysql
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

  

3.配置jpa

########################################################
### Java Persistence Api
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

  

4.测试实例:

Spring Boot JPA 总结
---------------------
1、需要添加相应的依赖包;
2、需要在application.properties文件添加配置信息;
3、需要创建一个实体类,比如Cat;
4、需要创建一个接口继承CrudRepository;
5、需要创建一个Service;
6、需要创建一个Controller;
7、代码测试;

cat.java

package com.muyang.boot1.demo.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; /**
* 创建一个实体类
* 如何持久化
* 1.使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类中有@Entity的 注解的时候
* 会在数据库中生成对应的表结构
* 2.如何制定主键及生成策略
* 使用@id来生成主键
* @author Administrator
*
*/
@Entity
public class Cat { /**
* 使用@Id生成主键
* 受用@GeneratedValue(strategy=GenerationType.AUTO)指定主键测策略,即mysql的自增Id
*/
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id; private String catName; private String catAge; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCatName() {
return catName;
} public void setCatName(String catName) {
this.catName = catName;
} public String getCatAge() {
return catAge;
} public void setCatAge(String catAge) {
this.catAge = catAge;
} }

  

catRepository.java

package com.muyang.boot1.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.muyang.boot1.demo.bean.Cat;

public interface CatRepository extends CrudRepository<Cat, Integer> {

}

  

catService.java增删改查

package com.muyang.boot1.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.muyang.boot1.demo.bean.Cat;
import com.muyang.boot1.demo.repository.CatRepository; @Service
public class CatService { @Resource
private CatRepository catRepository; /**
*
* update, save, delete操作
*/ //保存数据
public void save(Cat cat)
{
catRepository.save(cat);
} //删除数据
public void delete(int id)
{
catRepository.delete(id);
} //查询
public Iterable<Cat> getAll() {
return catRepository.findAll();
} }

  

catController.java控制器

package com.muyang.boot1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.muyang.boot1.demo.bean.Cat;
import com.muyang.boot1.demo.service.CatService; @RestController
@RequestMapping("/cat")
public class CatController { @Autowired
private CatService catService; //增加
@RequestMapping(value="/add")
public String save()
{
Cat cat = new Cat();
cat.setCatName("大五");
cat.setCatAge("22");
catService.save(cat);
return "save ok.";
} @RequestMapping("/del")
public String delete(int id)
{
catService.delete(id);
return "delete ok.";
} @RequestMapping(value="/getAll", produces="application/json; charset=utf-8")
public Iterable<Cat> getAll()
{
return catService.getAll();
} }

  

http://localhost:8080/cat/add

http://localhost:8080/cat/getAll