web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="defaultWebApp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Paas Common Service</display-name>
<!-- context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<listener>
<listener-class>com.unionpay.upaas.app.startup.StartupService</listener-class>
</listener>
<servlet>
<servlet-name>paasCommonService</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/paasCommonService-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>*.bmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>*.pdf</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
<servlet-name>paasCommonService</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<mime-mapping>
<extension>xml</extension>
<mime-type>application/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>war</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
<mime-mapping>
<extension>ear</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
<mime-mapping>
<extension>zip</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
</web-app>
Spring配置文件分为3部分
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/config.properties</value>
<value>/WEB-INF/config/jdbc/jdbc.properties</value>
</list>
</property>
</bean>
<!-- Spring服务 -->
<!-- 图片验证码 -->
<bean id="sequenceService" class="com.cup.paas.common.service.impl.SequenceServiceImpl"/>
<bean id="captchaEngine" class="com.cup.paas.common.util.captcha.CaptchaEngine"/>
<bean id="mCaptchaEngine" class="com.cup.paas.common.util.captcha.MCaptchaEngine"/>
<bean id="lCaptchaEngine" class="com.cup.paas.common.util.captcha.LCaptchaEngine"/>
<bean id="dBCaptchaStore" class="com.cup.paas.common.util.captcha.DBCaptchaStore"/>
<bean id="cacheCaptchaStore" class="com.cup.paas.common.util.captcha.CacheCaptchaStore"/>
<!-- 缓存 -->
<bean id="imageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="cacheCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="captchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="mImageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="cacheCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="mCaptchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="lImageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="cacheCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="lCaptchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<!-- db -->
<bean id="dbImageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="dBCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="captchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="dbMImageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="dBCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="mCaptchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="dbLImageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="dBCaptchaStore"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="lCaptchaEngine"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="mapsSignResponseAction" class="com.cup.paas.common.service.impl.MapsSignResponseAction"/>
<bean id="mapsAccountVerifyResponseAction" class="com.cup.paas.common.service.impl.MapsAccountVerifyResponseAction"/>
<bean id="mapsServiceHelper" class="com.cup.paas.common.util.maps.MapsServiceHelper">
<constructor-arg>
<map>
<entry key="0830161" value-ref="mapsSignResponseAction"></entry>
<entry key="021033000000" value-ref="mapsAccountVerifyResponseAction"></entry>
</map>
</constructor-arg>
</bean>
<!-- communication platform config start -->
<bean id="commuPlatform" class="com.cup.paas.common.util.communication.onetoone.platform.CommuPlatform"
factory-method="getInstance" init-method="start"/>
<context:component-scan base-package="com.cup.paas.common.service" />
<context:component-scan base-package="com.cup.paas.common.dao" />
<context:component-scan base-package="com.cup.paas.common.config" />
<context:component-scan base-package="com.cup.paas.common.util" />
<import resource="applicationContext-dao.xml" />
</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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 定义库数据源,使用Spring自己的数据源实现 -->
<bean id="paasDataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="${paas.jdbc.username}" />
<property name="password" value="${paas.jdbc.password}" />
<property name="url" value="${paas.jdbc.url}" />
<!-- <property name="initialSize" value="5"/>-->
<!--<property name="maxActive" value="30"/>-->
<!--<property name="maxIdle" value="20"/>-->
<!--<property name="minIdle" value="1"/>-->
<!--<property name="logAbandoned" value="true"/>-->
<!--<property name="removeAbandoned" value="true"/>-->
<!--<property name="removeAbandonedTimeout" value="10"/>-->
<!--<property name="maxWait" value="100"/>-->
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="paasDataSource" />
<property name="configLocation" value="classpath:sqlmap-config.xml" />
</bean>
<!-- 编程式事务配置 -->
<bean id="paasTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="paasDataSource" />
</bean>
<bean id="paasTransactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager">
<ref bean="paasTransactionManager"/>
</property>
</bean>
</beans>
paasCommonService-servlet.xml 配置整合Spring MVC 图片上传等
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<mvc:annotation-driven/>
<!-- <mvc:message-converters>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>application/x-www-form-urlencoded;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters> -->
<context:component-scan base-package="com.cup.paas.common.web.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="50000000"/>
<import resource="applicationContext.xml" />
</beans>
jdbc.properties 记录数据源信息
paas.jdbc.url=jdbc:mysql://172.16.26.48:3306/upaas
paas.jdbc.username=enap
paas.jdbc.password=123456
paas.schema.name=upaas
Mybatis配置文件的映射 加载
<?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>
<typeAliases>
<typeAlias alias="User" type="com.ssmtest.entity.User"/>
</typeAliases>
<!-- <mappers>
<mapper resource="com/ssmtest/mapper/userMapper.xml" />
</mappers> -->
</configuration>
MyBatis 的mapper.xml举例 TBL_MOBILE_ATTR.xml
<?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">
<span style="background-color: rgb(255, 255, 153);"><mapper namespace="com.ssmtest.mapper.UserMapper"> </span>
<resultMap type="com.ssmtest.entity.User" id="userResult">
<result property="userId" column="userID"/>
<result property="nickName" column="nickname" />
<result property="pwd" column="pwd" />
<result property="email" column="email" />
<result property="icq" column="icq" />
<result property="gender" column="gender" />
<result property="birth" column="birth" />
<result property="city" column="city" />
<result property="remoteIP" column="remoteIP" />
<result property="isMailPub" column="isMailPub" />
<result property="posts" column="posts" />
<result property="groupID" column="groupID" />
</resultMap>
<select id="userLogin" parameterType="User" resultType="com.ssmtest.entity.User">
SELECT
<span style="white-space:pre"> </span> u.`userID`,
<span style="white-space:pre"> </span> u.`nickname`,
<span style="white-space:pre"> </span> u.`pwd`,
<span style="white-space:pre"> </span> u.`email`,
<span style="white-space:pre"> </span> u.`icq`,
<span style="white-space:pre"> </span> u.`gender`,
<span style="white-space:pre"> </span> u.`birth`,
<span style="white-space:pre"> </span> u.`city`,
<span style="white-space:pre"> </span> u.`remoteIP`,
<span style="white-space:pre"> </span> u.`isMailPub`,
<span style="white-space:pre"> </span> u.`posts`,
<span style="white-space:pre"> </span> u.`groupID`
<span style="white-space:pre"> </span> FROM ejf_user AS u
<span style="white-space:pre"> </span> WHERE u.`userID` = #{userId}
<span style="white-space:pre"> </span> AND u.`pwd` = #{pwd}
</select>
<select id="selectAllUser" resultType="List">
select
<span style="white-space:pre"> </span> u.`userID`,
<span style="white-space:pre"> </span> u.`nickname`,
<span style="white-space:pre"> </span> u.`pwd`,
<span style="white-space:pre"> </span> u.`email`,
<span style="white-space:pre"> </span> u.`icq`,
<span style="white-space:pre"> </span> u.`gender`,
<span style="white-space:pre"> </span> u.`birth`,
<span style="white-space:pre"> </span> u.`city`,
<span style="white-space:pre"> </span> u.`remoteIP`,
<span style="white-space:pre"> </span> u.`isMailPub`,
<span style="white-space:pre"> </span> u.`posts`,
<span style="white-space:pre"> </span> u.`groupID`
<span style="white-space:pre"> </span>from user
</select>
<!--<select id="findUserById" parameterType="int" resultMap="userResult">
select *
from user where id=#{id}
</select>
<insert id="insertUser" parameterType="user">
<![CDATA[
insert into
user(username,password) values(#{username},#{password})
]]>
</insert>
<update id="updateUser" parameterType="user">
update user set
username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where
id=#{id}
</delete> -->
</mapper>
controller 代码
package com.cup.paas.common.web.controller;
import org.gocom.cloud.common.logger.api.ILogger;
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.ResponseBody;
import com.cup.paas.common.dao.object.MobileAttrDo;
import com.cup.paas.common.service.MobileAttrService;
import com.cup.paas.common.web.form.mobile.MobileAttrInfResponse;
import com.unionpay.upaas.app.api.log.UserLoggerFactory;
@Controller
<span style="background-color: rgb(255, 255, 153);">@RequestMapping(value="/mobileAttrInf")</span>
public class MobileAttrInfController {
public static ILogger logger = UserLoggerFactory.getLogger(MerchantRangeController.class);
@Autowired
private MobileAttrService mobileAttrService;
<span style="background-color: rgb(255, 255, 153);">@RequestMapping(value="/queryMobileAttrInf/{mobile}")</span>
public <span style="background-color: rgb(255, 255, 102);">@ResponseBody</span> MobileAttrInfResponse queryMobileAttrInf(<span style="background-color: rgb(255, 255, 102);">@PathVariable</span> String mobile,<span style="background-color: rgb(255, 204, 255);">@RequestBody <span style="font-family: Arial, Helvetica, sans-serif;">MobileAttrInf</span><span style="font-family: Arial, Helvetica, sans-serif;">Request request</span></span><span style="font-family: Arial, Helvetica, sans-serif;">) {</span>
MobileAttrInfResponse mobileAttrInfResponse = new MobileAttrInfResponse();
for(int i=0; i<mobile.length(); i++) {
if(!Character.isDigit(mobile.charAt(i))) {
mobileAttrInfResponse.setErrorStatus(MobileAttrInfResponse.MOBILE_ATTR_INF_FORMAT_ERROR);
return mobileAttrInfResponse;
}
}
String mobileFirstSevenDigit = mobile.substring(0, 7);
MobileAttrDo mobileAttrDo = mobileAttrService.queryMobileAttrInf(mobileFirstSevenDigit);
if(null == mobileAttrDo) {
mobileAttrInfResponse.setErrorStatus(MobileAttrInfResponse.MOBILE_ATTR_INF_RESULT_NOT_EXIST_ERROR);
} else {
mobileAttrInfResponse.setResponseCode(MobileAttrInfResponse.MOBILE_ATTR_INF_QUERY_SUCCESS_CODE);
mobileAttrInfResponse.setMobileAttrDo(mobileAttrDo);
}
return mobileAttrInfResponse;
}
}