zoukankan      html  css  js  c++  java
  • spring boot实战——微信点餐系统01:开始代码

    bojo(dataobject)的开发:

      @Entity:让这个方法变成bean。 来着jpa

      @DynamicUpdate : 时间戳自动更新

      @Data :Lombok注释,可以不用编写 getter、setter、tostring 方法,编译时自动添加这些方法

      @Id : 设置为主键        。

      @GeneratedValue:自增主键

    @Entity
    @DynamicUpdate
    @Data
    public class ProductCategory {
        /** 类目ID*/
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer categoryId;
        /** 类目名称*/
        private String categoryName;
        /** 类目编号*/
        private Integer categoryType;
    
        private Date updateTime;
        private Date createTime;
    
        public ProductCategory() {
        }
        public ProductCategory(String categoryName, Integer categoryType) {
            this.categoryName = categoryName;
            this.categoryType = categoryType;
        }
    }
    

    repository(dao):

      继承JpaRepository,使用 Spring Data JPA ,不用编写一些方法。

    public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
        /**
         * 该方法是通过 categoryTypeList 查找;该方法的命名是有语法规则的
         * @param categoryTypeList
         * @return
         */
        List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
    }
    

      

     service接口

    public interface CategoryService {
        ProductCategory findbyid(Integer categoryId);
        List<ProductCategory> findAll();
        List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
        ProductCategory save(ProductCategory productCategory);
    }
    

      

    service接口实现

    @Service
    public class CategoryServiceImpl implements CategoryService {
        @Autowired
        ProductCategoryRepository repository;
        @Override
        public ProductCategory findbyid(Integer categoryId) {
            return repository.findById(categoryId).get();
        }
    
        @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);
        }
    }
    

      

    测试类

      选中这个类或者方法,右键---> go  to ----> 选中想要测试的方法---->生成测试类

      @Transactional : 测试方法进行 修改 和 插入 后,对数据进行回滚。

    @SpringBootTest
    class CategoryServiceImplTest {
        @Autowired
        CategoryServiceImpl categoryService;
        @Test
        void findbyid() {
            ProductCategory productCategory = categoryService.findbyid(1);
            Assertions.assertEquals(1,productCategory.getCategoryId());
        }
    
        @Test
        void getAll() {
            List<ProductCategory> productCategoryList = categoryService.findAll();
            Assertions.assertNotEquals(0,productCategoryList.size());
        }
    
        @Test
        void findByCategoryTypeIn() {
            List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(Arrays.asList(1, 2, 3, 4));
            Assertions.assertNotEquals(0,productCategoryList.size());
        }
    
        @Test
        void save() {
            ProductCategory productCategory=new ProductCategory("男生专享",6);
            ProductCategory result = categoryService.save(productCategory);
            assertNotNull(result);
        }
    }
    

      

    Lombok

      是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具。(有人喜欢有人不喜欢)

      在pox.xml 引用资源:

            <!--一个插件,通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.10</version>
                <scope>provided</scope>
            </dependency>
    

       在IDEA 插件中心 添加插件 Lombok ,添加成功,就可以使用该工具。

     
  • 相关阅读:
    单调栈问题解析
    Linux进程状态切换
    Shell编程小例子
    Linux之shell编程
    树的遍历框架
    你真的会求1-100有多少个素数吗
    java实现LRU算法
    从上到下打印二叉树 III
    从上到下打印二叉树I
    模拟盘电路板调试过程中出现的问题
  • 原文地址:https://www.cnblogs.com/Lemonades/p/11721607.html
Copyright © 2011-2022 走看看