步骤
(1)在pom中添加mysql,spring-data-jpa依赖;
<!--添加mysql数据库驱动依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--添加spring-data-jpa依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
(2)在application.properties文件中配置mysql连接配置文件;
########################################################
###datasource -- 指定mysql数据库连接信息.
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = Root@123
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)在application.properties文件中配置JPA配置信息;
########################################################
### Java Persistence Api -- Spring jpa配置信息
########################################################
# 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)编写实体类
如何实现持久化呢??
a.使用@entity进行实体类的持久化操作,当JPA检测到我们的实体类由@Entity注解的时候,会在数据库中生成对应的表结构信息。
如何指定逐渐以及主键的生成策略?
b.使用@Id指定主键。
package com.huawei.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* 创建一个实体类。
* 如何实现持久化呢??
* 1.使用@entity进行实体类的持久化操作,当JPA检测到我们的实体类由@Entity注解的时候,会在数据库中生成对应的表结构信息。
* 如何指定逐渐以及主键的生成策略?
* 2.使用@Id指定主键。
*/
@Entity
public class Cat
{
/**
* 使用注解@Id指定主键
* 使用@GeneratedValue指定主键生成策略
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String catName;
private int catAge;
public Cat(int id, String catName, int catAge)
{
this.id = id;
this.catName = catName;
this.catAge = catAge;
}
public Cat(String catName, int catAge)
{
this.catName = catName;
this.catAge = catAge;
}
public Cat()
{
}
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 int getCatAge()
{
return catAge;
}
public void setCatAge(int catAge)
{
this.catAge = catAge;
}
@Override
public String toString()
{
return "Cat{" +
"id=" + id +
", catName='" + catName + '\'' +
", catAge=" + catAge +
'}';
}
}
此时运行app.java就会创建对用的表
(5)Repository类
package com.huawei.repository;
import com.huawei.bean.Cat;
import org.springframework.data.repository.CrudRepository;
public interface CatRepository extends CrudRepository<Cat, Integer>
{
}
(6)service类
package com.huawei.service;
import com.huawei.bean.Cat;
import com.huawei.repository.CatRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
@Service
public class CatService
{
@Resource
private CatRepository catRepository;
@Transactional
public void save(Cat cat) {
catRepository.save(cat);
}
public void delete(int id) {
catRepository.deleteById(id);
}
public Iterable<Cat> getAll() {
return catRepository.findAll();
}
}
(7)controller类
package com.huawei.controller;
import com.huawei.bean.Cat;
import com.huawei.service.CatService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/cat")
public class CatController
{
@Resource
private CatService catService;
@RequestMapping(value = "/save")
public String save() {
Cat cat = new Cat();
cat.setCatName("jack");
cat.setCatAge(3);
catService.save(cat);
return "save ok.";
}
@RequestMapping("/delete")
public String delete(){
catService.delete(1);
return "delete ok";
}
@RequestMapping("/getAll")
public Iterable<Cat> getAll(){
return catService.getAll();
}
}
知识点讲解:
Repository接口
Repository接口是Spring data的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法:
public interface Repository<T, ID>
PagingAndSortingRepository接口
该接口提供了分页与排序功能
Iterable<T> findAll(Sort var1);
Page<T> findAll(Pageable var1);
定义接口实现Repository接口