作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
流浪狗领养网站是一个基于ssm(Spring SpringMVC MyBatis)的项目,项目分为前后台。
前台网站主要首页(包含轮播图、关键字搜索、点击排行、最新文章、站长推荐、最新评论、标签云等)、文章推荐、收养狗狗、送养狗、留言等功能;
后台主要功能模块包括:
用户信息、流浪狗信息、疫苗管理、收养记录、客服管理等;
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
技术栈
1. 后端:Spring SpringMVC MyBatis
2. 前端:JSP+Bootstrap+JQuery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 将项目中db.properties配置文件中的数据库配置改为自己的配置
3. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;
4.修改DogController.java中第86行及第147行左右的uploadDB,根据自己的系统打开对应代码;
5. 运行项目,输入localhost:8080/ssm-adopt 登录
运行截图
前台界面
后台界面
相关代码
AdoptController
package com.ypf.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ypf.pojo.TDogUser;
import com.ypf.pojo.TUser;
import com.ypf.service.AdoptService;
import com.ypf.utils.AdoptJSONResult;
import com.ypf.utils.JqGridResult;
@Controller
@RequestMapping("/adopt")
public class AdoptController extends BaseController{
@Autowired
private AdoptService adoptService;
@RequestMapping("/showAdoptRecordInfoListPage")
public String showUserInfoListPage(){
return "/record/adoptRecordInfoList";
}
@RequestMapping("/showAdoptUserInfoPage")
public String showAdoptUserInfoPage(){
return "/record/adoptUserInfoList";
}
@RequestMapping("/getAdoptRecordInfoList")
@ResponseBody
public JqGridResult getAdoptRecordInfoList(TUser user,Integer page){
if(page == null){
page = 1;
}
JqGridResult jqGridResult = adoptService.queryAllAdoptRecord(page,pageSize);
return jqGridResult;
}
@RequestMapping("/getAdoptUserInfoList")
@ResponseBody
public JqGridResult getAdoptUserInfoList(TUser user,Integer page){
if(page == null){
page = 1;
}
JqGridResult jqGridResult = adoptService.queryAllAdoptUser(page, pageSize);
return jqGridResult;
}
@RequestMapping("/delete")
@ResponseBody
public AdoptJSONResult deleteComment(Integer recordId){
adoptService.deleteAdoptRecord(recordId);
return AdoptJSONResult.ok();
}
@RequestMapping("/modifyAdoptRecord")
@ResponseBody
public AdoptJSONResult modifyAdoptRecord(TDogUser adoptRecord){
//修改 status 0:未审核 1:审核通过
adoptRecord.setStatus(1);
adoptService.updateAdoptRecord(adoptRecord);
return AdoptJSONResult.ok();
}
}
BaseController
package com.ypf.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import com.ypf.pojo.TAdmin;
/**
* @Description: basic controller, controller中的大部分通用方法写在此
*/
public class BaseController {
/**
* 默认分页行数
*/
public static final Integer pageSize = 10;
/**
*
* @Description: 验证并且获得获得bean上的错误
* @param result
* @return
*/
protected Map<String, String> getErrors(BindingResult result) {
Map<String, String> map = new HashMap<String, String>();
List<FieldError> list = result.getFieldErrors();
for (FieldError error : list) {
map.put(error.getField(), error.getDefaultMessage());
}
return map;
}
/**
*
* @Description: 获得域名地址路径
* @param request
* @return
*/
protected String getWebUrlAddress(HttpServletRequest request) {
StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
return tempContextUrl;
}
protected TAdmin getCurrentUser(HttpServletRequest request) {
HttpSession session = request.getSession();
TAdmin admin = (TAdmin) session.getAttribute("sessionAdmin");
return admin;
}
}
DogController
package com.ypf.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.ypf.pojo.TDog;
import com.ypf.pojo.TDogUser;
import com.ypf.pojo.TUser;
import com.ypf.service.AdoptService;
import com.ypf.service.DogService;
import com.ypf.utils.AdoptJSONResult;
import com.ypf.utils.JqGridResult;
@Controller
@RequestMapping("/dog")
public class DogController extends BaseController{
@Autowired
private DogService dogService;
@Autowired
private AdoptService adoptService;
@RequestMapping("/showDogInfoListPage")
public String showDogInfoListPage(){
return "/dog/dogInfoList";
}
@RequestMapping("/showCreateDogPage")
public ModelAndView showCreateDogPage(HttpServletRequest request){
ModelAndView mv = new ModelAndView("dog/createDog");
return mv;
}
@RequestMapping("/getDogInfoList")
@ResponseBody
public JqGridResult getDogInfoList(TDog dog,Integer page){
if(page == null){
page = 1;
}
JqGridResult jqGridResult = dogService.queryAllDog(dog, page, pageSize);
return jqGridResult;
}
@RequestMapping("/delete")
@ResponseBody
public AdoptJSONResult deleteDog(Integer dogId){
dogService.deleteDog(dogId);
return AdoptJSONResult.ok();
}
@RequestMapping("/saveOrUpdate")
@ResponseBody
public AdoptJSONResult saveOrUpdate(TDog dog,@RequestParam(name="file",required=false) MultipartFile file,
HttpServletRequest request) throws IOException{
// 狗id不为空,则修改狗;狗id为空,则新建狗
Integer dogId = dog.getId();
if (dogId != null) {
dogService.updateDog(dog);
} else {
if(file != null){
//保存到数据库的路径
String temp=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=temp.indexOf(".metadata");
//String path=temp.substring(1,num).replace('/', '\\')+request.getContextPath().replaceAll("/", "")+"\\src\\main\\webapp\\";
String path=request.getSession().getServletContext().getRealPath("");
// windows下使用该路径;
//String uploadDB = "static\\pages\\img\\dog\\";
// mac下使用该路径
String uploadDB = "static/pages/img/dog/";
String fileName = file.getOriginalFilename();
String[] fileNames = fileName.split("\\.");
String sufixName = fileNames[fileNames.length-1];
fileName = new Date().getTime() + "." +sufixName;
uploadDB += fileName;
dog.setFaceImage(uploadDB);
InputStream in = file.getInputStream();
File finalFile = new File(path+uploadDB);
OutputStream os = new FileOutputStream(finalFile);
IOUtils.copy(in, os);
}
dogService.addDog(dog);
}
return AdoptJSONResult.ok();
}
@RequestMapping("/saveAdoptUserInfo")
public String saveOrUpdate(TDog dog,HttpServletRequest request) throws IOException{
HttpSession session = request.getSession();
dogService.addDog(dog);
int dogId = dog.getId();
TDogUser adopt = new TDogUser();
TUser user = (TUser) session.getAttribute("sessionUser");
adopt.setUserId(user.getId());
//type 1:收养 2:送养
adopt.setType(2);
adopt.setStatus(0);
adopt.setDogId(dogId);
adoptService.addAdoptRecord(adopt);
return "forward:/frontPage/adoptDog.jsp";
}
@RequestMapping("/showModifyDogPage")
public ModelAndView showModifyDog(Integer dogId, HttpServletRequest request){
// 查询狗信息
TDog dogInfo = dogService.queryDogInfoById(dogId);
ModelAndView mv = new ModelAndView("dog/modifyDog");
mv.addObject("dogInfo", dogInfo);
return mv;
}
@PostMapping("/upload")
@ResponseBody
public AdoptJSONResult faceUpload(MultipartFile file,HttpServletRequest request)throws Exception{
//保存到数据库中的相对路径
// windows系统
String uploadPathDB = "static\\pages\\img\\dog\\";
try {
if(file != null){
//保存到数据库的路径
String temp=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=temp.indexOf(".metadata");
//String path=temp.substring(1,num).replace('/', '\\')+request.getContextPath().replaceAll("/", "")+"\\src\\main\\webapp\\";
String path=request.getSession().getServletContext().getRealPath("");
// mac系统
//uploadPathDB = "static/pages/img/dog/";
String fileName = file.getOriginalFilename();
String[] fileNames = fileName.split("\\.");
String sufixName = fileNames[fileNames.length-1];
fileName = new Date().getTime() + "." +sufixName;
uploadPathDB += fileName;
InputStream in = file.getInputStream();
File finalFile = new File(path+uploadPathDB);
OutputStream os = new FileOutputStream(finalFile);
IOUtils.copy(in, os);
}else{
return AdoptJSONResult.errorMsg("上传出错...");
}
} catch (Exception e) {
e.printStackTrace();
return AdoptJSONResult.errorMsg("上传出错...");
}
return AdoptJSONResult.ok(uploadPathDB);
}
}
疫苗管理Controller
package com.ypf.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.ypf.pojo.TDog;
import com.ypf.pojo.TDogVaccine;
import com.ypf.pojo.TVaccine;
import com.ypf.service.DogService;
import com.ypf.service.VaccineService;
import com.ypf.utils.AdoptJSONResult;
import com.ypf.utils.JqGridResult;
/**
* 疫苗管理Controller
* @author 11023
*
*/
@Controller
@RequestMapping("/vaccine")
public class VaccineController extends BaseController{
@Autowired
private VaccineService vaccineService;
@Autowired
private DogService dogService;
@RequestMapping("/showVaccineInfoListPage")
public String showVaccineInfoListPage(){
return "/vaccine/vaccineInfoList";
}
@RequestMapping("/showDogVaccineInfoListPage")
public String showDogVaccineInfoListPage(){
return "/vaccine/dogVaccineInfoList";
}
@RequestMapping("/showCreateVaccinePage")
public ModelAndView showCreateVaccinePage(HttpServletRequest request){
ModelAndView mv = new ModelAndView("vaccine/createVaccine");
return mv;
}
@RequestMapping("/delete")
@ResponseBody
public AdoptJSONResult deleteVaccine(Integer vaccineId){
vaccineService.deleteVaccine(vaccineId);
return AdoptJSONResult.ok();
}
@RequestMapping("/getVaccineInfoList")
@ResponseBody
public JqGridResult getVaccineInfoList(TVaccine vaccine,Integer page){
if(page == null){
page = 1;
}
JqGridResult jqGridResult = vaccineService.queryAllVaccine(vaccine, page, pageSize);
return jqGridResult;
}
@RequestMapping("/getDogVaccineInfoList")
@ResponseBody
public JqGridResult getDogVaccineInfoList(TDogVaccine dogVaccine,Integer page){
if(page == null){
page = 1;
}
JqGridResult jqGridResult = vaccineService.queryDogVaccine(dogVaccine, page, pageSize);
return jqGridResult;
}
@RequestMapping("/saveOrUpdate")
@ResponseBody
public AdoptJSONResult saveOrUpdate(TVaccine vaccine){
Integer vaccineId = vaccine.getId();
if (vaccineId != null) {
vaccineService.updateVaccine(vaccine);
} else {
vaccineService.addVaccine(vaccine);
}
return AdoptJSONResult.ok();
}
@RequestMapping("/modifyVaccine")
public ModelAndView showModifyUser(Integer vaccineId, HttpServletRequest request){
// 查询疫苗信息
TVaccine vaccineInfo = vaccineService.queryVaccineInfoById(vaccineId);
ModelAndView mv = new ModelAndView("vaccine/modifyVaccine");
mv.addObject("vaccineInfo", vaccineInfo);
return mv;
}
@RequestMapping("/modifyDogVaccine")
public ModelAndView modifyDogVaccine(Integer dogId, HttpServletRequest request){
// 查询流浪狗接种疫苗信息
TDog dogInfo = dogService.queryDogInfoById(dogId);
ModelAndView mv = new ModelAndView("vaccine/modifyDogVaccine");
mv.addObject("dogInfo", dogInfo);
//查询疫苗信息
JqGridResult jqGridResult = vaccineService.queryAllVaccine(null, 1, pageSize);
List<TVaccine> vaccineInfoList = (List<TVaccine>) jqGridResult.getRows();
mv.addObject("vaccineInfoList", vaccineInfoList);
return mv;
}
/**
* 保存接种疫苗记录
* @param dogVaccine
* @return
*/
@RequestMapping("/saveDogVaccine")
@ResponseBody
public AdoptJSONResult saveDogVaccine(TDogVaccine dogVaccine){
//改变是否接种疫苗状态
Integer dogId = dogVaccine.getDogId();
TDog dog = dogService.queryDogInfoById(dogId);
dog.setVaccinationStatus(2);
dogService.updateDog(dog);
//接种疫苗 相应的疫苗库存 -1
Integer vaccineId = dogVaccine.getVaccineId();
TVaccine vaccine = vaccineService.queryVaccineInfoById(vaccineId);
vaccine.setVaccineCount(vaccine.getVaccineCount()-1);
vaccineService.updateVaccine(vaccine);
//添加接种记录
vaccineService.addDogVaccine(dogVaccine);
return AdoptJSONResult.ok();
}
}
如果也想学习本系统,下面领取。关注并回复:031ssm