欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 实战图书管理系统的实现流程

程序员文章站 2022-03-15 19:32:31
一、项目简述功能包括(管理员和学生角色): 管理员和学生登录,图书管理,图书添加删除修改,图书 借阅,图书归还,图书查看,学生管理,借还管理,读者 管理,密码管理,信息修改等等功能。二、项目运行环境配...

一、项目简述

功能包括(管理员和学生角色): 管理员和学生登录,图书管理,图书添加删除修改,图书 借阅,图书归还,图书查看,学生管理,借还管理,读者 管理,密码管理,信息修改等等功能。

二、项目运行

环境配置: jdk1.8 + tomcat8.5 + mysql + eclispe (intellij idea,eclispe,myeclispe,sts 都支持)

项目技术: jsp +spring + springmvc + mybatis + html+ css + javascript + jquery + ajax + layui+ maven等等。

Java 实战图书管理系统的实现流程

Java 实战图书管理系统的实现流程

Java 实战图书管理系统的实现流程

Java 实战图书管理系统的实现流程

读者信息控制器:

@controller
public class readercontroller {
    @autowired
    private readerinfoservice readerinfoservice;
 
    @autowired
    private loginservice loginservice;
 
    @autowired
    private readercardservice readercardservice;
 
    private readerinfo getreaderinfo(long readerid, string name, string sex, string birth, string address, string phone) {
        readerinfo readerinfo = new readerinfo();
        date date = new date();
        try {
            simpledateformat df = new simpledateformat("yyyy-mm-dd");
            date = df.parse(birth);
        } catch (parseexception e) {
            e.printstacktrace();
        }
        readerinfo.setaddress(address);
        readerinfo.setname(name);
        readerinfo.setreaderid(readerid);
        readerinfo.setphone(phone);
        readerinfo.setsex(sex);
        readerinfo.setbirth(date);
        return readerinfo;
    }
 
    @requestmapping("allreaders.html")
    public modelandview allbooks() {
        arraylist<readerinfo> readers = readerinfoservice.readerinfos();
        modelandview modelandview = new modelandview("admin_readers");
        modelandview.addobject("readers", readers);
        return modelandview;
    }
 
    @requestmapping("reader_delete.html")
    public string readerdelete(httpservletrequest request, redirectattributes redirectattributes) {
        long readerid = long.parselong(request.getparameter("readerid"));
        if (readerinfoservice.deletereaderinfo(readerid) && readercardservice.deletereadercard(readerid)) {
            redirectattributes.addflashattribute("succ", "删除成功!");
        } else {
            redirectattributes.addflashattribute("error", "删除失败!");
        }
        return "redirect:/allreaders.html";
    }
 
    @requestmapping("/reader_info.html")
    public modelandview toreaderinfo(httpservletrequest request) {
        readercard readercard = (readercard) request.getsession().getattribute("readercard");
        readerinfo readerinfo = readerinfoservice.getreaderinfo(readercard.getreaderid());
        modelandview modelandview = new modelandview("reader_info");
        modelandview.addobject("readerinfo", readerinfo);
        return modelandview;
    }
 
    @requestmapping("reader_edit.html")
    public modelandview readerinfoedit(httpservletrequest request) {
        long readerid = long.parselong(request.getparameter("readerid"));
        readerinfo readerinfo = readerinfoservice.getreaderinfo(readerid);
        modelandview modelandview = new modelandview("admin_reader_edit");
        modelandview.addobject("readerinfo", readerinfo);
        return modelandview;
    }
 
    @requestmapping("reader_edit_do.html")
    public string readerinfoeditdo(httpservletrequest request, string name, string sex, string birth, string address, string phone, redirectattributes redirectattributes) {
        long readerid = long.parselong(request.getparameter("readerid"));
        readerinfo readerinfo = getreaderinfo(readerid, name, sex, birth, address, phone);
        if (readerinfoservice.editreaderinfo(readerinfo) && readerinfoservice.editreadercard(readerinfo)) {
            redirectattributes.addflashattribute("succ", "读者信息修改成功!");
        } else {
            redirectattributes.addflashattribute("error", "读者信息修改失败!");
        }
        return "redirect:/allreaders.html";
    }
 
    @requestmapping("reader_add.html")
    public modelandview readerinfoadd() {
        return new modelandview("admin_reader_add");
    }
 
