阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

125次阅读
没有评论

共计 14011 个字符,预计需要花费 36 分钟才能阅读完成。

总结下如何用这四个常用框架搭建一个 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 语句。

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

SpringMVC 总结篇  http://www.linuxidc.com/Linux/2016-06/132659.htm

Spring+SpringMVC 企业快速开发架构搭建  http://www.linuxidc.com/Linux/2015-09/122942.htm

SpringMVC 的乱码处理  http://www.linuxidc.com/Linux/2015-07/120542.htm

Spring MVC+Spring3+Hibernate4 开发环境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm 

Spring MVC 整合 Freemarker 基于注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm 

基于注解的 Spring MVC 简单介绍 http://www.linuxidc.com/Linux/2012-02/54896.htm

SpringMVC 详细示例实战教程 http://www.linuxidc.com/Linux/2015-06/118461.htm

Spring MVC 框架搭建及详解 http://www.linuxidc.com/Linux/2012-01/52740.htm

SpringMVC 异常处理  http://www.linuxidc.com/Linux/2015-06/119049.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-10/135846.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计14011字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中