1. 程式人生 > >sell01 環境搭建、編寫持久層並進行測試

sell01 環境搭建、編寫持久層並進行測試

boot 就會 -s 虛擬 調試 deb project 直接 jdk1

1 環境配置

  JDK  1.8

  MAVEN  3.5

  MYSQL  5.7

  VirtualBox  5.1

2 搭建MYSQL環境

  下載 VM 和 虛擬鏡像文件

  虛擬鏡像文件:點擊前往

  技巧01:安裝完virtualBox後直接點擊下載好的鏡像文件就會自動調到導入鏡像文件頁面

  問題01:啟動虛擬機時啟動失敗,提示物理機的64位內核沒有開啟

  解決01:進入系統bios頁面,開啟虛擬內核即可(詳情請問問度娘)

技術分享圖片
# 虛擬機說明文檔
VirtualBox-5.1.22
虛擬機系統 centos7.3
賬號 root
密碼 123456
#### 包括軟件
* jdk 1.8
.0_111 * nginx 1.11.7 * mysql 5.7.17 * redis 3.2.8 ##### jdk * 路徑 /usr/local/jdk1.8.0_111 ##### nginx * 路徑 /usr/local/nginx * 啟動 nginx * 重啟 nginx -s reload ##### mysql * 配置 /etc/my.conf * 賬號 root * 密碼 123456 * 端口 3306 * 啟動 systemctl start mysqld * 停止 systemctl stop mysqld ##### redis * 路徑 /usr/local/redis * 配置 /etc/reis.conf
* 端口 6379 * 密碼 123456 * 啟動 systemctl start redis * 停止 systemctl stop redis ##### tomcat * 路徑 /usr/local/tomcat * 啟動 systemctl start tomcat * 停止 systemctl stop tomcat
鏡像文件說明

3 編寫實體類

  3.1 註解介紹

    @Table(name = "product_category")  實體類和表明對應

    @Entity  該類是一個實體類

    @DynamicUpdate  動態刷新

    @Data  自動為實體類生成get/set/tostring方法

    @Id  主鍵

    @GeneratedValue  主鍵自增  

    @Column  指定實體類屬性和數據庫字段保持一致

    問題01:使用 @Data 後打包後會自動生成相應的方法,但是在IDEA環境運行時需要下載一個插件

    解決01:插件安裝教程

    問題02:使用@Data註解時需要導入相應的jar包

    解決02:

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    問題03:當實體類名和數據庫表明不一致時需要利用註解實現一一對應

     解決03:利用@Table指定數據庫表名

     問題04:當實體類屬性名和數據庫表的字段名不一致時需要利用註解實現一一對應

    解決04:利用@Column指定數據庫字段名

  3.2 實體類代碼

技術分享圖片
package cn.xinagxu.sell.dataObject;

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

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

/**
 * 類目表實體對象
 */
@Table(name = "product_category")
@Entity
@DynamicUpdate  // 實現動態刷新(例如:當數據庫中的更新時間字段是自動刷新時,如果修改數據時傳入了這個字段的信息時如果不在實體類中添加這個註解那麽數據庫對該字段的自動更新就會失效)
@Data // 該註解會自動生成一些get/set/tostring方法
public class ProductCategory {

    /** 類目ID */
    @Id
    @GeneratedValue
    private Integer categoryId;
    /** 類目名名字 */
    private String categoryName;
    /** 類目編號 */
    private Integer categoryType;
    /** 類目創建時間 */
    private Date createTime;
    /** 類目更新時間 */
    private Date updateTime;

    public ProductCategory() {
    }

}
實體類源代碼

4 編寫Dao層

  4.1 引入數據庫相關jar包

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

  4.2 配置數據庫連接

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://172.25.129.244/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true

  技巧01:持久層必須繼承 JpaRepository,繼承了該接口後就不用在持久層接口添加註解來設置bean了,因為父類已經實現了

  技巧02:JPA教程

技術分享圖片
package cn.xinagxu.sell.repository;

import cn.xinagxu.sell.dataObject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
    /** 根據一個類型列表去查詢類目類型在這個列表中的類目 */
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
持久層接口源代碼

5 持久層測試類 

技術分享圖片
package cn.xinagxu.sell.repository;

import cn.xinagxu.sell.dataObject.ProductCategory;
import lombok.extern.slf4j.Slf4j;
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 java.util.Arrays;
import java.util.List;

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

    @Autowired
    private ProductCategoryRepository productCategoryRepository;

    /** 根據主鍵查詢數據 */
    @Test
    public void findOneTest() {
        log.error("===自定義錯誤日誌===");
        log.warn("===自定義警告日誌===");
        log.info("===自定義信息日誌===");
        log.debug("===自定義調試日誌===");
        ProductCategory productCategory = productCategoryRepository.findOne(1);
        System.out.println(productCategory);
    }

    /** 插入一條數據(主鍵、創建時間、更新時間是自動實現的不必傳入) */
    @Test
    public void saveOneTest() {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryName("女生的最愛");
        productCategory.setCategoryType(3);
        ProductCategory result = productCategoryRepository.save(productCategory);
        Assert.assertNotNull(result); // 利用斷言判斷:如果結果不為null就表明正確
//        Assert.assertNotEquals(null, result);  // 利用斷言判斷:如果結果不為null就表明正確
    }

    /** 根據主鍵去更新一個條數據 */
    @Test
    public void updateOneTest() {
        ProductCategory productCategory = productCategoryRepository.findOne(2);
        productCategory.setCategoryName("三少");
        productCategoryRepository.save(productCategory);
    }

    /** 根據類型列表查詢 */
    @Test
    public void findByCategoryTypeTest() {
        List<Integer> typeList = Arrays.asList(1,2,3,4);
        List<ProductCategory> results = productCategoryRepository.findByCategoryTypeIn(typeList);
        Assert.assertNotEquals(0, results.size());
    }

}
View Code

sell01 環境搭建、編寫持久層並進行測試