这是一个基于SpringBoot+mybatis的增删改查Demo,其功能是可以对书籍进行增删改查这些基本的操作。
开发工具:IDEA
核心技术:SpringBoot+mybatis;
前端:Bootstrap;
数据库:mysql;
数据库连接池:druid;
模板引擎:Thymeleaf;
@Controller
public class UserController {
@Autowired
UserService userService;
/**
* 用户名检查 不能注册相同用户名
* @param username
* @return
*/
@RequestMapping("/checkName.do")
@ResponseBody
public String checkName(@RequestParam("username") String username) {
User user = userService.getUserByName(username);
if (user == null) {
return "success";
}
return "false";
}
/**
* 注册功能
* @param username
* @param password
* @param email
* @return
*/
@RequestMapping("/addUser.do")
@ResponseBody
public int addUser(@RequestParam("userName") String username,
@RequestParam("password") String password,
@RequestParam("email") String email) {
int result = userService.addUser(username, password, email);
int flag = 0;
if (result > 0) {
flag = 1;
return flag;
}
return flag;
}
/**
* 密码找回功能
* @param username
* @param email
* @return
*/
@RequestMapping("/findPwd.do")
@ResponseBody
public int findPwd(@RequestParam("userName") String username,
@RequestParam("email") String email) {
User user = userService.findPwd(username, email);
Integer pwd = 0;
if (user != null) {
@Controller
public class BookController {
@Autowired
private BookService bookService;
/**
* 获取全部书籍信息
* @param pn
* @return
*/
@RequestMapping("/getAllBook")
@ResponseBody
public PageInfo<Book> getAllBook(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
//在查询之前只需要调用,传入页码,以及每页的大小
PageHelper.startPage(pn, 3);
//startPage后面紧跟的这个查询就是一个分页查询
List<Book> books = bookService.getAllBook();
//使用pageInfo包装查询后的结果,只需要将pageInfo交给页面就行了
//封装了详细的分页信息,包括我们查询出来的数据,传入连续显示的页数
return new PageInfo(books, 5);
}
/**
* 添加书籍功能
* @param bookPic
* @param bookName
* @param bookType
* @param bookIntro
* @param model
* @return
*/
@RequestMapping("/addBook")
@ResponseBody
public String addBook(@RequestPart("bookPic") MultipartFile bookPic,
@RequestParam("bookName") String bookName,
@RequestParam("bookType") Integer bookType,
@RequestParam("bookIntro") String bookIntro,
Model model) {
Book book = new Book();
try {
//根据创建时间对文件进行重命名
String fileName = System.currentTimeMillis() + bookPic.getOriginalFilename();
//上传文件存储的位置
String destFileName = "D://BookPic/images/" + File.separator + fileName;
//防止改文件夹不存在,创建一个新文件夹
File destFile = new File(destFileName);
destFile.getParentFile().mkdirs();
//将文件存储到该位置
bookPic.transferTo(destFile);
//传递文件
model.addAttribute("fileName", fileName);
//将文件名存储到数据库中,以便查询调用
book.setBookPic(fileName);
String pic = book.getBookPic();
bookService.addBook(bookName, bookType, bookIntro, pic);
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
}
return "success";
}
/**
* 删除书籍功能
* @param bookId
* @return
*/
@RequestMapping("/delBook")
@ResponseBody
public int delBook(@RequestParam(value = "bookId") Integer bookId) {
int result = bookService.delBook(bookId);
int flag = 0;
if (result > 0) {
flag = 1;
}
return flag;
}
/**
* 根据id查询书籍信息
* @param bookId
* @return
*/
@RequestMapping("/queryBookById")
@ResponseBody
public Book updateBook(@RequestParam(value = "bookId") Integer bookId) {
return bookService.queryBookById(bookId);
}
/**
* 批量删除书籍功能
* @param ids
* @return
*/
@RequestMapping("/delAllBook")
@ResponseBody
public int delAllBook(@RequestParam(value = "ids") String ids) {
int result = 0;
//批量删除
if(ids.contains("-")){
List<Integer> del_ids = new ArrayList<>();
String[] str_ids = ids.split("-");
//组装id的集合
for (String string : str_ids) {
del_ids.add(Integer.parseInt(string));
}
result = bookService.deleteBatch(del_ids);
} else{
Integer id = Integer.parseInt(ids);
result = bookService.delBook(id);
}
int flag = 0;
if (result > 0) {
flag = 1;
}
return flag;
}
/**
* 修改书籍功能
* @param book
* @return
*/
@RequestMapping("/updateBookById/{bookId}")
@ResponseBody
public int updateBookById(Book book) {
int result = bookService.updateBookById(book);
int flag = 0;
if (result > 0) {
flag = 1;
}
return flag;
}
/**
* 根据输入信息查询书籍功能
* @param pn
* @param request
* @return
*/
@RequestMapping("/searchBook")
@ResponseBody
public PageInfo<Book> searchBook(@RequestParam(value = "pn", defaultValue = "1") Integer pn, HttpServletRequest request) {
String searchBook = request.getParameter("searchBook");
//在查询之前只需要调用,传入页码,以及每页的大小
PageHelper.startPage(pn, 3);
//startPage后面紧跟的这个查询就是一个分页查询
List<Book> books = bookService.searchBook(searchBook);
//使用pageInfo包装查询后的结果,只需要将pageInfo交给页面就行了
//封装了详细的分页信息,包括我们查询出来的数据,传入连续显示的页数
return new PageInfo(books, 5);
}
}
//拦截器
@Configuration
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pic/**").addResourceLocations("file:D://BookPic/images/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") //所有请求都被拦截,包括静态资源
.excludePathPatterns("/", "/*.do", "/assets/**", "/bootstrap-3.4.1-dist/**", "/css/**", "/images/**", "/js/**", "/white/**", "/favicon.ico"); //放行的请求
}
}
Demo之Gitee地址:基于SpringBoot+mybatis的增删改查Demo
Demo之GitHub地址:基于SpringBoot+mybatis的增删改查Demo