1、导包 下载相关的包导入,下载链接:http://download.csdn.net/detail/gufengpiaoyi/9285145
2、WebContent/WEB-INF下新建conf文件夹,新建applicationContext.xml,applicationContext-bizc.xml,applicationContext-dao.xml三个xml文件:
applicationContext.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</beans>
applicationContext-bizc.xml:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataBizc" class="com.bizc.impl.DataBizcImpl">
<property name="dataDao" ref="dataDao"></property>
</bean>
</beans>
applicationContext-dao.xml:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataDao" class="com.dao.impl.DataDaoImpl" />
</beans>
3、src文件夹下新建六个包:com.bizc方法层,com.bizc.impl方法实现层(由com.dao.impl实现),com.dao数据库方法层(根据bizc写),com.dao.impl数据库方法实现层,com.contoller控制层,com.po实例层。
controller:
@Controller
@RequestMapping(value= "/getdata",method=RequestMethod.POST)
public class BDController {
@Autowired
private IDataBizc dataBizc;
@RequestMapping(value="/getminutedata")
public @ResponseBody WrappedResult getMinuteData(@RequestParam String ItemNO){
try {
return WrappedResult.successWrapedResult(dataBizc.getMinuteData(ItemNO));
} catch (ClassNotFoundException e) {
return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
} catch (SQLException e) {
return WrappedResult.failedWrappedResult("数据库查询失败!");
}
}
@RequestMapping(value="/getrealtimedata")
public @ResponseBody WrappedResult getRealTimeDataNew(){
try {
return WrappedResult.successWrapedResult(dataBizc.getRealTimeDataNew());
} catch (ClassNotFoundException e) {
return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
} catch (SQLException e) {
return WrappedResult.failedWrappedResult("数据库查询失败!");
}
}
@RequestMapping(value="/getbatterydata")
public @ResponseBody WrappedResult getBatteryData(){
try {
return WrappedResult.successWrapedResult(dataBizc.getBatteryData());
} catch (ClassNotFoundException e) {
return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
} catch (SQLException e) {
return WrappedResult.failedWrappedResult("数据库查询失败!");
}
}
public IDataBizc getDataBizc() {
return dataBizc;
}
public void setDataBizc(IDataBizc dataBizc) {
this.dataBizc = dataBizc;
}
}
返回json标准化:
public class WrappedResult {
public WrappedResult() {
}
/**
* 是否成功
*/
private boolean successful;
/**
* 业务方法返回数据
*/
private Object resultValue;
/**
* 提示信息
*/
private String resultHint;
/**
* 成功调用返回包装结果
*
* @param data 要展示的数据{"items":[{},{}]}
*/
public static WrappedResult successWrapedResult(Object data) {
return new WrappedResult(true, data, "");
}
/**
* 失败调用返回包装结果
*
* @param exMessage
* 异常信息
*/
public static WrappedResult failedWrappedResult(String exMessage) {
return new WrappedResult(false, "", exMessage);
}
/**
*
* @param isSuccess 请求是否成功
* @param data 请求返回的数据
* @param tip 请求失败时显示异常信息
*/
public WrappedResult(boolean isSuccess, Object data, String tip) {
super();
this.successful = isSuccess;
this.resultValue = data;
this.resultHint = tip;
}
public boolean isSuccessful() {
return successful;
}
public void setSuccessful(boolean successful) {
this.successful = successful;
}
public Object getResultValue() {
return resultValue;
}
public void setResultValue(Object resultValue) {
this.resultValue = resultValue;
}
public String getResultHint() {
return resultHint;
}
public void setResultHint(String resultHint) {
this.resultHint = resultHint;
}
}
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>BdForestGuard</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/applicationContext-*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>