能力提升-飞机起降系统案例

能力提升-飞机起降系统案例

能力提升-飞机起降系统案例_第1张图片
image.png
  1. 数据库设计
    1.1 根据要求,需要设计两个表(机场表和机场信息表)
    1.1.1 机场表


    能力提升-飞机起降系统案例_第2张图片
    airport表

    1.1.2机场信息表


    能力提升-飞机起降系统案例_第3张图片
    airplane表
  2. 搭建开发环境
    2.1 导jar包


    能力提升-飞机起降系统案例_第4张图片
    jar包

    2.2 全局配置文件




    
        
    
    
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

  1. 实体类
public class Airport {
    private int id;
    private String portName;
    private String cityName;
    /**setter and getter**/
}
public class Airplane {
    private int id;
    private String airNo;
    private int time;
    private double price;
    private Airport takePort;
    private Airport landPort;
    /**setter and getter**/
}
  1. 工具类
public class MapperUtil {
    //factory实例化过程是一个非常耗费性能的过程
    //保证有且只有一个factory
    private static SqlSessionFactory factory;
    private static ThreadLocal tl = new ThreadLocal<>();
    static{
        try {
            InputStream is = Resources.getResourceAsStream("mybatis.xml");
            factory = new SqlSessionFactoryBuilder().build(is); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    /**
     * 获取SqlSession的方法
     */
    public static SqlSession getSession(){
        SqlSession session = tl.get();
        if(session==null){
            tl.set(factory.openSession());
        }
        return tl.get();
    }
    /**
     * 关闭SqlSession的方法
     */
    public static void closeSession(){
        SqlSession session = tl.get();
        if(session!=null){
            session.close();
        }
        tl.set(null);
    }
}
  1. mapper
public interface AirportMapper {
    /**
     * 查询所有起飞机场
     * @return
     */
    public List selTakePort();

    public List selLandPort();
}
------



    
    

public interface AirplaneMapper {
    List selByTakeIdLandId(@Param("takeid") int takeId,@Param("landid") int landId);
}
-----



    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
       

  1. service层
public interface AirTakeService {
    public List showTake();
}
-----
public class AirTakeServiceImpl implements AirTakeService{
    @Override
    public List showTake() {
        SqlSession session = MapperUtil.getSession();
        AirportMapper airportMapper = session.getMapper(AirportMapper.class);
        return airportMapper.selTakePort();
    }
}
public interface AirLandService {
    public List showLand();
}
-----
public class AirLandServiceImpl implements AirLandService{
    @Override
    public List showLand() {
        SqlSession session = MapperUtil.getSession();
        AirportMapper airportMapper = session.getMapper(AirportMapper.class);
        return airportMapper.selLandPort();
    }
}
public interface AirplaneService {
    List showAirplane(int takeId,int landId);
}
-----
public class AirplaneServiceImpl implements AirplaneService{
    @Override
    public List showAirplane(int takeId, int landId) {
        
         SqlSession session = MapperUtil.getSession();
         AirplaneMapper airplaneMapper = session.getMapper(AirplaneMapper.class);
         return airplaneMapper.selByTakeIdLandId(takeId, landId);
    }   
}
  1. servlet层
@WebServlet("/showtake")
public class ShowTakeServlet extends HttpServlet{
    AirTakeService ats = new AirTakeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List showTake = ats.showTake();
        req.setAttribute("showtake", showTake);
        req.getRequestDispatcher("/showland").forward(req, resp);
        return;
    }
}
-----
@WebServlet("/showland")
public class ShowLandServlet extends HttpServlet {
    AirLandService als = new AirLandServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List showLand = als.showLand();
        req.setAttribute("showland", showLand);
        req.getRequestDispatcher("/showAirplane").forward(req, resp);
        return;
    }
}
-----
@WebServlet("/showAirplane")
public class ShowAirplaneServlet extends HttpServlet{
    private AirplaneService as = new AirplaneServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int takeId = 0;
        String takeIdStr = req.getParameter("takeid");
        if(takeIdStr!=null&&!takeIdStr.equals("")){
            takeId = Integer.parseInt(takeIdStr);
        }
        int landId = 0;
        String landIdStr = req.getParameter("landid");
        if(landIdStr!=null&&!landIdStr.equals("")){
            landId = Integer.parseInt(landIdStr);
        }
        req.setAttribute("airplane", as.showAirplane(takeId, landId));
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
        return;
    }
}
  1. filter
@WebFilter("/*")
public class OpenSessionInView implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub  
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        SqlSession session = MapperUtil.getSession();
        try{
            chain.doFilter(request, response);
            session.commit();
        }catch(Exception e){
            session.rollback();
            e.printStackTrace();
        }finally{
            MapperUtil.closeSession();
        }   
    }
    @Override
    public void destroy() {
        // TODO Auto-generated method stub  
    }
}
  1. index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>




Insert title here


起飞机场:      降落机场:
飞机编号 起飞机场 起飞城市 降落机场 降落城市 航行时间 价格(元)
${plane.airNo} ${plane.takePort.portName} ${plane.takePort.cityName} ${plane.landPort.portName} ${plane.landPort.cityName} 小时 ${plane.time%60}分钟 ${plane.price}

你可能感兴趣的:(能力提升-飞机起降系统案例)