1、在pom.xml中需要添加jpa的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、application.yml中配置数据库连接和jpa的相关属性
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.18/db_sell?charactorEncoding=utf-8&useSSL=false
username: root
password: 123456
jpa:
show-sql: true
3、实体类的使用(ProductCategory类)
import lombok.Data;
import lombok.RequiredArgsConstructor;
import javax.persistence.*;
/**
* 这是商品类目的实体类
*/
@Entity
@Data
public class ProductCategory {
/**
* 类别id
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
/**
* 类别名称
*/
@Column(name = "category_name")
private String categoryName;
/**
* 类别编号
*/
@Column(name = "category_type")
private Integer categoryType;
public ProductCategory() {
}
public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
@Entity注解代表说明这是一个实体类
@GeneratedValue 主键自增策略,有sequence,table,identity和auto四种策略,默认是AUTO,但是我在使用jpa连接mysql的时候一直报增长策略的错误,建议直接设置strategy为对应数据库的增长策略,例如mysql为identity
@Data是使用的lombok插件 它的作用是在编译阶段动态生成成员变量的getset方法和toString方法
@Setter@Getter同时用也能生成gettersetter方法 这两个注解也是lombok的注解
@NoArgsConstructor 动态创建无参构造方法 lombok的注解
4、dao层jpa的基本使用
import com.xaq.entity.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
/**
*
*/
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
/**
* 通过类别编号查询
* @param categoryType
* @return
*/
ProductCategory findByCategoryType(Integer categoryType);
/**
* 通过类别id查询
* @param categoryId
* @return
*/
ProductCategory findByCategoryId(Integer categoryId);
/**
* 通过类别编号范围得到列表
* @param categoryTypes
* @return
*/
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypes);
}
需要继承JpaRepository接口,泛型的第一个参数为绑定的实体类,第二个参数为主键id的类型
默认就可以使用该接口提供的方法,需要动态通过某个参数进行查询的话,可以定义为findBy参数名字,里面的api方法众多,至于排序分页查询jpa接口文档
5、lombok插件安装,这里本机已经安装了,未安装应该显示为install,点击安装
6、添加依赖到pom.xml中
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
7、springboot测试(测试ProductCategoryRepository接口)
package com.xaq.dao;
import com.xaq.entity.ProductCategory;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
/**
* 商品类别dao测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryRepositoryTest {
@Autowired
private ProductCategoryRepository repository;
/**
* 测试保存实体
* @throws Exception
*/
@Test
public void testSave () throws Exception {
ProductCategory productCategory = new ProductCategory("我的天",4);
repository.save(productCategory);
}
/**
* 测试查找所有的类别实体
* @throws Exception
*/
@Test
public void testFindAll() throws Exception {
List<ProductCategory> list = repository.findAll();
for (ProductCategory productCategory : list) {
System.out.println(productCategory);
}
}
/**
* 测试通过类别编号查询
* @throws Exception
*/
@Test
public void testFindByCategoryType() throws Exception {
ProductCategory productCategory = repository.findByCategoryType(4);
log.info(productCategory.toString());
}
/**
* 测试通过类别id查询
* @throws Exception
*/
@Test
public void testFindByCategoryId() throws Exception {
ProductCategory productCategory = repository.findByCategoryId(1);
ProductCategory productCategory1 = repository.findById(1).get();
log.info(productCategory.toString());
log.info(productCategory1.toString());
}
/**
* 测试通过一个返回的类别编号查询
* @throws Exception
*/
@Test
public void testFindByCategoryTypeIn() throws Exception {
List<Integer> list = Arrays.asList(1,2,3,4);
List<ProductCategory> resultList = repository.findByCategoryTypeIn(list);
for(ProductCategory productCategory : resultList){
log.info(productCategory.toString());
}
}
}
@Slf4J 是lombok的注解,它的性质相当于
private static final Logger log= LoggerFactory.getLogger(this.getClass())
使用了这个注解直接可以使用log.info() 或者其它级别的方法