设计简易的图书管理系统
系统架构
表结构
图书表
借出表
归还表
购买记录表
用户表
图书类型表
Dao层
#接口
xml文件
查找用户名是否在数据库中存在
筛选符合条件的book
Pro层
public class MapperConfig {
private static SqlSessionFactory factory;
static {
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
try {
InputStream is= Resources.getResourceAsStream(“mybatis.cfg.xml”);
factory=builder.build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
//非事务的情况下,请把自动提交开启
return factory.openSession(true);
}
public static void closeSession(SqlSession session){
if (session!=null){
session.close();
}
}
}
Until层
Service层
public class Userservice {
//获取结果集并进行逻辑判断
public User getUser(){
SqlSession session= MapperConfig.getSession();
User user=session.getMapper(Userdao.class).getusername(“admin”);
if (user==null){
return null;
}else {
return user;
}
}
public List<Book> getbooks(String bookname,String uname,String type){
SqlSession session= MapperConfig.getSession();
Map map=new HashMap();
map.put("bookname",bookname);
map.put("uname",uname);
map.put("type",type);
List book= (List) session.getMapper(Bookdao.class).getbooks(map);
return book;
}
}
Servlet层
public class Bookservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Userservice userservice=new Userservice();
//获取前端页面传送过来的信息
String bookname=req.getParameter(“bookname”);
String uname=req.getParameter(“uname”);
HttpSession session=req.getSession();
String type=req.getParameter(“type”);
List books=userservice.getbooks(bookname,uname,type);
if (books!=null){
session.setAttribute(“books”,books);
//将结果传回前端页面
resp.sendRedirect(“show.jsp”);
}
}
}
public class Userservlet extends HttpServlet {
Logger logger=Logger.getLogger(Userservlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Userservice userservice=new Userservice();
HttpSession session=req.getSession();
//进行必要的业务逻辑判断
if (req.getParameter(“uname”)==null){
session.setAttribute(“msg”,“用户名不能为空”);
resp.sendRedirect(“login.jsp”);
}else if (userservice.getUser()==null){
session.setAttribute(“msg”,“用户名不存在”);
resp.sendRedirect(“login.jsp”);
}else {
session.setAttribute(“msg”,“登陆成功”);
resp.sendRedirect(“login.jsp”);
}
}
}
页面展示
登录页面
图书查询页面
图书列表展示页面