中国加油,武汉加油!
篇幅较长,配合目录观看
案例准备
- 本案例基于springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建
- git地址 https://gitee.com/springboot-dubbo/nz1904-springboot-shop-02
1. 用户模块-添加 user-web
1.1 编写WebMVCConfig
package com.wpj.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("user/toAddUser").setViewName("user/addUser");
}
}
1.2 编写addUser.html
1.3 编写form-utils.js
1.4 shop-common导入依赖编写返回数据的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
package com.wpj.common.entity;
import com.sun.org.apache.regexp.internal.RE;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultEntity<T> {
private static final String SUCCESS= "SUCCESS";
private static final String FALL= "FALL";
private static final String MSG_SUCCESS="操作成功";
private static final String MSG_FALL="操作失败";
private static final String NO_URL=null;
private String msg;
private String state;
private T data;
private String url;
public static <T> ResultEntity<T> FALL(){
return new ResultEntity<T>(MSG_FALL,FALL,null,NO_URL);
}
public static <T> ResultEntity<T> FALL(String msg){
return new ResultEntity<T>(msg,FALL,null,NO_URL);
}
public static <T> ResultEntity<T> SUCCESS(){
return new ResultEntity<T>(MSG_SUCCESS,SUCCESS,null,NO_URL);
}
public static <T> ResultEntity<T> SUCCESS(String url){
return new ResultEntity<T>(MSG_SUCCESS,SUCCESS,null,url);
}
}
1.5 user-web添加依赖
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
1.6 user-web的 Controller添加方法
@RequestMapping("/addUser")
@ResponseBody
public ResultEntity addUser(User user){
int insert = userService.insert(user);
if (insert > 0) {
return ResultEntity.SUCCESS();
} else{
return ResultEntity.FALL();
}
}
1.7 数据库中给默认值

1.8 启动程序入口测试

2. 用户模块-修改 user-web
2.1 编写updateUser.html
2.2 Controller添加方法
@RequestMapping(value = "/getUserById/{id}")
public String getUserById(@PathVariable Integer id,ModelMap map){
User user = userService.selectById(id);
map.put("user",user);
return "user/updateUser";
}
2.3 如果出现A cannot be cast to B
spring.thyemleaf.mode=LEGACYHTML5
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
2.4 重构或者重启程序入口

3. 商品模块-查询 user-web
3.1 index.html添加商品管理
<li><a _href="goods/getGoodsPage" href="javascript:void(0)">商品管理</a></li>
3.2 shop-entity编写Goods实体
package com.wpj.entity;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_goods")
public class Goods implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
private String gname;
private String gdesc;
private BigDecimal gprice;
private String gpic;
private Integer gstock;
private Integer gtype;
private Integer gstate;
}
3.3 shop-mapper编写Mapper接口(参考IUserMapper)
3.4 shop-service-api编写Service接口(参考IUserService)
3.5 shop-service-iml新建goods-service(module-springboot)
3.5.1 导相关依赖(参考user-service项目)
3.5.2 goods-service编写ServiceImpl实现类(参考IUserServiceImpl)
3.5.3 修改主启动类(参考UserServiceApplication)
3.5.4 编写application.yml(参考user-service项目)
3.5.5 编写分页配置 (复制user-service项目里)
3.5.6 Test
package com.wpj.serivce.impl;
import com.wpj.entity.Goods;
import com.wpj.service.IGoodService;
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;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestService {
@Autowired
private IGoodService goodService;
@Test
public void testadd(){
Goods goods = new Goods();
goods.setGname("Apple");
goods.setGpic("apple.jpg");
goods.setGstate(1);
goods.setGdesc("好吃");
goods.setGtype(0);
goods.setGstock(10);
int insert = goodService.insert(goods);
System.out.println(insert);
}
}

3.6 user-web(shop_back)编写GoodsController
package com.wpj.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.entity.Goods;
import com.wpj.service.IGoodService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Reference
private IGoodService goodService;
@RequestMapping("/getGoodsPage")
public String getGoodsPage(Page<Goods> page, ModelMap map){
page = goodService.getDubboPage(page);
map.put("url", "goods/getGoodsPage");
map.put("page",page);
return "goods/goodsList";
}
}
3.7 编写goodsList.html
3.8 重启程序入口访问页面

4. 商品模块-添加-前端图片上传组件(后面用FastDFS)
4.1 编写addGoods.html
4.2 user-web(shop-back)添加跳转组件
package com.wpj.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("user/toAddUser").setViewName("user/addUser");
registry.addViewController("goods/toAddGoods").setViewName("goods/addGoods");
}
}
4.3 下载WebUpload插件
- 官网 http://fex.baidu.com/webuploader/
4.3.1 将下载的文件夹放入项目中static目录中
4.3.2 页面中引入css和js
4.4 application.yml
upload:
reqPngPath: http://localhost:8080/upload
4.5 GoodsController添加方法
package com.wpj.controller;
import com.alibaba.dubbo.common.utils.IOUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.common.entity.ResultEntity;
import com.wpj.entity.Goods;
import com.wpj.service.IGoodService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.ch.IOUtil;
import java.io.*;
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Reference
private IGoodService goodService;
@RequestMapping("/getGoodsPage")
public String getGoodsPage(Page<Goods> page, ModelMap map){
page = goodService.getDubboPage(page);
map.put("url", "goods/getGoodsPage");
map.put("page",page);
return "goods/goodsList";
}
@Value("${upload.reqPngPath}")
private String reqPngPath;
private String uploadPath = "E:\\codeDevelop\\ideaDevelop\\springboot\\nz1904-shop\\shop-web\\user-web\\src\\main\\resources\\static\\upload";
@RequestMapping("/uploadFile")
@ResponseBody
public String uploadFile(MultipartFile file){
System.out.println("文件名:"+ file.getOriginalFilename());
FileOutputStream os = null;
InputStream is = null;
File opsFile = new File(uploadPath+ File.separator+file.getOriginalFilename());
try {
is = file.getInputStream();
os = new FileOutputStream(opsFile);
IOUtils.write(is,os);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return reqPngPath+file.getOriginalFilename();
}
@RequestMapping(value = "/addGoods")
@ResponseBody
public ResultEntity addGoods(Goods goods){
goodService.insert(goods);
return ResultEntity.SUCCESS();
}
}
4.6 重启程序入口
