第一点配置数据库
image.png
第二点:pom.xml配置
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
第三点新建jpa
package com.example.demo.dataobject;
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 //更新时间
public class ProductCategory {
//类目id
@Id
@GeneratedValue
private Integer categoryId;
//类目名字
private String categoryName;
//类目编号
private Integer categoryType;
//时间
private Date createTime;
private Date updateTime;
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getCategoryType() {
return categoryType;
}
public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ProductCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryType=" + categoryType +
'}';
}
}
第四点新建 接口 jpa
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 org.springframework.transaction.annotation.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
public void saveTest(){
ProductCategory productCategory=new ProductCategory();
productCategory.setCategoryName("女生最爱");
productCategory.setCategoryType(3);
repository.save(productCategory);
}
// 更新
@Test
public void updateTest(){
ProductCategory productCategory=new ProductCategory();
productCategory.setCategoryId(2);
productCategory.setCategoryName("男生最爱");
productCategory.setCategoryType(2);
repository.save(productCategory);
}
}
网友评论