Springmvc+Mybatis+Velocity實現小demo(Maven項目)

來源:互聯網
上載者:User

  Velocity只是充當一個展示層,和JSP的功能類似,利用mybatis從資料庫中取出資料,然後進行資料處理,最後通過Velocity在頁面上展示出來。
  環境搭建主要分為幾個過程,第一步是配置pom檔案依賴,第二步是配置spring設定檔:applicationContext.xml,第三步就是開始寫DAO介面和對應的mapping.xml檔案,然後是Service介面和ServiceImpl類,最後就是寫controller。需要注意的是裡面用到一些spring的autowire,這個關係要縷清。
目錄
1.pom檔案依賴: 1.添加Velocity依賴:

       <dependency>            <groupId>org.apache.velocity</groupId>            <artifactId>velocity-tools</artifactId>            <version>2.0</version>        </dependency>        <dependency>            <groupId>org.apache.velocity</groupId>            <artifactId>velocity</artifactId>            <version>1.7</version>        </dependency>

注意要是apache下的velocity,而不是以下這種:

<dependency>    <groupId>velocity</groupId>    <artifactId>velocity</artifactId>    <version>1.5</version>    <type>pom</type></dependency>
2.添加springmvc依賴:
    <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-expression</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.2.5.RELEASE</version>        </dependency>
3.添加mybatis,c3p0資料來源等依賴
<dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.18</version>        </dependency>        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->        <dependency>            <groupId>c3p0</groupId>            <artifactId>c3p0</artifactId>            <version>0.9.1.2</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.2.8</version>        </dependency> <!--注意必須要有這個包-->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.1.1</version>        </dependency>
2.spring設定檔

applicationContext.xml設定檔主要包含幾個部分,一個是springmvc的,一個是velocity的,還有mybatis的配置。記得要在web.xml中配置一下applicationContext.xml的位置。 1.springmvc配置

<!--包掃描--><context:component-scan base-package="com.lumingfeng"></context:component-scan>    <mvc:annotation-driven></mvc:annotation-driven>    <!-- 設定檔,classpath*:*.properties  表示載入多個設定檔,即時有重名的 -->    <context:property-placeholder location="classpath:*.properties"        ignore-unresolvable="false" />        <!-- 設定檔,classpath*:*.properties 表示載入多個設定檔,即時有重名的,ignore-un..=false(如果無法解決就忽視=否)表示如果找到不到設定檔裡面對應的屬性         就報異常,true表示跳過這個屬性而不報異常-->    <context:property-placeholder location="classpath:*.properties"        ignore-unresolvable="false" />      <!-- 配置資料來源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="${jdbc.username}"></property>        <property name="password" value="${jdbc.password}"></property>        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>        <property name="driverClass" value="${jdbc.driverClass}"></property>        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>    </bean>
2.velocity配置
<!-- Velocity配置 -->    <bean id="velocityConfigurer"        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">        <!-- 模板位置,即vm(html)檔案的位置 -->        <property name="resourceLoaderPath" value="WEB-INF/views"></property>        <property name="velocityProperties">            <props>                <prop key="directive.foreach.counter.name">loopCounter</prop>                <prop key="directive.foreach.counter.initial.value">0</prop>                <prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎進行模板處理的編碼 -->                <prop key="output.encoding">UTF-8</prop><!-- 指定輸出資料流的編碼 -->            </props>        </property>    </bean><!-- 視圖解析器 -->    <bean id="viewResolver"        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">        <!-- 以什麼什麼結尾,注意不需要跟jsp的視圖解析器一樣配置prefix,因為首碼和上面的velocityConfigurer配置的位置是一樣的 -->        <property name="suffix" value=".html"></property>        <!-- 類型 -->        <property name="contentType" value="text/html;charset=UTF-8" />        <property name="dateToolAttribute" value="dateTool" />        <property name="numberToolAttribute" value="numberTool" />        <property name="exposeRequestAttributes" value="true" />        <property name="exposeSessionAttributes" value="true" />    </bean>

在視圖解析器的定義中,

“exposeSpringMacroHelpers”設定是否通過Spring的宏庫暴露一個RequestContext(名為springBindRequestContext)供外部使用,預設值為false。它暴露了處理表單和驗證錯誤資訊的宏操作;

“requestContextAttribute”把Spring的RequestContext對象暴露為變數content。利用“ content.contextPath”來擷取應用程式的contextPath;利用 {content.contextPath}”來擷取應用程式的contextPath;利用{content.getMessage(“user.name”)}讀取/WEB-INF/classes/messages.properties本地化資訊。此對象可以為那些不訪問serlvet請求的View技術(類似Velocity和FreeMarker模板)提供不少的方便。

exposeRequestAttributes:預設值false,設定是否所有的request屬性在與模板進行合并之前添加到model中。(request範圍內包含的所有對象,而不是一個Request對象。)

exposeSessionAttributes:預設值false,設定是否所有的session屬性在與模板進行合并之前添加到model中。(理解同上) 3.mybatis配置

配置資料來源:

    <!-- 配置資料來源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="${jdbc.username}"></property>        <property name="password" value="${jdbc.password}"></property>        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>        <property name="driverClass" value="${jdbc.driverClass}"></property>        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>    </bean>

配置Mybatis:
這裡需要搞清楚dao介面,mapping.xml檔案都是幹嘛的。dao介面就只是一個介面,裡面也沒有mybatis的@Insert等註解,然後介面裡面的增刪查改的具體實現方法就是在mapping.xml檔案裡面的,所以可以將一個dao介面和一個mapping.xml檔案相對應。
當然還有另外一種寫法就是直接在dao介面上添加增刪查改的註解,然後寫上sql語句,這種方法就可以不需要mapping.xml檔案。

<!-- 配置Mybatis -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!--dataSource屬性指定要用到的串連池 -->        <property name="dataSource" ref="dataSource" />        <!-- 自動掃描mapping.xml檔案,因為mybatis是在設定檔中執行的對資料庫的操作,我把它統一放到了resource/mybatis檔案夾下 -->        <

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.