因为有作业,所以我很“情愿”的就写完了这个项目,只为了学习知识,绝不是为了交作业,绝不是。
首先是导入依赖的问题需要的依赖:Lombok、Spring Web、Themleaf、MySQL Driver、MyBatis framework。
@Data:导入getter()、setter()方法
@AllArgsConstructor:导入全参构造
@NoArgsConstructor:导入无参构造
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface LibraryMapper {
// 添加图书
boolean AddBook(Library library);
// 图书查询(包括单条件查询,多条件查询,全条件查询)
List QueryBook(Library library);
// 删除图书信息
@Delete("Delete from book where id = #{id}")
int deleteBook(Integer id);
// 更新图书信息
@Update("update book set name = #{name},price=#{price} where id=#{id}")
int updateBook(Integer id);
}
2.Mapper.xml
通过配置文件进行CRUD操作,也可以使用注解。
LibraryMapper.xml
3.Controller层insert into book (id, name, price) values (#{id}, #{name}, #{price})
控制层,实现业务调度
LibController
package nuc.edu.controller;
import nuc.edu.entity.Library;
import nuc.edu.service.LibService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class LibController {
@Autowired
private LibService libService;
@GetMapping("/addbook")
public String AddBook(@RequestParam(value = "id",required = false ) Integer id,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "price",required = false) Integer price,
Model model){
if("".equals(id)){
id = null;
}
if ("".equals(price)){
price = null;
}
libService.AddBook(new Library(id, name, price));
//model.addAttribute("libraries",bookList);
return "redirect:/library";
}
@GetMapping ("/library")
public String QueryBook(@RequestParam(value = "id",required = false) Integer id,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "price",required = false) Integer price,
Model model){
if (name != ""){
name = null;
}
Library library = new Library(id, name, price);
List libraryList = libService.QueryBook(library);
model.addAttribute("libraries",libraryList);
return "index";
}
@GetMapping("/toadd")
public String ToAddBook(){
return "addbook";
}
@GetMapping("/deletebook/{id}")
public String deleteBook(@PathVariable Integer id){
libService.deleteBook(id);
return "index";
}
@GetMapping("/updateBook")
public String updateBook(@RequestParam(value = "id",required = false) Integer id,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "price",required = false) Integer price){
libService.updateBook(new Library(id,name,price));
return "redirect:/library";
}
@GetMapping("/toupdate/{id}/{name}/{price}")
public String toupdate(@PathVariable Integer id,
@PathVariable String name,
@PathVariable Integer price,
Model model){
System.out.println(id);
Library library = new Library(id,name,price);
model.addAttribute("library",library);
return "update";
}
}
4.前端页面
(1)主界面
index
Title
| Id | Name | Price | |
|---|---|---|---|
添加图书
(3)图书更新界面
更新界面
二、文件上传
1.Controller层
FileController
package nuc.edu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Controller
public class FileController {
@GetMapping("/upload")
public String uploadPage(){
return "upload";
}
@PostMapping("upload")
public Map upload(@RequestBody MultipartFile photo){
String path = "D:\IDEA\IDEA-workspace\MyBatis\src\main\resources\static\img\";//保存路径
String filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
if (!suffix.equals(".jpg")){
return dealResultMap(false,"上传失败");
}
try{
FileCopyUtils.copy(photo.getInputStream(), new FileOutputStream(path + filename +suffix));
} catch (IOException e){
e.printStackTrace();
return dealResultMap(false,"上传失败");
}
return dealResultMap(true,"上传成功");
}
private Map dealResultMap(boolean success, String msg){
Map result = new HashMap();
result.put("success", success);
result.put("msg", msg);
return result;
}
}
2.前端页面
upload
文件上传
三、登录管理
1.前端界面
(1)登录界面(包括用户与管理员)
login.html
登录
adminLogin.html
管理员登录
(2)用户注册界面
register.html
注册
(3)Controller层
LoginController
package nuc.edu.controller;
import nuc.edu.entity.Admin;
import nuc.edu.entity.User;
import nuc.edu.mapper.AdminMapper;
import nuc.edu.service.AdminService;
import nuc.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@Autowired
AdminService adminService;
@Autowired
UserService userService;
@GetMapping("/login")
public String toLogin(){
return "login";
}
@GetMapping("/checklogin")
public String login(@RequestParam(value = "username",required = false ) String username,
@RequestParam(value = "password",required = false) String password,
Model model,
HttpSession session){
User user = userService.QueryUser(new User(username,password));
if (user!=null){
session.setAttribute("LoginUser",username);
return "redirect:/ulibrary";
}else {
model.addAttribute("msg","用户名或密码错误!");
return "login";
}
}
@GetMapping("/adminLogin")
public String toAdminLogin(){
return "adminLogin";
}
@GetMapping("/checkAdminLogin")
public String adminLogin(@RequestParam(value = "name",required = false ) String name,
@RequestParam(value = "password",required = false) String password,
Model model,
HttpSession session){
Admin admin = adminService.Queryadmin(new Admin(name,password));
if (admin != null){
session.setAttribute("LoginUser",name);
return "redirect:/library";
}else {
model.addAttribute("msg","用户名或密码错误!");
return "adminLogin";
}
}
@GetMapping("/toregister")
public String toregister(){
return "register";
}
@GetMapping("/register")
public String registerUser(@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "password", required = false) String password){
userService.AddUser(new User(name,password));
return "redirect:/login";
}
}
四、总结
放一张某位大佬给讲的图,debug很有用。直接上图



