添加pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
下载插件
image.png
简化过后
package com.example.demo.dataobject;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
/**
* 类目表 product_category
*
*/
//映射
@Entity
@DynamicUpdate //更新时间
@Data //lombok简化 get set
public class ProductCategory {
//类目id
@Id
@GeneratedValue
private Integer categoryId;
//类目名字
private String categoryName;
//类目编号
private Integer categoryType;
// //时间
// private Date createTime;
// private Date updateTime;
//构造方法
ProductCategory(String categoryName,Integer categoryType){
this.categoryType=categoryType;
this.categoryName=categoryName;
}
}
测试查询
package com.example.demo.repository;
import com.example.demo.dataobject.ProductCategory;
import com.example.demo.repository.ProductCategoryRepository;
import org.junit.Assert;
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.test.context.junit4.SpringRunner;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
@Autowired
private ProductCategoryRepository repository;
//查找
@Test
public void findOneTest() {
ProductCategory productCategory = repository.findById(1).get();
System.out.println(productCategory.toString());
}
//新增
@Test
@Transactional //测试里面就是完全回滚 做完以后回滚
public void saveTest(){
ProductCategory productCategory=new ProductCategory("男生最爱",5);
ProductCategory result= repository.save(productCategory);
Assert.assertNotNull(result);
}
// 更新
@Test
public void updateTest(){
//先查出来
ProductCategory productCategory = repository.findById(2).get();
productCategory.setCategoryType(3);
repository.save(productCategory);
}
// 根据类目编号查询所有类目
@Test
public void findByCategoryTypeIn(){
List<Integer> list=Arrays.asList(0,3);
List<ProductCategory> result = repository.findByCategoryTypeIn(list);
Assert.assertNotEquals(0,result.size()); //判断如果等于0 则报错打印
}
}
jpa的接口
package com.example.demo.repository;
import com.example.demo.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
//后面integer是主见
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
//一次性查询 通过类目 查询所有
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
网友评论