基于javaweb+mysql的springboot校园二手交易平台二手市场(java+springboot+maven+mysql+freemarker)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SpringBoot校园二手交易平台二手市场(java+springboot+maven+mysql+freemarker)
前台学生:
http://localhost:8088/home
student1 123456
student2 123456
student3 123456
功能:查看商品、求购、发布宝贝、评价等
后台管理员:
http://localhost:8088
admin 123456
功能:系统设置、物品及分类管理、学生管理、评论管理、新闻管理等
private GoodsCategoryService goodsCategoryService;
@Autowired
private StudentService studentService;
@Autowired
private NewsService newsService;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping(value="/index")
public String index(Model model,PageBean<Goods> pageBean,Goods goods){
pageBean.setPageSize(12);
goods.setStatus(Goods.GOODS_STATUS_UP);
model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
model.addAttribute("name",goods.getName());
model.addAttribute("newsList",newsService.findList(3));
return "home/index/index";
}
/**
* 新闻详情页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/news_detail")
public String index(Model model,@RequestParam(name="id",required=true)Long id){
News news = newsService.find(id);
model.addAttribute("news",news);
news.setViewNumber(news.getViewNumber()+1);
newsService.save(news);
return "home/index/news_detail";
}
/**
* 用户登录页面
* @param model
* @return
*/
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(Model model){
return "home/index/login";
}
model.addAttribute("newsTitle", news.getTitle());
model.addAttribute("pageBean", newsService.findList(pageBean,news));
return "admin/news/list";
}
/**
* 新增新闻公告页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
return "admin/news/add";
}
/**
* 新闻公告添加表单提交处理
* @param news
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> add(News news){
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(news);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
//到这说明一切符合条件,进行数据库新增
if(newsService.save(news) == null){
return Result.error(CodeMsg.ADMIN_NEWS_ADD_ERROR);
}
operaterLogService.add("添加新闻公告:" + news);
return Result.success(true);
}
/**
* 新闻公告编辑页面
* @param model
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.GET)
public String edit(Model model,@RequestParam(name="id",required=true)Long id){
model.addAttribute("news", newsService.find(id));
return "admin/news/edit";
@RequestMapping(value="/list")
public String list(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
model.addAttribute("thirdMenus",MenuUtil.getThirdMenus(findAll));
return "admin/menu/list";
}
/**
* 菜单添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
return "admin/menu/add";
}
/**
* 菜单添加提交表单处理
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> add(Menu menu){
if(menu == null){
Result.error(CodeMsg.DATA_ERROR);
}
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(menu);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
if(menu.getParent() != null){
if(menu.getParent().getId() == null){
menu.setParent(null);
}
}
//表示验证都通过,开始添加数据库
if(menuService.save(menu) == null){
Result.error(CodeMsg.ADMIN_MENU_ADD_ERROR);
/**
* 前台首页控制器
*
*/
@RequestMapping("/home/index")
@Controller
public class IndexController {
@Autowired
private GoodsService goodsService;
@Autowired
private GoodsCategoryService goodsCategoryService;
@Autowired
private StudentService studentService;
@Autowired
private NewsService newsService;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping(value="/index")
public String index(Model model,PageBean<Goods> pageBean,Goods goods){
pageBean.setPageSize(12);
goods.setStatus(Goods.GOODS_STATUS_UP);
model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
model.addAttribute("name",goods.getName());
model.addAttribute("newsList",newsService.findList(3));
return "home/index/index";
}
/**
/**
* 公用的上传类
*
*/
@RequestMapping("/admin/upload")
@Controller
public class AdminUploadController {
@Value("${upload.photo.sufix}")
private String uploadPhotoSufix;
@Value("${upload.photo.maxsize}")
private long uploadPhotoMaxSize;
private Logger log = LoggerFactory.getLogger(AdminUploadController.class);
/**
* 图片统一上传类
*
* @param photo
* @return
*/
@RequestMapping(value = "/upload_photo", method = RequestMethod.POST)
@ResponseBody
public Result<String> uploadPhoto(@RequestParam(name = "photo", required = true) MultipartFile photo) {
//判断文件类型是否是图片
String originalFilename = photo.getOriginalFilename();
//获取文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
if (!uploadPhotoSufix.contains(suffix.toLowerCase())) {
return Result.error(CodeMsg.UPLOAD_PHOTO_SUFFIX_ERROR);
}
if (photo.getSize() / 1024 > uploadPhotoMaxSize) {
CodeMsg codeMsg = CodeMsg.UPLOAD_PHOTO_ERROR;
codeMsg.setMsg("图片大小不能超过" + (uploadPhotoMaxSize / 1024) + "M");
return Result.error(codeMsg);
}
//准备保存文件
String uploadPhotoPath = PathUtil.newInstance().getUploadPhotoPath();
File filePath = new File(uploadPhotoPath);
}
//到这说明一切符合条件,进行数据库保存
User findById = userService.find(user.getId());
//讲提交的用户信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
BeanUtils.copyProperties(user, findById, "id","createTime","updateTime");
if(userService.save(findById) == null){
return Result.error(CodeMsg.ADMIN_USE_EDIT_ERROR);
}
operaterLogService.add("编辑用户,用户名:" + user.getUsername());
return Result.success(true);
}
/**
* 删除用户
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
userService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_USE_DELETE_ERROR);
}
operaterLogService.add("添加用户,用户ID:" + id);
return Result.success(true);
}
}
/**
* 后台菜单管理控制器
*
*/
@RequestMapping("/admin/menu")
@Controller
public class MenuController {
@Autowired
private MenuService menuService;
@Autowired
private OperaterLogService operaterLogService;
/**
* 菜单列表展示页面
* @param model
* @return
*/
@RequestMapping(value="/list")
public String list(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
model.addAttribute("thirdMenus",MenuUtil.getThirdMenus(findAll));
return "admin/menu/list";
}
/**
* 菜单添加页面
* @param model
public Result<Boolean> recommend(@RequestParam(name="id",required=true)Long id ,@RequestParam(name="recommend",required=true)Integer recommend){
Goods goods = goodsService.findById(id);
if(goods == null){
return Result.error(CodeMsg.ADMIN_GOODS_NO_EXIST);
}
if(goods.getRecommend() == recommend){
return Result.error(CodeMsg.ADMIN_GOODS_STATUS_NO_CHANGE);
}
if(recommend != Goods.GOODS_RECOMMEND_OFF && recommend != Goods.GOODS_RECOMMEND_ON){
return Result.error(CodeMsg.ADMIN_GOODS_STATUS_ERROR);
}
if(goods.getStatus() == Goods.GOODS_STATUS_SOLD){
return Result.error(CodeMsg.ADMIN_GOODS_STATUS_UNABLE);
}
goods.setRecommend(recommend);;
//进行更新数据库
if(goodsService.save(goods) ==null){
return Result.error(CodeMsg.ADMIN_GOODS_EDIT_ERROR);
}
return Result.success(true);
}
/**
* 物品删除操作
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
goodsService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_GOODS_DELETE_ERROR);
}
return Result.success(true);
}
}
/**
* 物品删除操作
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
goodsService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_GOODS_DELETE_ERROR);
}
return Result.success(true);
}
}
/**
* 后台友情链接管理控制器
/**
* 前台首页控制器
*
*/
@RequestMapping("/home/index")
@Controller
public class IndexController {
@Autowired
private GoodsService goodsService;
@Autowired
private GoodsCategoryService goodsCategoryService;
@Autowired
private StudentService studentService;
@Autowired
private NewsService newsService;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping(value="/index")
public String index(Model model,PageBean<Goods> pageBean,Goods goods){
pageBean.setPageSize(12);
goods.setStatus(Goods.GOODS_STATUS_UP);
model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
model.addAttribute("name",goods.getName());
model.addAttribute("newsList",newsService.findList(3));
return "home/index/index";
}
/**
* 新闻详情页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/news_detail")
public String index(Model model,@RequestParam(name="id",required=true)Long id){
News news = newsService.find(id);
model.addAttribute("news",news);
if(existGoods == null){
return Result.error(CodeMsg.HOME_STUDENT_GOODS_NO_EXIST);
}
existGoods.setStatus(status);
if(goodsService.save(existGoods) == null){
return Result.error(CodeMsg.HOME_STUDENT_GOODS_EDIT_ERROR);
}
return Result.success(true);
}
/**
* 发布求购物品页面
* @param model
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.GET)
public String publishWanted(Model model){
return "home/student/publish_wanted";
}
/**
* 求购物品发布提交
* @param wantedGoods
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> publishWanted(WantedGoods wantedGoods){
CodeMsg validate = ValidateEntityUtil.validate(wantedGoods);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
Student loginedStudent = (Student)SessionUtil.get(SessionConstant.SESSION_STUDENT_LOGIN_KEY);
wantedGoods.setStudent(loginedStudent);
if(wantedGoodsService.save(wantedGoods) == null){
return Result.error(CodeMsg.HOME_STUDENT_PUBLISH_ERROR);
}
return Result.success(true);
}
/**
* 编辑求购物品页面
* @param id
* @param model
* @return
*/
@RequestMapping(value="/edit_wanted_goods",method=RequestMethod.GET)
public String editWantedGoods(@RequestParam(name="id",required=true)Long id,Model model){
WantedGoods wantedGoods = wantedGoodsService.find(id);
if(wantedGoods == null){
model.addAttribute("msg", "求购物品不存在!");
return "error/runtime_error";
/**
* 发布求购物品页面
* @param model
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.GET)
public String publishWanted(Model model){
return "home/student/publish_wanted";
}
/**
* 求购物品发布提交
* @param wantedGoods
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> publishWanted(WantedGoods wantedGoods){
CodeMsg validate = ValidateEntityUtil.validate(wantedGoods);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
Student loginedStudent = (Student)SessionUtil.get(SessionConstant.SESSION_STUDENT_LOGIN_KEY);
wantedGoods.setStudent(loginedStudent);
if(wantedGoodsService.save(wantedGoods) == null){
return Result.error(CodeMsg.HOME_STUDENT_PUBLISH_ERROR);
}
return Result.success(true);
}
/**
* 编辑求购物品页面
* @param id
* @param model
* @return
*/
@RequestMapping(value="/edit_wanted_goods",method=RequestMethod.GET)
public String editWantedGoods(@RequestParam(name="id",required=true)Long id,Model model){
WantedGoods wantedGoods = wantedGoodsService.find(id);
if(wantedGoods == null){
model.addAttribute("msg", "求购物品不存在!");
return "error/runtime_error";
}
model.addAttribute("wantedGoods", wantedGoods);
return "home/student/edit_wanted";
}
/**
@ResponseBody
public Result<Boolean> edit(News news){
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(news);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
//到这说明一切符合条件,进行数据库保存
News findById = newsService.find(news.getId());
//讲提交的新闻公告信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
BeanUtils.copyProperties(news, findById, "id","createTime","updateTime");
if(newsService.save(findById) == null){
return Result.error(CodeMsg.ADMIN_NEWS_EDIT_ERROR);
}
operaterLogService.add("编辑新闻公告:" + findById);
return Result.success(true);
}
/**
* 删除新闻公告
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
newsService.delete(id);
operaterLogService.add("删除新闻公告,新闻公告ID:" + id);
return Result.success(true);
}
}
* 登录页面
*
* @param name
* @param model
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
return "admin/system/login";
}
/**
* 用户登录提交表单处理方法
*
* @param request
* @param user
* @param cpacha
* @return
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Result<Boolean> login(HttpServletRequest request, User user, String cpacha) {
if (user == null) {
return Result.error(CodeMsg.DATA_ERROR);
}
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(user);
if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
return Result.error(validate);
}
Object attribute = request.getSession().getAttribute("admin_login");
if (attribute == null) {
return Result.error(CodeMsg.SESSION_EXPIRED);
}
//表示session未失效,进一步判断用户填写的验证码是否正确
//表示实体信息合法,开始验证验证码是否为空
if (StringUtils.isEmpty(cpacha)) {
return Result.error(CodeMsg.CPACHA_EMPTY);
}
//说明验证码不为空,从session里获取验证码
if (!cpacha.equalsIgnoreCase(attribute.toString())) {
return Result.error(CodeMsg.CPACHA_ERROR);
}
//表示验证码正确,开始查询数据库,检验密码是否正确
User findByUsername = userService.findByUsername(user.getUsername());
*/
@RequestMapping(value="/update_status",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> upDown(@RequestParam(name="id",required=true)Long id ,
@RequestParam(name="status",required=true)Integer status){
Student student = studentService.findById(id);
if(student == null){
return Result.error(CodeMsg.ADMIN_GOODS_NO_EXIST);
}
if(student.getStatus() == status){
return Result.error(CodeMsg.ADMIN_STUDENT_STATUS_NO_CHANGE);
}
if(status != Student.STUDENT_STATUS_ENABLE && status != Student.STUDENT_STATUS_UNABLE){
return Result.error(CodeMsg.ADMIN_STUDENT_STATUS_ERROR);
}
student.setStatus(status);
//进行更新数据库
if(studentService.save(student) ==null){
return Result.error(CodeMsg.ADMIN_STUDENT_EDIT_ERROR);
}
return Result.success(true);
}
/**
* 学生删除操作
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
try {
studentService.delete(id);
} catch (Exception e) {
return Result.error(CodeMsg.ADMIN_STUDENT_DELETE_ERROR);
}
return Result.success(true);
}
}
* 菜单列表展示页面
* @param model
* @return
*/
@RequestMapping(value="/list")
public String list(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
model.addAttribute("thirdMenus",MenuUtil.getThirdMenus(findAll));
return "admin/menu/list";
}
/**
* 菜单添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model){
List<Menu> findAll = menuService.findAll();
model.addAttribute("title","菜单列表");
model.addAttribute("topMenus",MenuUtil.getTopMenus(findAll));
model.addAttribute("secondMenus",MenuUtil.getSecondMenus(findAll));
return "admin/menu/add";
}
/**
* 菜单添加提交表单处理
* @param menu
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> add(Menu menu){
if(menu == null){
Result.error(CodeMsg.DATA_ERROR);
}
//用统一验证实体方法验证是否合法
CodeMsg validate = ValidateEntityUtil.validate(menu);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
}
existGoods.setStatus(status);
if(goodsService.save(existGoods) == null){
return Result.error(CodeMsg.HOME_STUDENT_GOODS_EDIT_ERROR);
}
return Result.success(true);
}
/**
* 发布求购物品页面
* @param model
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.GET)
public String publishWanted(Model model){
return "home/student/publish_wanted";
}
/**
* 求购物品发布提交
* @param wantedGoods
* @return
*/
@RequestMapping(value="/publish_wanted",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> publishWanted(WantedGoods wantedGoods){
CodeMsg validate = ValidateEntityUtil.validate(wantedGoods);
if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
Student loginedStudent = (Student)SessionUtil.get(SessionConstant.SESSION_STUDENT_LOGIN_KEY);
wantedGoods.setStudent(loginedStudent);
if(wantedGoodsService.save(wantedGoods) == null){
return Result.error(CodeMsg.HOME_STUDENT_PUBLISH_ERROR);
}
return Result.success(true);
}
/**
* 编辑求购物品页面
* @param id
* @param model
* @return
@Autowired
private StudentService studentService;
@Autowired
private NewsService newsService;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping(value="/index")
public String index(Model model,PageBean<Goods> pageBean,Goods goods){
pageBean.setPageSize(12);
goods.setStatus(Goods.GOODS_STATUS_UP);
model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
model.addAttribute("name",goods.getName());
model.addAttribute("newsList",newsService.findList(3));
return "home/index/index";
}
/**
* 新闻详情页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/news_detail")
public String index(Model model,@RequestParam(name="id",required=true)Long id){
News news = newsService.find(id);
model.addAttribute("news",news);
news.setViewNumber(news.getViewNumber()+1);
newsService.save(news);
return "home/index/news_detail";
}
/**
* 用户登录页面
* @param model
* @return
*/
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(Model model){
return "home/index/login";
}