近段要實現一個上傳excel文件到服務器
excel文件有兩種格式
開始的時候
代碼是這樣的
action中:
CityDayService cds = new CityDayServiceImpl();
ImpExcelData impExcelData = new ImpExcelData(filePath
try {
List list = impExcelData
List<CityDay> sameList = cds
//boolean isOk = cds
if (sameList
request
request
return mapping
} else {
cds
}
System
} catch (Exception e) {
// TODO Auto
e
}
另一種格式的excel文件對應的action是這樣的
/* 導入表 */
ZhbTCityDayForecastService cds = new ZhbTCityDayForecastServiceImpl();/////////////////
ImpExcelData impExcelData = new ImpExcelData(filePath
try {
List list = impExcelData
List<ZhbTCityDayForecast> sameList = cds
if (sameList
request
request
return mapping
} else {
cds
}
System
} catch (Exception e) {
// TODO Auto
e
}
業務層:
public interface CityDayService{
public List findByCityAndDate(String city
public void deleteSame(List excelDataList);
public void add(List excelDatalist);
public List sameList(List excelDatalist);
}
和
public interface ZhbTCityDayForecastService{
public List findByCityAndDate(String city
public void deleteSame(List excelDataList);
public void add(List excelDatalist);
public List sameList(List excelDatalist);
}
業務層實現類:
public class CityDayServiceImpl implements CityDayService {
public List findByCityAndDate(String city
CityDayDAO cdd = new CityDayDAOImpl();
return cdd
}
public void add(List excelDatalist) {
Session session = HibernateSessionFactory
Transaction tran = session
List<CityDay> newList = new ArrayList();
for (int i =
HashMap map = (HashMap) excelDatalist
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
CityDayDAO cdd = new CityDayDAOImpl();
CityDay cd = new CityDay();
cd
cd
cd
cd
cd
cd
cdd
} catch (ParseException e) {
tran
e
}
}
mit();
}
public void deleteSame(List excelDataList) {
CityDayDAO cdd = new CityDayDAOImpl();
boolean result = true;
List<CityDay> list = new ArrayList<CityDay>();
for (int i =
HashMap map = (HashMap) excelDataList
String city = map
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
List onelist = cdd
for(int j=
list
}
} catch (ParseException e) {
// TODO Auto
e
}
}
for(int j=
CityDay cd = (CityDay)list
System
}
cdd
}
public List<CityDay> sameList(List excelDatalist) {
CityDayDAO cdd = new CityDayDAOImpl();
boolean result = true;
List<CityDay> list = new ArrayList<CityDay>();
for (int i =
HashMap map = (HashMap) excelDatalist
String city = map
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
List onelist = cdd
for(int j=
list
}
} catch (ParseException e) {
// TODO Auto
e
}
}
for(int j=
CityDay cd = (CityDay)list
System
}
return list;
}
}
和
public class ZhbTCityDayForecastServiceImpl implements ZhbTCityDayForecastService {
public List findByCityAndDate(String city
ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
return cdd
}
public void add(List excelDatalist) {
Session session = HibernateSessionFactory
Transaction tran = session
List<CityDay> newList = new ArrayList();
for (int i =
HashMap map = (HashMap) excelDatalist
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
ZhbTCityDayForecast cd = new ZhbTCityDayForecast();
cd
cd
cd
cd
cd
cd
cd
cdd
} catch (ParseException e) {
tran
e
}
}
mit();
}
public void deleteSame(List excelDataList) {
ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
boolean result = true;
List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();
for (int i =
HashMap map = (HashMap) excelDataList
String city = map
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
List<ZhbTCityDayForecast> onelist = cdd
for(int j=
list
}
} catch (ParseException e) {
// TODO Auto
e
}
}
cdd
}
public List<ZhbTCityDayForecast> sameList(List excelDatalist) {
// TODO Auto
ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();
for (int i =
HashMap map = (HashMap) excelDatalist
String city = map
String strDate = map
Date date;
try {
date = new SimpleDateFormat(
List onelist = cdd
for(int j=
list
}
} catch (ParseException e) {
// TODO Auto
e
}
}
return list;
}
}
這樣做完後
如果以後需求增加
經過思索
public void add(List excelDatalist);
public List sameList(List excelDatalist);
public void deleteSame(List excelDataList);
三個方法是不同的
那麼在action中可以根據參數生成不同的service
首先把這三個方法提到一個接口裡來
public interface CommonUploadService {
public void add(List excelDatalist);
public List sameList(List excelDatalist);
public void deleteSame(List excelDataList);
}
原為的service就變成這樣
public interface CityDayService extends CommonUploadService{
public List findByCityAndDate(String city
}
public interface ZhbTCityDayForecastService extends CommonUploadService{
public List findByCityAndDate(String city
}
實現類不用變
工廠方法
public class UploadServiceFactory {
private static CommonUploadService cus;
private static final String cityDayService =
private static final String foreCastService =
private UploadServiceFactory() {
}
public static CommonUploadService getInstance(String type) {
try {
if (
Class cls = Class
cus = (CommonUploadService) cls
}
if (
Class cls = Class
cus = (CommonUploadService) cls
}
} catch (Exception e) {
e
}
return cus;
}
}
改造後的action
/* 導入表 */
// CityDayService cds = new CityDayServiceImpl();//這裡有不同 可以用工廠方法
String type = request
CommonUploadService cds = UploadServiceFactory
ImpExcelData impExcelData = new ImpExcelData(filePath
try {
List list = impExcelData
List<CityDay> sameList = cds
//boolean isOk = cds
if (sameList
request
request
return mapping
} else {
cds
}
System
} catch (Exception e) {
// TODO Auto
e
}
這樣一個action就可以搞定了
如果以為要增加新格式的excel文件
總結:
以後遇到功能類似的需求
運用面向對象的多態的性能
設計模式關暫在於實踐
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27596.html