Spring Boot 学习之路 微信点餐 类目表创建

1.配置pom.xml文件    
<!--配置Mysql 属性 -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>
<!--配置hibernate JPA属性 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--配置LomBok 属性-->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.16.20</version>
   <scope>provided</scope>
</dependency>

2.配置application.yml文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  servlet:
    context-path: /sell
3.配置ProductCategory.java 文件 【GET SET toString】
package com.imooc.sell.dataobject;

import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

/**
 * 类目
 * @author GXX
 * @data 2018/7/10 0:54
 * s_product_category
 */
@Entity
/** 修改时间属性. */
@DynamicUpdate
@Data
public class ProductCategory {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    /**类目id. */
    private Integer categoryId;
    /**类目名称. */
    private String categoryName;
    /**类目属性. */
    private Integer categoryType;

    public ProductCategory() {

    }
    public ProductCategory(String categoryName, Integer categoryType) {
        this.categoryName = categoryName;
        this.categoryType = categoryType;
    }
}

4.配置ProductCategoryRepository.java 文件 【Dao】
package com.imooc.sell.repository;

import com.imooc.sell.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author GXX
 * @data 2018/7/10 0:57
 */
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer>{
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categroyTypeList);
}

5.生成ProductCategoryRepositoryTest.java 测试类【Dao 测试类】
package com.imooc.sell.repository;

import com.imooc.sell.dataobject.ProductCategory;
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.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author GXX
 * @data 2018/7/10 0:58
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
        @Autowired
        private ProductCategoryRepository repository;

        @Test
        public void findOneTest(){
           ProductCategory category = repository.findById(1).orElse(null);
            System.out.println(category.toString());
        }
        @Test
        @Transactional
        public void saveTest(){
            ProductCategory productCategory = new ProductCategory("男生最爱",5);
            ProductCategory result = repository.save(productCategory);
            Assert.assertNotNull(result);
        }
        @Test
        public void findByCategoryTypeInTest(){
            List<Integer> list = Arrays.asList(1,4,5);
            List<ProductCategory> result = repository.findByCategoryTypeIn(list);
            Assert.assertNotEquals(0,result.size());
        }
}

6.生成CategoryService 文件【service】
package com.imooc.sell.service;

import com.imooc.sell.dataobject.ProductCategory;

import java.util.List;

/**
 * 类目 服务端
 * @author GXX
 * @data 2018/7/11 0:13
 */
public interface CategoryService {

    ProductCategory findById(Integer categoryId);

    List<ProductCategory> findAll();

    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);

    ProductCategory save(ProductCategory productCategory);

}

7.生成CategoryServiceImpl 文件【serviceImpl】
package com.imooc.sell.service.impl;

import com.imooc.sell.dataobject.ProductCategory;
import com.imooc.sell.repository.ProductCategoryRepository;
import com.imooc.sell.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类目 服务端接口
 * @author GXX
 * @data 2018/7/11 0:16
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ProductCategoryRepository repository;

    @Override
    public ProductCategory findById(Integer categoryId) {
        return repository.findById(categoryId).orElse(null);
    }

    @Override
    public List<ProductCategory> findAll() {
        return repository.findAll();
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return repository.findByCategoryTypeIn(categoryTypeList);
    }

    @Override
    public ProductCategory save(ProductCategory productCategory) {
        return repository.save(productCategory);
    }


}

8.生成CategoryServiceImplTest 文件【serviceImpl 测试类】
package com.imooc.sell.repository;

import com.imooc.sell.dataobject.ProductCategory;
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.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author GXX
 * @data 2018/7/10 0:58
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
        @Autowired
        private ProductCategoryRepository repository;

        @Test
        public void findOneTest(){
           ProductCategory category = repository.findById(1).orElse(null);
            System.out.println(category.toString());
        }
        @Test
        @Transactional
        public void saveTest(){
            ProductCategory productCategory = new ProductCategory("男生最爱",5);
            ProductCategory result = repository.save(productCategory);
            Assert.assertNotNull(result);
        }
        @Test
        public void findByCategoryTypeInTest(){
            List<Integer> list = Arrays.asList(1,4,5);
            List<ProductCategory> result = repository.findByCategoryTypeIn(list);
            Assert.assertNotEquals(0,result.size());
        }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值