    @requestmapping("reader_add_do.html")
    public string readerinfoadddo(string name, string sex, string birth, string address, string phone, string password, redirectattributes redirectattributes) {
        readerinfo readerinfo = getreaderinfo(0, name, sex, birth, address, phone);
        long readerid = readerinfoservice.addreaderinfo(readerinfo);
        readerinfo.setreaderid(readerid);
        if (readerid > 0 && readercardservice.addreadercard(readerinfo, password)) {
            redirectattributes.addflashattribute("succ", "添加读者信息成功!");
        } else {
            redirectattributes.addflashattribute("succ", "添加读者信息失败!");
        }
        return "redirect:/allreaders.html";
    }
 
    @requestmapping("reader_info_edit.html")
    public modelandview readerinfoeditreader(httpservletrequest request) {
        readercard readercard = (readercard) request.getsession().getattribute("readercard");
        readerinfo readerinfo = readerinfoservice.getreaderinfo(readercard.getreaderid());
        modelandview modelandview = new modelandview("reader_info_edit");
        modelandview.addobject("readerinfo", readerinfo);
        return modelandview;
    }
 
    @requestmapping("reader_edit_do_r.html")
    public string readerinfoeditdoreader(httpservletrequest request, string name, string sex, string birth, string address, string phone, redirectattributes redirectattributes) {
        readercard readercard = (readercard) request.getsession().getattribute("readercard");
        readerinfo readerinfo = getreaderinfo(readercard.getreaderid(), name, sex, birth, address, phone);
        if (readerinfoservice.editreaderinfo(readerinfo) && readerinfoservice.editreadercard(readerinfo)) {
            readercard readercardnew = loginservice.findreadercardbyreaderid(readercard.getreaderid());
            request.getsession().setattribute("readercard", readercardnew);
            redirectattributes.addflashattribute("succ", "信息修改成功!");
        } else {
            redirectattributes.addflashattribute("error", "信息修改失败!");
        }
        return "redirect:/reader_info.html";
    }
}

登陆控制层:

@controller
public class logincontroller {
 
    private loginservice loginservice;
 
 
    @autowired
    public void setloginservice(loginservice loginservice) {
        this.loginservice = loginservice;
    }
 
 
    @requestmapping(value = {"/", "/login.html"})
    public string tologin(httpservletrequest request) {
        request.getsession().invalidate();
        return "index";
    }
 
    @requestmapping("/logout.html")
    public string logout(httpservletrequest request) {
        request.getsession().invalidate();
        return "redirect:/login.html";
    }
 
 
    //负责处理logincheck.html请求
    //请求参数会根据参数名称默认契约自动绑定到相应方法的入参中
    @requestmapping(value = "/api/logincheck", method = requestmethod.post)
    public @responsebody
    object logincheck(httpservletrequest request) {
        long id = long.parselong(request.getparameter("id"));
        string passwd = request.getparameter("passwd");
        boolean isreader = loginservice.hasmatchreader(id, passwd);
        boolean isadmin = loginservice.hasmatchadmin(id, passwd);
        hashmap<string, string> res = new hashmap<>();
        if (isadmin) {
            admin admin = new admin();
            admin.setadminid(id);
            admin.setpassword(passwd);
            string username = loginservice.getadminusername(id);
            admin.setusername(username);
            request.getsession().setattribute("admin", admin);
            res.put("statecode", "1");
            res.put("msg", "管理员登陆成功!");
        } else if (isreader) {
            readercard readercard = loginservice.findreadercardbyreaderid(id);
            request.getsession().setattribute("readercard", readercard);
            res.put("statecode", "2");
            res.put("msg", "读者登陆成功!");
        } else {
            res.put("statecode", "0");
            res.put("msg", "账号或密码错误!");
        }
        return res;
    }
 
    @requestmapping("/admin_main.html")
    public modelandview toadminmain(httpservletresponse response) {
        return new modelandview("admin_main");
    }
 
    @requestmapping("/reader_main.html")
    public modelandview toreadermain(httpservletresponse response) {
        return new modelandview("reader_main");
    }
 
    @requestmapping("/admin_repasswd.html")
    public modelandview readminpasswd() {
        return new modelandview("admin_repasswd");
    }
 
    @requestmapping("/admin_repasswd_do")
    public string readminpasswddo(httpservletrequest request, string oldpasswd, string newpasswd, string renewpasswd, redirectattributes redirectattributes) {
        admin admin = (admin) request.getsession().getattribute("admin");
        long id = admin.getadminid();
        string password = loginservice.getadminpassword(id);
        if (password.equals(oldpasswd)) {
            if (loginservice.adminrepassword(id, newpasswd)) {
                redirectattributes.addflashattribute("succ", "密码修改成功!");
                return "redirect:/admin_repasswd.html";
            } else {
                redirectattributes.addflashattribute("error", "密码修改失败!");
                return "redirect:/admin_repasswd.html";
            }
        } else {
            redirectattributes.addflashattribute("error", "旧密码错误!");
            return "redirect:/admin_repasswd.html";
        }
    }
 
    @requestmapping("/reader_repasswd.html")
    public modelandview rereaderpasswd() {
        return new modelandview("reader_repasswd");
    }
 
    @requestmapping("/reader_repasswd_do")
    public string rereaderpasswddo(httpservletrequest request, string oldpasswd, string newpasswd, string renewpasswd, redirectattributes redirectattributes) {
        readercard reader = (readercard) request.getsession().getattribute("readercard");
        long id = reader.getreaderid();
        string password = loginservice.getreaderpassword(id);
        if (password.equals(oldpasswd)) {
            if (loginservice.readerrepassword(id, newpasswd)) {
                redirectattributes.addflashattribute("succ", "密码修改成功!");
                return "redirect:/reader_repasswd.html";
            } else {
                redirectattributes.addflashattribute("error", "密码修改失败!");
                return "redirect:/reader_repasswd.html";
            }
        } else {
            redirectattributes.addflashattribute("error", "旧密码错误!");
            return "redirect:/reader_repasswd.html";
        }
    }
 
    //配置404页面
    @requestmapping("*")
    public string notfind() {
        return "404";
    }
 
}

图书控制层:

@controller
public class bookcontroller {
    @autowired
    private bookservice bookservice;
    @autowired
    private lendservice lendservice;
 
    private date getdate(string pubstr) {
        try {
            simpledateformat df = new simpledateformat("yyyy-mm-dd");
            return df.parse(pubstr);
        } catch (parseexception e) {
            e.printstacktrace();
            return new date();
        }
    }
 
    @requestmapping("/querybook.html")
    public modelandview querybookdo(string searchword) {
        if (bookservice.matchbook(searchword)) {
            arraylist<book> books = bookservice.querybook(searchword);
            modelandview modelandview = new modelandview("admin_books");
            modelandview.addobject("books", books);
            return modelandview;
        } else {
            return new modelandview("admin_books", "error", "没有匹配的图书");
        }
    }
 
    @requestmapping("/reader_querybook_do.html")
    public modelandview readerquerybookdo(string searchword) {
        if (bookservice.matchbook(searchword)) {
            arraylist<book> books = bookservice.querybook(searchword);
            modelandview modelandview = new modelandview("reader_books");
            modelandview.addobject("books", books);
            return modelandview;
        } else {
            return new modelandview("reader_books", "error", "没有匹配的图书");
        }
    }
 
    @requestmapping("/admin_books.html")
    public modelandview adminbooks() {
        arraylist<book> books = bookservice.getallbooks();
        modelandview modelandview = new modelandview("admin_books");
        modelandview.addobject("books", books);
        return modelandview;
    }
 
    @requestmapping("/book_add.html")
    public modelandview addbook() {
        return new modelandview("admin_book_add");
    }
 
    @requestmapping("/book_add_do.html")
    public string addbookdo(@requestparam(value = "pubstr") string pubstr, book book, redirectattributes redirectattributes) {
        book.setpubdate(getdate(pubstr));
        if (bookservice.addbook(book)) {
            redirectattributes.addflashattribute("succ", "图书添加成功!");
        } else {
            redirectattributes.addflashattribute("succ", "图书添加失败!");
        }
        return "redirect:/admin_books.html";
    }
 
    @requestmapping("/updatebook.html")
    public modelandview bookedit(httpservletrequest request) {
        long bookid = long.parselong(request.getparameter("bookid"));
        book book = bookservice.getbook(bookid);
        modelandview modelandview = new modelandview("admin_book_edit");
        modelandview.addobject("detail", book);
        return modelandview;
    }
 
    @requestmapping("/book_edit_do.html")
    public string bookeditdo(@requestparam(value = "pubstr") string pubstr, book book, redirectattributes redirectattributes) {
        book.setpubdate(getdate(pubstr));
        if (bookservice.editbook(book)) {
            redirectattributes.addflashattribute("succ", "图书修改成功!");
        } else {
            redirectattributes.addflashattribute("error", "图书修改失败!");
        }
        return "redirect:/admin_books.html";
    }
 
