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