知识点:工厂模式,MVC模式,反射,xml文件解析存取,I/O流txt文件存取。
flights.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 航班信息 -->
<flights>
<fight flightNo="AB1234">
<departureCity>大连</departureCity>
<departureTime>2019-05-06 19:33:00</departureTime>
<arrivalCity>上海</arrivalCity>
<arrivalTime>2019-05-06 19:33:00</arrivalTime>
</fight>
<fight flightNo="CA1234">
<departureCity>北京</departureCity>
<departureTime>2019-08-02 12:28:00</departureTime>
<arrivalCity>上海</arrivalCity>
<arrivalTime>2019-08-02 12:28:00</arrivalTime>
</fight>
<fight flightNo="NA1234">
<departureCity>北京</departureCity>
<departureTime>2019-08-02 15:28:00</departureTime>
<arrivalCity>上海</arrivalCity>
<arrivalTime>2019-08-02 15:28:00</arrivalTime>
</fight>
</flights>
flight.txt
CA1234,北京,2019-08-02 10:30:00,上海,2019-08-02 12:28:00
NA1234,北京,2019-08-02 13:30:00,上海,2019-08-02 15:28:00
CZ1234,北京,2019-08-02 17:03:00,上海,2019-08-02 19:28:00
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans >
<bean id="dao" class="cn.kgc.tangcco.chengxiangjian.dao.impl.FlightDaoImp"></bean>
<bean id="ser" class="cn.kgc.tangcco.chengxiangjian.service.impl.FlightServiceImp"></bean>
</beans>
test
package cn.kgc.tangcco.chengxiangjian.test;
import cn.kgc.tangcco.chengxiangjian.action.FlightAction;
public class test {
//创建main方法
public static void main(String[] args) {
//实例化FlightAction
FlightAction flightAction = new FlightAction();
//调用menu()方法
flightAction.menu();
}
}
Constant
package cn.kgc.tangcco.chengxiangjian.common;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Constant {
//创建map集合,用于解析applicationContext.xml后存放数据
private static Map<String, String> map = new ConcurrentHashMap<String, String>();
//设定常量值
public static final String DAO = "dao";
public static final String SER = "ser";
//解析applicationContext.xml文件,数据存入map集合
static {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File("src/files/applicationContext.xml"));
Element root = document.getRootElement();
List<Element> list = root.elements();
for (Element element : list) {
String key = element.attributeValue("id");
String value = element.attributeValue("class");
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//根据键,返回对应的值
public static String getUrl(String key) {
return map.get(key);
}
}
ObjectFactory
package cn.kgc.tangcco.chengxiangjian.common;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
//工厂模式
public class ObjectFactory {
//创建map集合
private static Map<String, Object> map = new ConcurrentHashMap<String, Object>();
//反射模式创建对象
public static <E> E getInstence(String key) {
E object