Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deal2/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

18 changes: 18 additions & 0 deletions deal2/WEB-INF/classes/applicationContext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 包扫描器,扫描所以@Service类 -->
<context:component-scan base-package="com.wendell.dealServiceImpl,com.wendell.dealDaoImpl"/>
</beans>
58 changes: 58 additions & 0 deletions deal2/WEB-INF/classes/business.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>

<deals>
<deal id="41020238-2367-4c45-b0a5-9e35615a9231">
<date>2015-11-24</date>
<state>0</state>
<type>1</type>
<money>86.5</money>
<currency>1</currency>
<userId>001</userId>
<userName>zhangsan</userName>
</deal>
<deal id="3eca016c-b8c6-4ba3-9527-eaf27d9d0d58">
<date>2015-11-24</date>
<state>0</state>
<type>1</type>
<money>86.5</money>
<currency>1</currency>
<userId>002</userId>
<userName>success1</userName>
</deal>
<deal id="67341d7d-6908-44e5-9384-13e5e66f0a9a">
<date>2015-11-24</date>
<state>0</state>
<type>1</type>
<money>86.5</money>
<currency>2</currency>
<userId>003</userId>
<userName>success2</userName>
</deal>
<deal id="109999c8-05d0-453d-93a6-40539400091d">
<date>2015-11-24</date>
<state>0</state>
<type>1</type>
<money>86.5</money>
<currency>1</currency>
<userId>004</userId>
<userName>success3</userName>
</deal>
<deal id="2c7326a8-08eb-4638-b0e0-80e61c9edae1">
<date>2015-11-24</date>
<state>1</state>
<type>1</type>
<money>-86.5</money>
<currency>1</currency>
<userId>006</userId>
<userName>fail1</userName>
</deal>
<deal id="acbbcaa0-dadb-4e5e-8f0d-9b107e9044a8">
<date>2015-11-24</date>
<state>1</state>
<type>1</type>
<money>0</money>
<currency>1</currency>
<userId>007</userId>
<userName>fail2</userName>
</deal>
</deals>
Binary file not shown.
73 changes: 73 additions & 0 deletions deal2/WEB-INF/classes/com/wendell/controller/DealController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.wendell.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wendell.dealService.DealService;
import com.wendell.domain.Deal;

/**
* 交易controller层
* @author Wendell
*
*/
@Controller
@RequestMapping("/rest")
public class DealController {

//注入DealService对象
@Autowired
private DealService dealService;

//使用springmvc向外提供Rest风格服务
@RequestMapping(value="/addDeal",method=RequestMethod.POST)
@ResponseBody
public ModelAndView makeDeal(Deal deal){

try {
//设置交易记录ID
deal.setId(UUID.randomUUID().toString());
//设置交易记录时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateFormat = simpleDateFormat.format(new Date());
deal.setDate(dateFormat);
//设置交易状态
if(Double.valueOf(deal.getMoney())<=0){
//如果交易金额为负数,则交易失败
deal.setState(String.valueOf(1));
}else{
//否则交易成功
deal.setState(String.valueOf(0));
}
//保存交易记录
dealService.addDeal(deal);
ModelAndView view = new ModelAndView();
view.setViewName("success");
return view;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("添加交易记录失败!");
}
}

//对外提供查询交易记录的接口
@RequestMapping(value="/getDeals/{state}",method=RequestMethod.GET)
@ResponseBody
public List<Deal> getDealList(@PathVariable String state){
List<Deal> dealList = dealService.getDealList(state);
return dealList;

}


}
Binary file not shown.
12 changes: 12 additions & 0 deletions deal2/WEB-INF/classes/com/wendell/dealDao/BusinessDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wendell.dealDao;

import java.util.List;

import com.wendell.domain.Deal;

public interface BusinessDao {
//保存交易记录到business.xml中
void saveDeal(Deal deal);
//根据交易状态查询交易信息
List<Deal> getDealList(String state);
}
Binary file not shown.
154 changes: 154 additions & 0 deletions deal2/WEB-INF/classes/com/wendell/dealDaoImpl/BusinessDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.wendell.dealDaoImpl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.stereotype.Repository;

