Skip to content

Instantly share code, notes, and snippets.

@kuoshenghsu
Last active April 6, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuoshenghsu/3a89c871c05de27af49691001dee06e9 to your computer and use it in GitHub Desktop.
Save kuoshenghsu/3a89c871c05de27af49691001dee06e9 to your computer and use it in GitHub Desktop.
Spring MVC SSM 基礎配置
datasource.driverClass=org.postgresql.Driver
datasource.jdbcUrl=jdbc:postgresql://127.0.0.1:5888/test_schema
datasource.commander=test
datasource.commanderPwd=test
datasource.querier=testquerier
datasource.querierPwd=testquerier
<?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">
<mapper namespace="test.mapper.command.UserCommandMapper">
<insert id="save" parameterType="User">
<![CDATA[
INSERT INTO public.user(
sys_id,
action_time,
remote_ip,
action_item,
actor_account,
action_result,
error_code,
event_level)
VALUES (
#{sysId, typeHandler=UUIDHandler, javaType=UUID, jdbcType=OTHER},
#{actionTime}, #{remoteIp}, #{actionItem}, #{actorAccount}, #{actionResult},
#{errorCode}, #{eventLevel});
]]>
</insert>
<delete id="remove" parameterType="user">
<![CDATA[
DELETE FROM public.user
WHERE
sys_id = #{sysId,
typeHandler=UUIDHandler,
javaType=UUID,
jdbcType=OTHER};
]]>
</delete>
</mapper>
<?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">
<mapper namespace="test.mapper.query.UserQueryMapper">
<resultMap id="UseerControlMap" type="User">
<id column="sys_id" property="sysId" typeHandler="UUIDHandler"/>
<result column="action_time" property="actionTime"/>
<result column="remote_ip" property="remoteIp"/>
<result column="action_item" property="actionItem"/>
<result column="actor_account" property="actorAccount"/>
<result column="log_result" property="actionResult"/>
<result column="error_code" property="errorCode"/>
<result column="event_level" property="eventLevel"/>
</resultMap>
<select id="findById" parameterType="String" resultMap="UseerControlMap">
...
</select>
</mapper>
# C3P0
c3p0.acquireIncrement=1
c3p0.initialPoolSize=5
c3p0.minPoolSize=1
c3p0.maxPoolSize=15
c3p0.maxIdleTime=30
c3p0.idleConnectionTestPeriod=30
c3p0.maxStatements=0
c3p0.numHelperThreads=100
c3p0.checkoutTimeout=0
c3p0.validate=true
<?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>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<typeAliases>
<typeAlias type="java.util.UUID" alias="UUID"/>
<typeAlias type="org.joda.time.DateTime" alias="DateTime"/>
<typeAlias type="test.util.UUIDHandler" alias="UUIDHandler"/>
<typeAlias type="test.util.DateTimeHandler" alias="DateTimeHandler"/>
<package name="test.domain.entity"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="test.util.UUIDHandler" javaType="java.util.UUID"/>
<typeHandler handler="test.util.DateTimeHandler" javaType="org.joda.time.DateTime"/>
</typeHandlers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:persistence.properties</value>
<value>classpath:datasource.properties</value>
</list>
</property>
</bean>
<!-- C3P0 Config -->
<bean id="abstractC3P0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" abstract="true">
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
</bean>
<!-- dataSource Config -->
<bean id="commandDataSource" parent="abstractC3P0DataSource">
<property name="driverClass" value="${datasource.driverClass}"/>
<property name="jdbcUrl" value="${datasource.jdbcUrl}"/>
<property name="user" value="${datasource.commander}"/>
<property name="password" value="${datasource.commanderPwd}"/>
</bean>
<bean id="queryDataSource" parent="abstractC3P0DataSource">
<property name="driverClass" value="${datasource.driverClass}"/>
<property name="jdbcUrl" value="${datasource.jdbcUrl}"/>
<property name="user" value="${datasource.querier}"/>
<property name="password" value="${datasource.querierPwd}"/>
</bean>
</beans>
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- DataSource Config -->
<import resource="classpath:spring-datasource.xml"/>
<tx:annotation-driven transaction-manager="commandTxManager"/>
<tx:annotation-driven transaction-manager="queryTxManager"/>
<tx:annotation-driven transaction-manager="doxCommandTxManager"/>
<tx:annotation-driven transaction-manager="doxQueryTxManager"/>
<!-- MyBatis base config -->
<bean id="abstractSqlSessionFactory" abstract="true" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-mapper.xml"/>
</bean>
<!-- Command DataSource Config -->
<bean id="commandTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="commandDataSource"/>
</bean>
<bean id="commandSqlSessionFactory" parent="abstractSqlSessionFactory">
<property name="dataSource" ref="commandDataSource"/>
<property name="mapperLocations" value="classpath*:test/mapper/command/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="commandSqlSessionFactory"/>
<property name="basePackage" value="test.mapper.command"/>
</bean>
<!-- Query DataSource Config -->
<bean id="queryTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="queryDataSource"/>
</bean>
<bean id="querySqlSessionFactory" parent="abstractSqlSessionFactory">
<property name="dataSource" ref="queryDataSource"/>
<property name="mapperLocations" value="classpath*:test/mapper/query/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="querySqlSessionFactory"/>
<property name="basePackage" value="test.mapper.query" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="gov.archives.**.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971520"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- JSON Support -->
<bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
<mvc:default-servlet-handler/>
<mvc:resources mapping="/resource/**" location="/resource/"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
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">
<context:annotation-config/>
<import resource="classpath:spring-quartz.xml"/>
<context:component-scan base-package="gov.archives.**">
<context:include-filter type="regex" expression="(service|facade|accessor|security|job|event)\..*"/>
</context:component-scan>
</beans>
=== 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_3_0.xsd"
version="3.0">
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-service.xml,
classpath:spring-mapper.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1000</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>testWeb</param-value>
</context-param>
<session-config>
<session-timeout>10</session-timeout>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/resources/error/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/resources/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/resources/error/500.jsp</location>
</error-page>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment