Spring + Spring MVC + Ibatis + Velocity 框架搭建

总结下如何用这四个常用框架搭建一个Java Web工程,方便以后更快速的开发工程。首先得用maven搭建一个多模块的web工程,这里不再赘述,请参考以前的总结:用Maven命令行创建多模块Web项目

首先导入这些框架所需的maven依赖:

<spring.version>3.2.2.RELEASE</spring.version>
<v.velocity>1.6.3</v.velocity>
<v.velocity.tool>1.2</v.velocity.tool>
<jdbc.driver.version>5.1.15</jdbc.driver.version>

<!-- ibatis begin -->
<dependency>
    <groupId>org.apache.ibatis</groupId>
    <artifactId>ibatis-sqlmap</artifactId>
    <version>2.3.4.726</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${jdbc.driver.version}</version>
</dependency>
<!-- ibatis end -->

<!-- spring begin -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <artifactId>spring-context</artifactId>
    <groupId>org.springframework</groupId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<!-- spring end -->

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
</dependency>

<!-- velocity begin -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>${v.velocity}</version>
</dependency>
<dependency>
    <groupId>velocity-tools</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>${v.velocity.tool}</version>
</dependency>
<!-- velocity end -->

    web.xml里配置spring和spring mvc 入口


 <servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>appServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
    DispatcherServlet是spring mvc的所有请求入口的servlet。contextConfigLocation参数配置spring的总配置文件位置。如果是spring集成struts的话,还需要单独配置spring的监听器,但是如果是集成spring mvc,则不需要下面这段配置了:


<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring-context.xml</param-value>
</context-param>

    spring总配置文件


<?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:aop="http://www.springframework.org/schema/aop"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      default-autowire="byName">

    <context:component-scan base-package="com.jd.im.**"/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:im-king.properties</value>
                <value>classpath:important.properties</value>
                <value>classpath:jss_config.properties</value>
            </list>
        </property>
    </bean>

    <import resource="spring/spring-config-mvc.xml"></import>
    <import resource="spring/spring-config-datasource.xml"/>
</beans>
    总配置文件里集成了spring mvc配置文件和ibatis数据源的配置文件。

    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: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-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">

 <context:component-scan base-package="com.**.dd.mall.web.controller" />
 <mvc:annotation-driven />
 
 <bean id="dateObjectMapper" class="com.**.dd.mall.web.util.DateObjectMapper"></bean>
 <!-- Json返回 乱码处理 -->
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  <property name="messageConverters">
   <list>
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
     <property name="objectMapper" ref="dateObjectMapper"></property>
    </bean>
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
     <property name="supportedMediaTypes">
      <list>
       <value>text/plain;charset=UTF-8</value>
      </list>
     </property>
    </bean>
    <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
    <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
    <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
   </list>
  </property>
 </bean>

 <mvc:resources mapping="/dist/**" location="/dist/" cache-period="3600" />
 <mvc:resources mapping="/js/**" location="/js/" cache-period="3600" />
 <mvc:resources mapping="/css/**" location="/css/" cache-period="3600" />
 <!--<mvc:resources mapping="/fonts/**" location="/fonts/" cache-period="3600" /> -->
 <mvc:resources mapping="/config.json" location="/" cache-period="3600" />
 <mvc:resources mapping="/image/**" location="/image/" cache-period="3600" />

 <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
  <property name="cache" value="true"></property>
  <property name="suffix" value=".vm"></property>
  <property name="prefix" value=""></property>
  <property name="contentType" value="text/html;charset=utf-8"></property>
  <property name="exposeRequestAttributes" value="true"></property>
  <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"></property>
 </bean>

 <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  <property name="resourceLoaderPath" value="/WEB-INF/vm/"></property>
  <property name="configLocation" value="classpath:velocity.properties"></property>
 </bean>

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="10485760" />
  <property name="defaultEncoding" value="utf-8"></property>
 </bean>

 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <!-- 设置为true以忽略对Accept Header的支持 -->
  <property name="order" value="1" />
  <property name="contentNegotiationManager">
   <bean class="org.springframework.web.accept.ContentNegotiationManager">
    <constructor-arg>
     <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
      <constructor-arg>
       <map>
        <entry key="json" value="application/json" />
        <entry key="xml" value="application/xml" />
       </map>
      </constructor-arg>
     </bean>
    </constructor-arg>
   </bean>
  </property>
  <property name="ignoreAcceptHeader" value="true" />
  <!-- 在没有扩展名时即: "/user/getUser" 时的默认展现形式 -->
  <property name="defaultContentType" value="text/html" />

  <!-- 用于开启 /user/getUser?format=json 的支持 -->
  <property name="favorParameter" value="true" />
  <property name="defaultViews">
   <list>
    <!-- for application/json -->
    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    </bean>
   </list>
  </property>
 </bean>
 
