1.3 新建项目

1.引入的依赖包

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

2.application.yml 中引入数据源

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 
    url: jdbc:mysql://localhost:3306/sell?serverTimezone=GMT%2B8&characterEncoding=utf-8&userSSL=false
  jpa:
    show-sql: true

3.实体类

@Entity
@DynamicUpdate  // 动态自动更新时间
@Proxy(lazy=false) // 懒加载设置为false
public class ProductCategory { 
    @Override
    public String toString() {
        return "ProductCategory{" +
                "categoryId=" + categoryId +
                ", categoryName='" + categoryName + '\'' +
                ", categoryType=" + categoryType +
                '}';
    }

    //    类目
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
    //    类目名字
    private String categoryName;

    //    类目编号
    private Integer categoryType;


    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;
    }
}

4.dao 接口 继承jpa

public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
}

5.测试用例

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryRepositoryTest {

    @Autowired
    private ProductCategoryRepository repository;

    @Test
    public void findOneTest() {
        Optional<ProductCategory> producCategory = repository.findById(1);
        log.info(producCategory.toString());
    }
}

 

posted @ 2018-11-05 05:17  cloud_shi  阅读(168)  评论(0编辑  收藏  举报