完成简单的图书馆管理项目

完成简单的图书馆管理项目
新建web工程 配置环境 引入jar包 配置Tomcat
1、在mysql中建好需要使用到的表

#图书管理类项目
#图书表:
CREATE DATABASE IF NOT EXISTS book;
USE book;
#图书表
DROP TABLE librarys;
CREATE TABLE IF NOT EXISTS books(
	book_id INT(4) ,
	book_name VARCHAR(50),
	price DOUBLE(10,2),
	store INT(10),
	des VARCHAR(50),
	book_type INT(1)
);
INSERT INTO books VALUES
(1,'《西游记》',20.5,100,'一个人喝三个妖怪一匹马的故事',1),
(2,'《水浒传》',18,80,'一群男人和几个女人的故事',1),
(3,'《科学探秘》',22,50,'科学类图书',2),
(4,'《走进科学》',19.5,30,'又是一本科学类图书',2),
(5,'《艺术鉴赏》',9.9,100,'你懂不懂艺术',3),
(6,'《鉴赏艺术》',10,100,'你不懂艺术',3);
#图书类型表
DROP TABLE book_type;
CREATE TABLE IF NOT EXISTS book_type(
	type_id INT (3),
	type_name VARCHAR(10),
	default_date INT (4),
	delay_money_per_day DOUBLE (10,2)
);
INSERT INTO book_type VALUES
(1,'小说类',10,5),
(2,'科学类',20,3),
(3,'艺术类',15,10);
#用户表
drop table ` borrow`;
create table if not exists users(
	user_id int (4),
	user_name varchar(10),
	`password` varchar(20),
	phone varchar(20),
	user_type varchar(10)
);
insert into users values 
(1,'admin','admin','0000',1),
(2,'user','user','1111',2);
#用户类型表
CREATE TABLE IF NOT EXISTS user_type(
	type_id INT (3),
	type_name VARCHAR(10)
);
INSERT INTO user_type VALUES
(1,'管理员'),
(2,'普通用户');
#借出表
create table if not exists `borrow`(
	book_id int(4),
	borrow_id int (4),
	borrow_date date,
	back_date date,
	delay_money double (10,2)
);
#购买记录表
create table if not exists buy(
	buy_id int (4),
	book_id int (4),
	user_name varchar (10),
	buy_date date,
	buy_num int (3),
	total_price double (10,2)
                                                                                                           );







2、完成登录模块
在idea中新建一个web项目,src文件夹下新建pojo文件夹,新建User类

package pojo;

public class User {
    private int user_id;
    private String user_name;
    private int grade;
    private String phone;
    private int user_type;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getUser_type() {
        return user_type;
    }

    public void setUser_type(int user_type) {
        this.user_type = user_type;
    }
}

新建文件夹dao
新建接口UserMapper 定义方法

package dao;

import pojo.User;

public interface UserMapper {
    User queryUserByName(String name);
}

采用MyBatis 所以同文件夹下新建对应的Mapper.xml文件 并实现接口中的方法




    

在编写test之前需要写一个工具类用来连接数据库
先编写配置文件mybatis-cfg.xml





    
    
    
    
        
    
    
    
        
        
    
    
    
        
            
            
            
                
                
                
                
                
            
        
    
    
        
        
        
    

和配置文件db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://填写你的数据库地址/库名
user=用户名
pwd=密码

在src下新建util文件夹 新建MapperConfig类




    

编写测试类在UserMapper接口下使用Ctrl+Shift+T快速生成Test

package dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import pojo.User;
import util.MapperConfig;

import static org.junit.Assert.*;

public class UserMapperTest {
    Logger logger=Logger.getLogger(UserMapperTest.class);
    @Test
    public void queryUserByName() {
        SqlSession session=MapperConfig.getSession();
        User user=session.getMapper(UserMapper.class).queryUserByName("admin");
        session.close();
        logger.info(user);
        
    }
}

测试通过后编写工程的Service层
在src下新建service文件夹下新建接口UserService

package service;

import pojo.User;

public interface UserService {
    User login(String userName);
}

在service文件夹下新建impl文件夹下新建接口的实现类UserServiceImpl

package service.impl;

import dao.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import service.UserService;
import util.MapperConfig;

public class UserServiceImpl implements UserService {
    @Override
    public User login(String userName) {
        SqlSession session=MapperConfig.getSession();
        User user=session.getMapper(UserMapper.class).queryUserByName(userName);
        session.close();
        return user;
    }
}

编写servlet层
在src文件夹下新建servlet文件夹下新建LoginServlet

package servlet;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String userName=req.getParameter("name");
        UserService service=new UserServiceImpl();
        User user=service.login(userName);
        if(user!=null){
            req.getSession().setAttribute("msg","登录成功");
            req.getSession().setAttribute("user",user);
            resp.sendRedirect("index.jsp");
        }else{
            req.getSession().setAttribute("msg","您不是该校的用户,无法登录");
            resp.sendRedirect("index.jsp");
        }
    }
}

同理编写Book的各层代码
之后要更改配置文件
之后编写页面
index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 1
  Date: 2020/6/15
  Time: 22:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

  
    首页
  
  
  <%request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");%>
  
  
  
    

${user.user_name}${msg}

图书编辑 展示所有图书列表

<%session.invalidate();%>

web.xml中的配置



    
        loginServlet
        servlet.LoginServlet
    
    
        loginServlet
        /login.do
    

    
        bookListServlet
        servlet.BookListServlet
    
    
        bookListServlet
        /bookList.do
    

你可能感兴趣的:(mybatis,servlet)