基于javaweb+mysql的springboot流浪猫救助系统(java+springboot+ssm+mysql+maven+thymeleaf+html)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot流浪猫救助系统(java+springboot+ssm+mysql+maven+thymeleaf+html)
一、项目简述
本系统功能包括:springboot+springmvc+mybatis的流浪 猫救助系统,后台分为普通用户可管理员用,包括注册, 登录,流浪猫发布,评论,分享,后台管理,用户管理, 热门文章赞助等等功能,非常不错,可以以参考二次开发 学习使用。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + Mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP +SpringBoot + MyBatis + Json+ Css + JavaScript + JQuery + Ajax + PageHelper+ Maven等等。
// 原图数据库路径
StringBuffer originalDataPath = new StringBuffer();
originalDataPath.append("/").append(hold).append(fileName);
// 压缩图数据库路径
StringBuffer compressDataPath = new StringBuffer();
compressDataPath.append("/").append(hold).append(nameSuffix).append("_small.").append(fileSuffix);
User user = (User) session.getAttribute(CatConst.USER_SESSION_KEY);
// 添加数据库
Enclosure enclosure = new Enclosure();
enclosure.setEnclosureUrl(originalDataPath.toString());
enclosure.setEnclosureSmallUrl(compressDataPath.toString());
enclosure.setEnclosureType(1);
enclosure.setEnclosureCreatetime(new Date());
enclosure.setEnclosureNumber(user.getUserPhone());
enclosure.setEnclosureWh(StarUtil.getImageWh(new File(mediaPath.toString() + "/" + fileName)));
enclosureService.addEnclosure(enclosure);
} catch (Exception e) {
//log.error("上传附件错误" + e.getMessage());
return new JsonResult(false, "系统未知错误");
}
} else {
return new JsonResult(false, "文件不能为空");
}
return new JsonResult(true, "上传成功");
}
}
package com.stray.cat.controller;
return new PageInfo<>(postMapper.queryPostAll());
}
@Override
@Cacheable(value = POST_CACHE_NAME_SEARCH, key = "'findPagePostSearch'+#page+#limit+#search")
public PageInfo<Post> findPageSearchPost(int page, int limit, String search) {
PageHelper.startPage(page,limit);
String information1="%"+search+"%";
List<Post> posts=postMapper.queryPostByTitle(information1);
return new PageInfo<>(posts);
}
@Override
public List<Post> findMaxPost(int limit) {
return postMapper.queryPostMaxCount(limit);
}
}
package com.stray.cat.controller;
@Controller
@RequestMapping("")
public class indexController {
@Autowired
IndexService indexService;
@Autowired
NoticeService noticeService;
@Autowired
SentenceService sentenceService;
@Autowired
HttpSession session;
@GetMapping
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SponsorServiceimpl implements SponsorService {
private static final String POST_CACHE_NAME = "POST2";
private static final String SPOSORVO_CACHE_NAME = "SPOSORVO";
@Autowired
HttpSession session;
@Autowired
SponsorMapper sponsorMapper;
@Autowired
UserMapper userMapper;
@Override
@Cacheable(value = POST_CACHE_NAME, key = "'findPageSponsor'+#page+#limit")
public PageInfo<Sponsor> findPageSponsorTowrite(int page, int limit) {
User user= (User) session.getAttribute(CatConst.USER_SESSION_KEY);
PageHelper.startPage(page,limit);
return new PageInfo<Sponsor>(sponsorMapper.querySponsorByPhone(user.getUserPhone()));
}
@Override
public int addSponsor(Sponsor sponsor) {
return sponsorMapper.addSponsor(sponsor);
}
@Override
public void deletesponsor(int sponsorId) {
sponsorMapper.deleteSponsor(sponsorId);
}
@Override
public Sponsor querySponsorById(int sponsorId) {
return sponsorMapper.querySponsorById(sponsorId);
}
List<IndexVo> indexMax=indexService.findMaxCount();
List<Notice> notices=noticeService.queryNotice();
Sentence sentence=sentenceService.findSentence();
model.addAttribute("sentence",sentence);
model.addAttribute("notices",notices);
model.addAttribute("search",null);
model.addAttribute("indexMax",indexMax);
model.addAttribute("info",info);
return "index";
}
@RequestMapping("indexSearch")
public String indexSearch(String search,Model model){
Sentence sentence=sentenceService.findSentence();
model.addAttribute("sentence",sentence);
PageInfo<IndexVo> info=indexService.findPageIndexSearch(search);
model.addAttribute("info",info);
model.addAttribute("search",search);
List<Notice> notices=noticeService.queryNotice();
model.addAttribute("notices",notices);
List<IndexVo> indexMax=indexService.findMaxCount();
model.addAttribute("indexMax",indexMax);
return "index";
}
@GetMapping("out")
public String index(){
session.invalidate();
return "redirect:";
}
}
package com.stray.cat.controller.admin;
@Autowired
PostService postService;
@Autowired
SponsorService sponsorService;
@Autowired
ShareService shareService;
@Autowired
CommentMapper commentMapper;
@Autowired
HttpSession session;
@Autowired
ReportService reportService;
@Autowired
SentenceService sentenceService;
@GetMapping
public String index(Model model,Integer id,String status){
int i=-1;
if(status.equals(IndexFrom.Post.getDesc())){
i=0;
}else if(status.equals(IndexFrom.Share.getDesc())){
i=1;
} else if(status.equals(IndexFrom.Sponsor.getDesc())){
i=2;
}
session.setAttribute(CatConst.USER_DETAILS_STATUS,i);
session.setAttribute(CatConst.USER_DETAILS_CODE,id);
model.addAttribute("comments",detailsService.findComment(i,id));
detailsService.addCount(id,i);
model.addAttribute("count",commentMapper.queryCount1(id,i));
model.addAttribute("recommends",detailsService.findRecommends(i));
IndexVo info=detailsService.findDetails(id,i);
model.addAttribute("info",info);
model.addAttribute("url",TitleUtil.getUrl());
List<IndexVo> indexMax=indexService.findMaxCount();
List<Notice> notices=noticeService.queryNotice();
model.addAttribute("notices",notices);
model.addAttribute("indexMax",indexMax);
Sentence sentence=sentenceService.findSentence();
model.addAttribute("sentence",sentence);
return "details";
}
@PostMapping("interceptor/addComment")
public String comment(String comment,Model model){
int i= (int) session.getAttribute(CatConst.USER_DETAILS_STATUS);
int id= (int) session.getAttribute(CatConst.USER_DETAILS_CODE);
User user= (User) session.getAttribute("user_session");
Comment comment1=new Comment();
comment1.setCommentContent(comment);
comment1.setCommentPhone(user.getUserPhone());
comment1.setCommentCreatetime(new Date());
comment1.setCommentPsId(id);
}
}
package com.stray.cat.controller.admin;
@Controller
@RequestMapping("admin/report")
public class adminReportController {
@Autowired
ReportService reportService;
@Autowired
HttpSession session;
@Autowired
ShareService shareService;
@Autowired
SponsorService sponsorService;
@Autowired
PostService postService;
@Autowired
UserService userService;
@Autowired
EmailService emailService;
@GetMapping
// 有时,一些网站的部分操作需要登录才能访问。如果跳转到登录界面登录成功后,怎样才能返回到登录之前的界面呢?
// 很简单,我们在代码部分中添加一部分很少的代码即可。在从一个页面跳转到登录界面之前的代码,我们用session保
// 存当前界面的url信息,在跳转到登录界面,登录成功后的代码中,判读是否有这个session信息,如果有,则跳转到
// session所存的url,记住跳转前清空这个session,否则在未关闭浏览器重新登录时可能又会跳到session中所存储url的界面。
}
package com.stray.cat.controller;
@Controller
@RequestMapping("sponsor")
public class sponsorController {
@Autowired
SponsorService sponsorService;
@Autowired
SentenceService sentenceService;
@GetMapping("")
public String sponsor(Model model,HttpSession session,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "8") int limit){
User user = (User) session.getAttribute(CatConst.USER_SESSION_KEY);
model.addAttribute("info",sponsorService.findPagePostSponsorse(page,limit));
model.addAttribute("indexMax",sponsorService.findindexMaxSponsorse(4));
model.addAttribute("search",null);
Sentence sentence=sentenceService.findSentence();
model.addAttribute("sentence",sentence);
@Override
@Cacheable(value = SPOSORVO_CACHE_NAME, key = "'findPageSponsorVo'+#page+#limit")
public PageInfo<SponsorVo> findPagePostSponsorVo(int page, int limit) {
PageHelper.startPage(page,limit);
List<SponsorVo> sponsorVos=sponsorMapper.querySponsorVoByStatus1(SponsorStatus.Examine.getStatus());
for(int i=0;i<sponsorVos.size();i++){
User user=userMapper.queryByPhone(sponsorVos.get(i).getSponsorPhone());
sponsorVos.get(i).setQq(user.getUserQq());
sponsorVos.get(i).setSponsorUserName(user.getUserNickname());
}
return new PageInfo<SponsorVo>(sponsorVos);
}
}
package com.stray.cat.service.Impl;
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ShareServiceImpl implements ShareService {
private static final String POST_CACHE_NAME = "POST";
private static final String POST_CACHE_NAME_SEARCH = "POST_SEARCH";
@Autowired
HttpSession session;
@Autowired
ShareMapper shareMapper;
@Override
public List<Share> queryShareByPhone(String sharePhone) {
return shareMapper.queryShareByPhone(sharePhone);
@Controller
@RequestMapping("admin/user")
public class adminUserController {
@Autowired
UserService userService;
@Autowired
PostMapper postMapper;
@Autowired
ShareMapper shareMapper;
@Autowired
SponsorMapper sponsorMapper;
@GetMapping("")
public String write(Model model, HttpSession session,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "12") int limit){
model.addAttribute("info",userService.queryUserAll(page,limit));
return "admin/admin_user";
}
@GetMapping("delete")
public String deleteUser(@RequestParam("userId") int userId){
User user=userService.queryUserById(userId);
if(user.getUserPhone().equals("admin")){
return "redirect:/admin/user";
}
postMapper.deletePostByPhone(user.getUserPhone());
shareMapper.deleteShareByPhone(user.getUserPhone());
sponsorMapper.deleteSponsorByPhone(user.getUserPhone());
userService.deleteUser(userId);
return "redirect:/admin/user";
}
}
package com.stray.cat.controller.admin;
@Override
public List<Share> queryShareByPhone(String sharePhone) {
return shareMapper.queryShareByPhone(sharePhone);
}
@Override
public List<Share> queryShareAll() {
return shareMapper.queryShareAll();
}
@Override
public List<Share> findindexMaxShare(int limit) {
return shareMapper.queryShareMaxCount(limit);
}
@Override
public Share queryShareById(int shareId) {
return shareMapper.queryShareById(shareId);
}
@Override
public int addShare(Share share) {
return shareMapper.addShare(share);
}
@Override
public int updateShareCount(int shareCount, int shareId) {
return shareMapper.updateShareCount(shareCount,shareId);
}
@Override
public int updateShare(Share share) {
return shareMapper.updateShare(share);
}
@Override
public int deleteShare(int shareId) {
return shareMapper.deleteShare(shareId);
}
@Override
@Cacheable(value = POST_CACHE_NAME, key = "'findPageShare'+#page+#limit")
public PageInfo<Share> findPageShare(int page, int limit) {
User user= (User) session.getAttribute(CatConst.USER_SESSION_KEY);
PageHelper.startPage(page,limit);
return new PageInfo<>(shareMapper.queryShareByPhone(user.getUserPhone()));
}
@Override
public PageInfo<Share> findPageAllShare(int page, int limit) {
return uploadPicture(file,request,session);
}
//上传
public JsonResult uploadPicture(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request,HttpSession session) {
if (!file.isEmpty()) {
try {
// 获取用户目录
String userPath = System.getProperties().getProperty("user.home") + "/cat/";
// 保存目录
StringBuffer hold = new StringBuffer("upload/");
// 获取时间,以年月创建目录
Date date = DateUtil.date();
hold.append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth() + 1).append("/");
File mediaPath = new File(userPath, hold.toString());
// 如果没有该目录则创建
if (!mediaPath.exists()) {
mediaPath.mkdirs();
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
// 生成文件名称
String nameSuffix = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))
.replaceAll(" ", "_").replaceAll(",", "") + format.format(DateUtil.date())
+ new Random().nextInt(1000);
// 文件后缀
String fileSuffix = file.getOriginalFilename()
.substring(file.getOriginalFilename().lastIndexOf(".") + 1);
// 上传文件名加后缀
String fileName = nameSuffix + "." + fileSuffix;
// 转存文件
file.transferTo(new File(mediaPath.toString(), fileName));
// 原图片路径
StringBuffer originalPath = new StringBuffer();
originalPath.append(mediaPath.getAbsolutePath()).append("/").append(fileName);
// 压缩图片路径
StringBuffer compressPath = new StringBuffer();
compressPath.append(mediaPath.getAbsolutePath()).append("/").append(nameSuffix).append("_small.")
.append(fileSuffix);
// 压缩图片
Thumbnails.of(originalPath.toString()).size(256, 256).keepAspectRatio(false).toFile(compressPath.toString());
// 原图数据库路径
StringBuffer originalDataPath = new StringBuffer();
originalDataPath.append("/").append(hold).append(fileName);
// 压缩图数据库路径
StringBuffer compressDataPath = new StringBuffer();
compressDataPath.append("/").append(hold).append(nameSuffix).append("_small.").append(fileSuffix);
} else if(status.equals(IndexFrom.Sponsor.getDesc())){
i=2;
}
session.setAttribute(CatConst.USER_DETAILS_STATUS,i);
session.setAttribute(CatConst.USER_DETAILS_CODE,id);
model.addAttribute("comments",detailsService.findComment(i,id));
detailsService.addCount(id,i);
model.addAttribute("count",commentMapper.queryCount1(id,i));
model.addAttribute("recommends",detailsService.findRecommends(i));
IndexVo info=detailsService.findDetails(id,i);
model.addAttribute("info",info);
model.addAttribute("url",TitleUtil.getUrl());
List<IndexVo> indexMax=indexService.findMaxCount();
List<Notice> notices=noticeService.queryNotice();
model.addAttribute("notices",notices);
model.addAttribute("indexMax",indexMax);
Sentence sentence=sentenceService.findSentence();
model.addAttribute("sentence",sentence);
return "details";
}
@PostMapping("interceptor/addComment")
public String comment(String comment,Model model){
int i= (int) session.getAttribute(CatConst.USER_DETAILS_STATUS);
int id= (int) session.getAttribute(CatConst.USER_DETAILS_CODE);
User user= (User) session.getAttribute("user_session");
Comment comment1=new Comment();
comment1.setCommentContent(comment);
comment1.setCommentPhone(user.getUserPhone());
comment1.setCommentCreatetime(new Date());
comment1.setCommentPsId(id);
comment1.setCommentSource(i);
commentMapper.addComment(comment1);
model.addAttribute("comments",detailsService.findComment(i,id));
detailsService.addCount(id,i);
model.addAttribute("count",commentMapper.queryCount1(id,i));
model.addAttribute("recommends",detailsService.findRecommends(i));
IndexVo info=detailsService.findDetails(id,i);
model.addAttribute("info",info);
List<IndexVo> indexMax=indexService.findMaxCount();
List<Notice> notices=noticeService.queryNotice();
model.addAttribute("notices",notices);
@Controller
@RequestMapping("admin/user")
public class adminUserController {
@Autowired
UserService userService;
@Autowired
PostMapper postMapper;
@Autowired
ShareMapper shareMapper;
@Autowired
SponsorMapper sponsorMapper;
@GetMapping("")
public String write(Model model, HttpSession session,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "12") int limit){
model.addAttribute("info",userService.queryUserAll(page,limit));
return "admin/admin_user";
}
@GetMapping("delete")
public String deleteUser(@RequestParam("userId") int userId){
User user=userService.queryUserById(userId);
if(user.getUserPhone().equals("admin")){
return "redirect:/admin/user";
}
postMapper.deletePostByPhone(user.getUserPhone());
shareMapper.deleteShareByPhone(user.getUserPhone());
sponsorMapper.deleteSponsorByPhone(user.getUserPhone());
userService.deleteUser(userId);
return "redirect:/admin/user";
}
}
@RequestParam(value = "limit", defaultValue = "10") int limit){
model.addAttribute("status",0);
User user = (User) session.getAttribute(CatConst.USER_SESSION_KEY);
model.addAttribute("info",sponsorService.findPageSponsorTowrite(page,limit));
return "sponsor_writing";
}
@GetMapping("interceptor/writeSponsorStatus")
public String status(@RequestParam("status") int status, Model model,HttpSession session){
model.addAttribute("status",status);
if(status==0){
return "redirect:/sponsor/interceptor/writeSponsor";
}else {
return "sponsor_writing";
}
}
@PostMapping("interceptor/writeSponsor")
@ResponseBody
public JsonResult writePost(Sponsor sponsor, HttpSession session){
User user= (User) session.getAttribute(CatConst.USER_SESSION_KEY);
sponsor.setSponsorPhone(user.getUserPhone());
sponsor.setSponsorCreatetime(new Date());
sponsor.setSponsorStatus(0);
int i=sponsorService.addSponsor(sponsor);
if(i==1){
return new JsonResult(true,"发布成功!");
}else {
return new JsonResult(false, "发布失败!");
}
}
@GetMapping("interceptor/deleteSponsor")
public String deletePost(@RequestParam("sponsorId") int sponsorId){
sponsorService.deletesponsor(sponsorId);
return "redirect:/sponsor/interceptor/writeSponsor";
}
@GetMapping("interceptor/updateSponsor")
public String updatePost(@RequestParam("sponsorId") int sponsorId,Model model){
User user = (User) session.getAttribute(CatConst.USER_SESSION_KEY);
model.addAttribute("info",postService.findPagePost(page,limit));
return "revelation_writing";
}
@GetMapping("interceptor/writeRevelationStatus")
public String status(@RequestParam("status") int status, Model model,HttpSession session){
model.addAttribute("status",status);
if(status==0){
return "redirect:/revelation/interceptor/writeRevelation";
}else {
return "revelation_writing";
}
}
@PostMapping("interceptor/writePost")
@ResponseBody
public JsonResult writePost(Post post, HttpSession session){
User user= (User) session.getAttribute(CatConst.USER_SESSION_KEY);
post.setPostPhone(user.getUserPhone());
post.setPostCreatetime(new Date());
int i=postService.addPost(post);
if(i==1){
return new JsonResult(true,"发布成功!");
}else {
return new JsonResult(false, "发布失败!");
}
}
@GetMapping("interceptor/deletePost")
public String deletePost(@RequestParam("postId") int postId){
postService.deletePost(postId);
return "redirect:/revelation/interceptor/writeRevelation";
}
@GetMapping("interceptor/updatePost")
public String updatePost(@RequestParam("postId") int postId,Model model){
Post post=postService.queryPostById(postId);
System.out.println(post);
model.addAttribute("post",post);
model.addAttribute("status",1);
return "revelation_writing";
}
@GetMapping
public String index(Model model,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "12") int limit){
model.addAttribute("info",noticeService.findPageNotice(page,limit));
return "admin/admin_notice";
}
@GetMapping("deleteNotice")
public String deleteNotice(int noticeId){
noticeService.deleteNotice(noticeId);
return "redirect:/admin/notice";
}
@PostMapping("addNotice")
@ResponseBody
public JsonResult writePost(String noticeContent){
Notice notice=new Notice();
notice.setNoticeContent(noticeContent);
notice.setNoticeCreatetime(new Date());
int i=noticeService.addNotice(notice);
if(i==1){
return new JsonResult(true,"添加成功!");
}else {
return new JsonResult(false, "添加失败!");
}
}
}
package com.stray.cat.controller;
public String deleteReport2(int id){
reportService.deleteReport(id);
return "redirect:/admin/report";
}
}
package com.stray.cat.service.Impl;
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class PostServiceImpl implements PostService {
private static final String POST_CACHE_NAME = "POST";
private static final String POST_CACHE_NAME_SEARCH = "POST_SEARCH";
@Autowired
HttpSession session;
@Autowired
PostMapper postMapper;
@Override
public List<Post> queryPostByPhone(String postPhone) {
return postMapper.queryPostByPhone(postPhone);
}
@Override
public List<Post> queryPostAll() {
return postMapper.queryPostAll();
}
@Override
public Post queryPostById(int postId) {
return postMapper.queryPostById(postId);
}
public JsonResult register(User user,@RequestParam(value ="userCode") String userCode,HttpSession session){
String code=session.getAttribute(CatConst.USER_SESSION_CODE).toString();
if(code.equalsIgnoreCase(userCode)){
if(userService.queryByPhone(user.getUserPhone())!=null){
return new JsonResult(false, "电话号码已经注册!");
}else {
user.setUserPassword(MD5Util.getMD5(user.getUserPassword()));
user.setUserCreatetime(new Date());
user.setUserUrl(TitleUtil.getUrl());
int i=userService.addUser(user);
if(i==1){
session.removeAttribute(CatConst.USER_SESSION_CODE);
return new JsonResult(true, "注册成功!");
}else {
return new JsonResult(false, "注册失败!");
}
}
}else {
return new JsonResult(false, "验证码错误!");
}
}
@PostMapping("login/updatePassword")
@ResponseBody
public JsonResult updatePassword(@RequestParam(value ="userPhone") String userPhone,
@RequestParam(value ="userCode") String userCode,
@RequestParam(value ="userPassword") String userPassword,HttpSession session){
String code=session.getAttribute(CatConst.USER_SESSION_CODE).toString();
if(code.equalsIgnoreCase(userCode)){
int i=userService.updateUserPhone(MD5Util.getMD5(userPassword),userPhone);
if(i==1){
session.removeAttribute(CatConst.USER_SESSION_CODE);
return new JsonResult(true, "密码更新成功!");
}else {
return new JsonResult(false, "密码更新失败!");
}
}else {
return new JsonResult(false, "验证码错误!");
}
}
@PostMapping("login/getCode")
@ResponseBody
public JsonResult getCode(@RequestParam(value ="userPhone") String userPhone,HttpSession session){
DuanxinService duanxinService=new DuanxinService();
// int code=duanxinService.duanXin(userPhone);
session.setAttribute(CatConst.USER_SESSION_CODE,"1234");
return new JsonResult(true,"发送成功");
}