主函数:
package com.pet.hospital;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import javax.sql.DataSource;
@SpringBootApplication
@MapperScan("com.pet.hospital.mapper")
public class HospitalApplication {
public static void main(String[] args) {
SpringApplication.run(HospitalApplication.class, args);
}
}
上传控制器:
package com.pet.hospital.controller;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
@Controller
public class UploadController {
@RequestMapping("/uploadImg")
@ResponseBody
public JSONObject uploadImg(MultipartFile file, HttpServletRequest request) throws Exception {
JSONObject res = new JSONObject();
JSONObject resUrl = new JSONObject();
//判断文件是否为空
if (file.isEmpty()) {
System.out.println("图片为空");
}
String ext = FilenameUtils.getExtension(file.getOriginalFilename());
String filename = UUID.randomUUID().toString().replaceAll("-", "") +"."+ ext;
String pathName = "D:\\file\\" + filename;
file.transferTo(new File(pathName));
res.put("msg", "");
res.put("code", 0);
resUrl.put("src", "/pic/" + filename);
res.put("data", resUrl);
return res;
}
}
宠物控制器:
package com.pet.hospital.controller;
import com.github.pagehelper.PageInfo;
import com.pet.hospital.constants.SysConstant;
import com.pet.hospital.domain.Pet;
import com.pet.hospital.service.PetService;
import com.pet.hospital.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/pet")
public class PetController {
@Autowired
PetService petService;
@RequestMapping("/add")
@ResponseBody
public R add(Pet pet) {
int res = petService.add(pet);
if (res != 0) {
return R.exeSuccess();
}
return R.exeFail();
}
@RequestMapping("/list")
@ResponseBody
public R list(Pet pet, @RequestParam(required = false, defaultValue = "10") int limit, @RequestParam(required = false, defaultValue = "1") int page) {
PageInfo<Pet> pageInfo = petService.list(pet, limit, page);
if (pageInfo.getList() != null && pageInfo.getList().size() != 0) {
return R.exeSuccess(pageInfo.getList(), pageInfo.getTotal());
}
return R.exeFail(SysConstant.queryFail);
}
@RequestMapping("/delete")
@ResponseBody
public R delete(String id) {
if (id != null) {
int res = petService.delete(id);
if (res != 0) {
return R.exeSuccess();
}
}
return R.exeFail(SysConstant.parameterMsg);
}
@RequestMapping("/update")
public String update(String id, Model model) {
Pet pet = petService.update(id);
model.addAttribute("pet", pet);
return "pet/update";
}
@RequestMapping("/do_update")
@ResponseBody
public R doUpdate(Pet pet) {
int res = petService.doUpdate(pet);
if (res != 0) {
return R.exeSuccess();
}
return R.exeFail();
}
}
登录控制器:
package com.pet.hospital.controller;
import com.pet.hospital.constants.SysConstant;
import com.pet.hospital.domain.Account;
import com.pet.hospital.domain.Doctor;
import com.pet.hospital.service.AccountRelationService;
import com.pet.hospital.service.LoginService;
import com.pet.hospital.utils.R;
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class LoginController {
@Autowired
LoginService loginService;
@Autowired
AccountRelationService accountRelationService;
@RequestMapping("/doLogin")
@ResponseBody
public R doLogin(Account account, HttpSession session) {
Account res = loginService.doLogin(account);
if (res != null) {
if (SysConstant.accountDoctorType.equals(account.getIdentityId())) {
//医生身份 保存session
Long doctorId = accountRelationService.queryByAccountId(res.getId());
if (doctorId != null && doctorId != 0) {
session.setAttribute("doctorId", doctorId);
}
}
session.setAttribute("account", account.getAccount());
return R.exeSuccess();
}
return R.exeFail();
}
@RequestMapping("/logout")
public void logOut(HttpSession session, HttpServletRequest request, HttpServletResponse response){
session.removeAttribute("account");
try {
request.getRequestDispatcher("/login").forward(request,response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
更多源码参考完整源码下载: