安装教程
1. 先部署shop_db内数据库到MySQL
2. 启动shop_back后端服务器代码,在IDEA开发工具内使用,JDK1.8,需要MAVEN3.5环境
3. 在WebStorm(前端开发工具均可)内启动shop_front后台管理系统
4 小程序开发工具内使用shop_wx
基于java springboot宠物交易商城微信小程序源码和论文
package com.ndky.shop_back.controller;
import com.alibaba.fastjson.JSON;
import com.ndky.shop_back.entity.User;
import com.ndky.shop_back.service.LoginService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
public class LoginController {
@Autowired
LoginService loginService;
@RequestMapping("/userLogin")
public String userLogin(@RequestBody User user){
String flag="error";
User us=loginService.userLogin(user.getUser_name(),user.getUser_password());
if(us!=null){
flag="ok";
}
HashMap res=new HashMap<>();
res.put("flag",flag);
res.put("user",user);
String res_json = JSON.toJSONString(res);
return res_json;
}
@RequestMapping("/wxLogin")
public String wxLogin(@Param("user_name") String user_name,@Param("user_password") String user_password){
User user=loginService.wxLogin(user_name,user_password);
String flag="error";
if(user!=null){
flag="ok";
}
HashMap res=new HashMap<>();
res.put("flag",flag);
res.put("user",user);
String res_json = JSON.toJSONString(res);
return res_json;
}
}
package com.ndky.shop_back.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ndky.shop_back.dao.GoodDao;
import com.ndky.shop_back.entity.Good;
import com.ndky.shop_back.entity.QueryInfo;
import com.ndky.shop_back.service.GoodService;
import com.ndky.shop_back.util.Consts;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@RestController
public class GoodController {
@Autowired
GoodService goodService;
@RequestMapping("/allGood")
public String getGoodList(QueryInfo queryInfo) {
// 获取最大列表数和当前编号
int number = goodService.getGoodCounts("%" + queryInfo.getQuery() + "%");
int pageState = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
List good = goodService.getAllGood("%" + queryInfo.getQuery() + "%", pageState, queryInfo.getPageSize());
HashMap res = new HashMap<>();
res.put("number", number);
res.put("data", good);
String res_string = JSON.toJSONString(res);
return res_string;
}
@RequestMapping("/goodState")
public String updateGoodState(@RequestParam("good_id") Integer good_id,
@RequestParam("good_state") Boolean good_state) {
int i = goodService.updateState(good_id, good_state);
return i > 0 ? "success" : "error";
}
@RequestMapping("/addGood")
public String addGood(@RequestBody Good good) {
int i = goodService.addGood(good);
return i > 0 ? "success" : "error";
}
@RequestMapping("/deleteGood")
public String deleteGood(int good_id) {
int i = goodService.deleteGood(good_id);
return i > 0 ? "success" : "error";
}
@RequestMapping("/getUpdateGood")
public String getUpdateGood(int good_id) {
Good good=goodService.getUpdateGood(good_id);
String string = JSON.toJSONString(good);//注意这里也可以直接返回对象,springboot会把对象直接转换成字符串
return string;
}
@RequestMapping("/editGood")
public String editUser(@RequestBody Good good) {
int i = goodService.editGood(good);
return i > 0 ? "success" : "error";
}
//查询所有商品,返回到微信小程序
@RequestMapping("/selectAllGood")
public String selectAllGood(){
List list=goodService.selectAllGood();
String string = JSON.toJSONString(list);//注意这里也可以直接返回对象,springboot会把对象直接转换成字符串
return string;
}
//按照class_name查询商品,填充商品分类右边栏目
@RequestMapping("/selectGoodByClass")
public String selectGoodByClass(@Param("class_name") String class_name){
List list=goodService.selectGoodByClass(class_name);
String string = JSON.toJSONString(list);
return string;
}
@RequestMapping("/selectGoodById")
public String selectGoodById(@Param("good_id") Integer good_id){
Good good=goodService.selectGoodById(good_id);
String string = JSON.toJSONString(good);
return string;
}
@RequestMapping("/searchGood")
public String searchGood(@Param("query") String query){
List list=goodService.searchGood(query);
String string = JSON.toJSONString(list);
return string;
}
@RequestMapping("/setGoodStar")
public String setGoodStar(@Param("good_id") Integer good_id,@Param("good_star")Integer good_star){
int i = goodService.setGoodStar(good_id, good_star);
return i > 0 ? "success" : "error";
}
@RequestMapping("/getGoodByStar")
public String getGoodByStar(QueryInfo queryInfo){
// 获取最大列表数和当前编号
int number = goodService.getGoodCounts("%" + queryInfo.getQuery() + "%");
int pageState = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
List good = goodService.getGoodByStar("%" + queryInfo.getQuery() + "%", pageState, queryInfo.getPageSize());
HashMap res = new HashMap<>();
res.put("number", number);
res.put("data", good);
String res_string = JSON.toJSONString(res);
return res_string;
}
@RequestMapping("/getStarByGoodId")
public Integer getStarByGoodId(@Param("good_id") Integer good_id){
return goodService.getStarByGoodId(good_id);
}
//更新商品图片
@RequestMapping("/updateGoodImage")
public Object updateGoodImage(@RequestParam("file") MultipartFile avatorFile, @RequestParam("good_id")int id){
JSONObject jsonObject = new JSONObject();
if(avatorFile.isEmpty()){
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"文件上传失败");
return jsonObject;
}
//文件名=当前时间到毫秒+原来的文件名
String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
//文件路径
String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
+System.getProperty("file.separator")+"goodImage";
//如果文件路径不存在,新增该路径
File file1 = new File(filePath);
if(!file1.exists()){
file1.mkdir();
}
//实际的文件地址
File dest = new File(filePath+System.getProperty("file.separator")+fileName);
//存储到数据库里的相对文件地址
String storeAvatorPath = Consts.URL+"/img/goodImage/"+fileName;
try {
avatorFile.transferTo(dest);
Good good=new Good();
good.setGood_id(id);
good.setGood_image(storeAvatorPath);
int flag = goodService.editGood(good);
if(flag>0){
jsonObject.put(Consts.CODE,1);
jsonObject.put(Consts.MSG,"上传成功");
jsonObject.put("pic",storeAvatorPath);
return jsonObject;
}
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"上传失败");
return jsonObject;
} catch (IOException e) {
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
}finally {
return jsonObject;
}
}
}