步骤 1 : 明确需求,首页需要什么数据? 步骤 2 : Category 步骤 3 : CategoryService 步骤 4 : ProductDAO 步骤 5 : ProductService 步骤 6 : ForePageController 步骤 7 : ForeRESTController 步骤 8 : home.html
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.pojo;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "category")
@JsonIgnoreProperties({ "handler","hibernateLazyInitializer" })
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
int id;
String name;
@Transient
List<Product> products;
@Transient
List<List<Product>> productsByRow;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public List<List<Product>> getProductsByRow() {
return productsByRow;
}
public void setProductsByRow(List<List<Product>> productsByRow) {
this.productsByRow = productsByRow;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.service;
import java.util.List;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.how2java.tmall.dao.CategoryDAO;
import com.how2java.tmall.pojo.Category;
@Service
public class CategoryService {
@Autowired CategoryDAO categoryDAO;
public Page4Navigator<Category> list(int start, int size, int navigatePages) {
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(start, size,sort);
Page pageFromJPA =categoryDAO.findAll(pageable);
return new Page4Navigator<>(pageFromJPA,navigatePages);
}
public List<Category> list() {
Sort sort = new Sort(Sort.Direction.DESC, "id");
return categoryDAO.findAll(sort);
}
public void add(Category bean) {
categoryDAO.save(bean);
}
public void delete(int id) {
categoryDAO.delete(id);
}
public Category get(int id) {
Category c= categoryDAO.findOne(id);
return c;
}
public void update(Category bean) {
categoryDAO.save(bean);
}
public void removeCategoryFromProduct(List<Category> cs) {
for (Category category : cs) {
removeCategoryFromProduct(category);
}
}
public void removeCategoryFromProduct(Category category) {
List<Product> products =category.getProducts();
if(null!=products) {
for (Product product : products) {
product.setCategory(null);
}
}
List<List<Product>> productsByRow =category.getProductsByRow();
if(null!=productsByRow) {
for (List<Product> ps : productsByRow) {
for (Product p: ps) {
p.setCategory(null);
}
}
}
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.dao;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.pojo.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ProductDAO extends JpaRepository<Product,Integer>{
Page<Product> findByCategory(Category category, Pageable pageable);
List<Product> findByCategoryOrderById(Category category);
}
package com.how2java.tmall.dao; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.pojo.Product; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface ProductDAO extends JpaRepository<Product,Integer>{ Page<Product> findByCategory(Category category, Pageable pageable); List<Product> findByCategoryOrderById(Category category); }
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.service;
import com.how2java.tmall.dao.ProductDAO;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductService {
@Autowired ProductDAO productDAO;
@Autowired CategoryService categoryService;
@Autowired ProductImageService productImageService;
public void add(Product bean) {
productDAO.save(bean);
}
public void delete(int id) {
productDAO.delete(id);
}
public Product get(int id) {
return productDAO.findOne(id);
}
public void update(Product bean) {
productDAO.save(bean);
}
public Page4Navigator<Product> list(int cid, int start, int size,int navigatePages) {
Category category = categoryService.get(cid);
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(start, size, sort);
Page<Product> pageFromJPA =productDAO.findByCategory(category,pageable);
return new Page4Navigator<>(pageFromJPA,navigatePages);
}
public void fill(List<Category> categorys) {
for (Category category : categorys) {
fill(category);
}
}
public void fill(Category category) {
List<Product> products = listByCategory(category);
productImageService.setFirstProdutImages(products);
category.setProducts(products);
}
public void fillByRow(List<Category> categorys) {
int productNumberEachRow = 8;
for (Category category : categorys) {
List<Product> products = category.getProducts();
List<List<Product>> productsByRow = new ArrayList<>();
for (int i = 0; i < products.size(); i+=productNumberEachRow) {
int size = i+productNumberEachRow;
size= size>products.size()?products.size():size;
List<Product> productsOfEachRow =products.subList(i, size);
productsByRow.add(productsOfEachRow);
}
category.setProductsByRow(productsByRow);
}
}
public List<Product> listByCategory(Category category){
return productDAO.findByCategoryOrderById(category);
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ForePageController {
@GetMapping(value="/")
public String index(){
return "redirect:home";
}
@GetMapping(value="/home")
public String home(){
return "fore/home";
}
}
package com.how2java.tmall.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class ForePageController { @GetMapping(value="/") public String index(){ return "redirect:home"; } @GetMapping(value="/home") public String home(){ return "fore/home"; } }
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package com.how2java.tmall.web;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ForeRESTController {
@Autowired
CategoryService categoryService;
@Autowired
ProductService productService;
@GetMapping("/forehome")
public Object home() {
List<Category> cs= categoryService.list();
productService.fill(cs);
productService.fillByRow(cs);
categoryService.removeCategoryFromProduct(cs);
return cs;
}
}
增值内容,请先登录
完整的 Springboot 模仿天猫项目,使用 Springboot 、Vue.js、shiro、redis、elasticsearch 等一整套技术栈, 从无到有涵盖全部129个知识点,565个开发步骤, 充实 Springboot 项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|