栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

根据springboot项目基础整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据springboot项目基础整合mybatis

因为有作业,所以我很“情愿”的就写完了这个项目,只为了学习知识,绝不是为了交作业,绝不是。

首先是导入依赖的问题

需要的依赖:Lombok、Spring Web、Themleaf、MySQL Driver、MyBatis framework。

实体类中需要用到的注释

@Data:导入getter()、setter()方法
@AllArgsConstructor:导入全参构造
@NoArgsConstructor:导入无参构造

一、图书信息的管理(CRUD) 1.Mapper层(也就是Dao层)
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





    
        insert into book (id, name, price) values (#{id}, #{name}, #{price})
    

    

3.Controller层

控制层,实现业务调度
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
Id Name Price
(2)图书添加界面 addbook



    
    添加图书


图书编号ID 图书名称Name 价格Price
(3)图书更新界面



    
    更新界面


图书ID 图书名称Name 价格Price
二、文件上传 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很有用。直接上图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602933.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号