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

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

395次阅读
没有评论

共计 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、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7992229
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

仅2MB大小!开源硬件监控工具:Win11 无缝适配,CPU、GPU、网速全维度掌控

还在忍受动辄数百兆的“全家桶”监控软件?后台偷占资源、界面杂乱冗余,想查个 CPU 温度都要层层点选? 今天给...
星哥带你玩飞牛NAS-16:飞牛云NAS换桌面,fndesk图标管理神器上线!

星哥带你玩飞牛NAS-16:飞牛云NAS换桌面,fndesk图标管理神器上线!

  星哥带你玩飞牛 NAS-16:飞牛云 NAS 换桌面,fndesk 图标管理神器上线! 引言 哈...
星哥带你玩飞牛NAS-7:手把手教你免费内网穿透-Cloudflare tunnel

星哥带你玩飞牛NAS-7:手把手教你免费内网穿透-Cloudflare tunnel

星哥带你玩飞牛 NAS-7:手把手教你免费内网穿透 -Cloudflare tunnel 前言 大家好,我是星...
2025年11月28日-Cloudflare史诗级事故:一次配置失误,引爆全球宕机

2025年11月28日-Cloudflare史诗级事故:一次配置失误,引爆全球宕机

2025 年 11 月 28 日 -Cloudflare 史诗级事故: 一次配置失误,引爆全球宕机 前言 继今...
把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地 大家好,我是星哥,今天教大家在飞牛 NA...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年 0.99 刀,拿下你的第一个顶级域名,详细注册使用 前言 作为长期折腾云服务、域名建站的老玩家,星哥一直...
星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛NAS硬件02:某鱼6张左右就可拿下5盘位的飞牛圣体NAS

星哥带你玩飞牛 NAS 硬件 02:某鱼 6 张左右就可拿下 5 盘位的飞牛圣体 NAS 前言 大家好,我是星...
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

  告别 Notion 焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁” 引言 在数字笔记工...