</beans>
    viewResolver和velocityConfig节点指定了spring mv返回页面用velocity。

    Ibatis配置
    带读写分离的数据源配置
<?xml version="1.0" encoding="GBK"?>
<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-2.5.xsd"
      default-autowire="byName">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${chat.jdbc.driver}"/>
        <property name="url" value="${chat.jdbc.url}"/>
        <property name="username" value="${chat.jdbc.username}"/>
        <property name="password" value="${chat.jdbc.password}"/>
        <property name="maxActive" value="${chat.jdbc.maxActive}"/>
        <!-- sql 心跳 -->
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="validationQuery" value="select 1"/>
        <property name="validationQueryTimeout" value="1"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="numTestsPerEvictionRun" value="${chat.jdbc.maxActive}"/>
    </bean>

    <bean id="selectedDataSource" name="selectedDataSource"
          class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${select.chat.jdbc.driver}"/>
        <property name="url" value="${select.chat.jdbc.url}"/>
        <property name="username" value="${select.chat.jdbc.username}"/>
        <property name="password" value="${select.chat.jdbc.password}"/>
        <property name="maxActive" value="${select.chat.jdbc.maxActive}"/>
        <!-- sql 心跳 -->
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="validationQuery" value="select 1"/>
        <property name="validationQueryTimeout" value="1"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="numTestsPerEvictionRun" value="${select.chat.jdbc.maxActive}"/>
    </bean>

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocations">
            <list>
                <value>classpath:sqlmap-config.xml</value>
            </list>
        </property>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="sqlMapClientRead" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocations">
            <list>
                <value>classpath:sqlmap-config.xml</value>
            </list>
        </property>
        <property name="dataSource" ref="selectedDataSource"/>
    </bean>

    <bean id="selectDao" class="com.jd.im.king.template.dao.impl.SelectDaoImpl">
        <property name="sqlMapClient" ref="sqlMapClientRead"/>
    </bean>

    <bean id="writeDao" class="com.jd.im.king.template.dao.impl.WriteDaoImpl">
        <property name="sqlMapClient" ref="sqlMapClient"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


    Ibatis的查询依赖SqlMapClientFactoryBean这个对象的配置。这个对象同时指定了ibatis的总配置文件和数据源。一共配了两个,一个负责读,一个负责写。

    Ibatis总配置文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map
Config 2.0/" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
    <settings
            cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="false"
            errorTracingEnabled="true"
            maxRequests="32"
            maxSessions="10"
            maxTransactions="5"
            useStatementNamespaces="true"/>

    <sqlMap resource="sqlmap/word.xml"/>
   
</sqlMapConfig>


    sqlMap节点负责导入分配置文件,也就是sql文件。可以有多个。

    ibatis的sql分配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="word">
 
 <typeAlias alias="Word" type="com.**.im.RiskKeyWord"/>
 
    <insert id="add" parameterclass="Word">
        INSERT
        INTO key_word(lib_id, word)
        VALUES (#lib_id#, #word:VARCHAR#)
        <selectKey resultclass="int" keyProperty="id">
            select last_insert_id() limit 1
        </selectKey>
    </insert>
 
 <update id="update" parameterclass="Word">
  UPDATE key_word
  SET lib_id = #lib_id#, word = #word:VARCHAR#
  WHERE id = #id#
 </update>
 
 <select id="query" parameterclass="int" resultclass="Word">
  SELECT id, lib_id, word
  FROM key_word
  WHERE  id = #id#
 </select>
 
 <delete id="del" parameterclass="int" >
  DELETE
  FROM key_word
  WHERE  id = #id#
 </delete>
</sqlMap>

    ibatis的分配置文件里都是sql语句。

    至此,这四个框架的搭建完成。

相关推荐