美文网首页
能力提升-飞机起降系统案例

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

作者: 默辽 | 来源:发表于2019-06-21 09:26 被阅读0次

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

image.png
  1. 数据库设计
    1.1 根据要求,需要设计两个表(机场表和机场信息表)
    1.1.1 机场表


    airport表

    1.1.2机场信息表


    airplane表
  2. 搭建开发环境
    2.1 导jar包


    jar包

    2.2 全局配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
    <typeAliases>
        <package name="cn.uaichn.pojo"/>
    </typeAliases>
    <environments default="default">
        <environment id="default">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/java505"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="cn.uaichn.mapper"/>
    </mappers>
</configuration>
  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<SqlSession> 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<Airport> selTakePort();

    public List<Airport> selLandPort();
}
------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.uaichn.mapper.AirportMapper">
    <select id="selTakePort" resultType="airport">
        select * from airport where id in(select takeid from airplane);
    </select>
    <select id="selLandPort" resultType="airport">
        select * from airport where id in(select landid from airplane);
    </select>
</mapper>
public interface AirplaneMapper {
    List<Airplane> selByTakeIdLandId(@Param("takeid") int takeId,@Param("landid") int landId);
}
-----
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.uaichn.mapper.AirplaneMapper">
    <resultMap type="airplane" id="selmap">
        <id column="id" property="id"/>
        <result column="airno" property="airNo"/>
        <result column="time" property="time"/>
        <result column="price" property="price"/>
        <association property="takePort" javaType="airport">
        <id column="takeid" property="id"/>
        <result column="takeportname" property="portName"/>
        <result column="takecityname" property="cityName"/>
        </association>
        <association property="landPort" javaType="airport">
        <id column="landid" property="id"/>
        <result column="landportname" property="portName"/>
        <result column="landcityname" property="cityName"/>
        </association>
    </resultMap>
    <select id="selByTakeIdLandId" resultMap="selmap">
        select a.*,p.id takeid,p.portname takeportname,p.cityname takecityname,
        t.id landid,t.portname landportname,t.cityname landcityname
        from airplane a 
        left join airport p on a.takeid = p.id 
        left join airport t on a.landid = t.id
        <where>
            <if test="takeid>0">
                and takeid=#{takeid}
            </if>
            <if test="landid>0">
                and landid=#{landid}
            </if>
        </where>
    </select>   
</mapper>
  1. service层
public interface AirTakeService {
    public List<Airport> showTake();
}
-----
public class AirTakeServiceImpl implements AirTakeService{
    @Override
    public List<Airport> showTake() {
        SqlSession session = MapperUtil.getSession();
        AirportMapper airportMapper = session.getMapper(AirportMapper.class);
        return airportMapper.selTakePort();
    }
}
public interface AirLandService {
    public List<Airport> showLand();
}
-----
public class AirLandServiceImpl implements AirLandService{
    @Override
    public List<Airport> showLand() {
        SqlSession session = MapperUtil.getSession();
        AirportMapper airportMapper = session.getMapper(AirportMapper.class);
        return airportMapper.selLandPort();
    }
}
public interface AirplaneService {
    List<Airplane> showAirplane(int takeId,int landId);
}
-----
public class AirplaneServiceImpl implements AirplaneService{
    @Override
    public List<Airplane> 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<Airport> 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<Airport> 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="showtake" method="post">
起飞机场:
<select name ="takeid">
    <option value="0">请选择</option>
    <c:forEach items = "${showtake}" var="take">
    <option value="${take.id}">${take.portName}</option>
    </c:forEach>

</select>
&nbsp;&nbsp;&nbsp;&nbsp;
降落机场:
<select name = "landid">
    <option value="0">请选择</option>
    <c:forEach items = "${showland}" var="land">
    <option value="${land.id}">${land.portName}</option>
    </c:forEach>
</select>
<input type="submit" value="查询">
</form>
<table border="1">
    <tr>
        <td>飞机编号</td>
        <td>起飞机场</td>
        <td>起飞城市</td>
        <td>降落机场</td>
        <td>降落城市</td>
        <td>航行时间</td>
        <td>价格(元)</td>
    </tr>
    <c:forEach items="${airplane}" var="plane">
    <tr>
        <td>${plane.airNo}</td>
        <td>${plane.takePort.portName}</td>
        <td>${plane.takePort.cityName}</td>
        <td>${plane.landPort.portName}</td>
        <td>${plane.landPort.cityName}</td>
        <td>
            <c:if test="${plane.time/60>0}">
                <fmt:formatNumber value="${plane.time/60}" pattern="0"></fmt:formatNumber>小时
            </c:if>
            <c:if test="${plane.time%60>0}">
                ${plane.time%60}分钟
            </c:if>
        </td>
        <td>${plane.price}</td>
    </tr>
    </c:forEach>
</table>
</body>
</html>

相关文章

网友评论

      本文标题:能力提升-飞机起降系统案例

      本文链接:https://www.haomeiwen.com/subject/dqeeqctx.html