    @requestmapping("/admin_book_detail.html")
    public modelandview adminbookdetail(httpservletrequest request) {
        long bookid = long.parselong(request.getparameter("bookid"));
        book book = bookservice.getbook(bookid);
        modelandview modelandview = new modelandview("admin_book_detail");
        modelandview.addobject("detail", book);
        return modelandview;
    }
 
    @requestmapping("/reader_book_detail.html")
    public modelandview readerbookdetail(httpservletrequest request) {
        long bookid = long.parselong(request.getparameter("bookid"));
        book book = bookservice.getbook(bookid);
        modelandview modelandview = new modelandview("reader_book_detail");
        modelandview.addobject("detail", book);
        return modelandview;
    }
 
    @requestmapping("/admin_header.html")
    public modelandview admin_header() {
        return new modelandview("admin_header");
    }
 
    @requestmapping("/reader_header.html")
    public modelandview reader_header() {
        return new modelandview("reader_header");
    }
 
    @requestmapping("/reader_books.html")
    public modelandview readerbooks(httpservletrequest request) {
        arraylist<book> books = bookservice.getallbooks();
        readercard readercard = (readercard) request.getsession().getattribute("readercard");
        arraylist<lend> myalllendlist = lendservice.mylendlist(readercard.getreaderid());
        arraylist<long> mylendlist = new arraylist<>();
        for (lend lend : myalllendlist) {
            // 是否已归还
            if (lend.getbackdate() == null) {
                mylendlist.add(lend.getbookid());
            }
        }
        modelandview modelandview = new modelandview("reader_books");
        modelandview.addobject("books", books);
        modelandview.addobject("mylendlist", mylendlist);
        return modelandview;
    }
}

借书控制层:

@controller
public class lendcontroller {
    @autowired
    private lendservice lendservice;
 
    @autowired
    private bookservice bookservice;
 
    @requestmapping("/deletebook.html")
    public string deletebook(httpservletrequest request, redirectattributes redirectattributes) {
        long bookid = long.parselong(request.getparameter("bookid"));
        if (bookservice.deletebook(bookid)) {
            redirectattributes.addflashattribute("succ", "图书删除成功!");
        } else {
            redirectattributes.addflashattribute("error", "图书删除失败!");
        }
        return "redirect:/admin_books.html";
    }
 
    @requestmapping("/lendlist.html")
    public modelandview lendlist(httpservletrequest request) {
        modelandview modelandview = new modelandview("admin_lend_list");
        modelandview.addobject("list", lendservice.lendlist());
        return modelandview;
    }
 
    @requestmapping("/mylend.html")
    public modelandview mylend(httpservletrequest request) {
        readercard readercard = (readercard) request.getsession().getattribute("readercard");
        modelandview modelandview = new modelandview("reader_lend_list");
        modelandview.addobject("list", lendservice.mylendlist(readercard.getreaderid()));
        return modelandview;
    }
 
    @requestmapping("/deletelend.html")
    public string deletelend(httpservletrequest request, redirectattributes redirectattributes) {
        long sernum = long.parselong(request.getparameter("sernum"));
        if (lendservice.deletelend(sernum) > 0) {
            redirectattributes.addflashattribute("succ", "记录删除成功!");
        } else {
            redirectattributes.addflashattribute("error", "记录删除失败!");
        }
        return "redirect:/lendlist.html";
    }
 
    @requestmapping("/lendbook.html")
    public string booklend(httpservletrequest request, redirectattributes redirectattributes) {
        long bookid = long.parselong(request.getparameter("bookid"));
        long readerid = ((readercard) request.getsession().getattribute("readercard")).getreaderid();
        if (lendservice.lendbook(bookid, readerid)) {
            redirectattributes.addflashattribute("succ", "图书借阅成功!");
        } else {
            redirectattributes.addflashattribute("succ", "图书借阅成功!");
        }
        return "redirect:/reader_books.html";
    }
 
    @requestmapping("/returnbook.html")
    public string bookreturn(httpservletrequest request, redirectattributes redirectattributes) {
        long bookid = long.parselong(request.getparameter("bookid"));
        long readerid = ((readercard) request.getsession().getattribute("readercard")).getreaderid();
        if (lendservice.returnbook(bookid, readerid)) {
            redirectattributes.addflashattribute("succ", "图书归还成功!");
        } else {
            redirectattributes.addflashattribute("error", "图书归还失败!");
        }
        return "redirect:/reader_books.html";
    }
}

以上就是java 实战图书管理系统的实现流程的详细内容,更多关于java 图书管理系统的资料请关注其它相关文章!