import com.wendell.dealDao.BusinessDao;
import com.wendell.domain.Deal;
/**
* 对business.xml操作的DAO实现类
* @author Wendell
*
*/
@Repository
public class BusinessDaoImpl implements BusinessDao{

//保存交易信息到business.xml中
public void saveDeal(Deal deal) {
//获得xml数据源的文档对象
Document dc = getDocument();
//获得根元素deals
Element deals = dc.getRootElement();
//在根元素下增加deal子元素并添加id属性
Element dealEle = deals.addElement("deal").addAttribute("id", deal.getId());
//为deal增加其他子元素
dealEle.addElement("date").addText(deal.getDate());
dealEle.addElement("state").addText(deal.getState());
dealEle.addElement("type").addText(deal.getType());
dealEle.addElement("money").addText(deal.getMoney());
dealEle.addElement("currency").addText(deal.getCurrency());
dealEle.addElement("userId").addText(deal.getUserId());
dealEle.addElement("userName").addText(deal.getUserName());
//将修改后的内容写回到xml文件中
saveDocument(dc);


}
//通过交易状态查询交易记录
public List<Deal> getDealList(String state) {
//得到document对象
Document document = getDocument();
//定义xpath
String xpath = "/deals/deal";
//根据xpath找到所有的元素
List<Element> elements = document.selectNodes(xpath);
//判断集合是否为空
if(elements==null){
return Collections.EMPTY_LIST;
}
List<Deal> dealList = new ArrayList<Deal>();
for(Element element:elements){
Deal deal = ele2deal(element);
if(deal.getState().equals(state)){
dealList.add(deal);
}
}
return dealList;
}
//---------以下是抽取通用方法-------------------------
//获得文档对象
private Document getDocument(){
try {
//创建解析器
SAXReader reader = new SAXReader();
//获得xml的资源流,交给解析器解析,得到Document对象
String path = getXmlPath();
InputStream fis = new FileInputStream(path);
Document document = reader.read(fis);
//关闭资源流
fis.close();
//返回Document对象
return document;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("读取xml文件失败!");

}

}
//获得xml文件的路径
private String getXmlPath(){
URL url = BusinessDaoImpl.class.getResource("/business.xml");
String path = url.getPath();
return path;
}
//将document写回到xml文件中
private void saveDocument(Document dc){
try {
//准备输出到xml文件中的输出流
String path = getXmlPath();
FileOutputStream os = new FileOutputStream(path);
//创建outputFormat
OutputFormat format = OutputFormat.createPrettyPrint();
//创建XmlWriter
XMLWriter writer = new XMLWriter(os, format);
//写入
writer.write(dc);
//关闭资源
os.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件写入失败!");
}
}
//将元素封装到Deal中
public Deal ele2deal(Element dealEle){
Deal deal = new Deal();
//封装交易记录ID
String id = dealEle.attributeValue("id");
deal.setId(id);
//封装用户ID
String userId = dealEle.elementText("userId");
deal.setUserId(userId);
//封装用户名
String userName = dealEle.elementText("userName");
deal.setUserName(userName);
//封装交易金额
String money = dealEle.elementText("money");
deal.setMoney(money);
//封装交易类型
String type = dealEle.elementText("type");
deal.setType(type);
//封装交易时间
String date = dealEle.elementText("date");
deal.setDate(date);
//封装交易状态
String state = dealEle.elementText("state");
deal.setState(state);
//封装交易币种
String currency = dealEle.elementText("currency");
deal.setCurrency(currency);
return deal;
}




}
Binary file not shown.
12 changes: 12 additions & 0 deletions deal2/WEB-INF/classes/com/wendell/dealService/DealService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wendell.dealService;

import java.util.List;

import com.wendell.domain.Deal;

public interface DealService {
//增加交易记录
void addDeal(Deal deal);
//获得交易记录
List<Deal> getDealList(String state);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.wendell.dealServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wendell.dealDao.BusinessDao;
import com.wendell.dealService.DealService;
import com.wendell.domain.Deal;
/**
* 交易Service层
* @author Wendell
*
*/
@Service
public class DealServiceImpl implements DealService {
//注入DealDao实现类
@Autowired
private BusinessDao businessDao;
//增加交易记录
public void addDeal(Deal deal) {
businessDao.saveDeal(deal);
}
//获得交易记录
public List<Deal> getDealList(String state) {
List<Deal> dealList = businessDao.getDealList(state);
return dealList;
}

}
Binary file not shown.
Loading