搭建springmvc+ibatis+velocity工程

本文介绍如何使用Maven搭建Web应用程序,并集成Spring MVC、Velocity模板引擎及iBatis ORM框架,实现基本的Web开发环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一、搭建工程

1.  创建空的webapp工程:

在想要创建工程的路径下,打开cmd,输入命令:

mvn archetype:create -DgroupId=com.sw.app -DartifactId=app -DarchetypeArtifactId=maven-archetype-webapp

其中app是工程名称。

-DgroupId=com.sw.app 填入自己的groupId

-DartifactId=app 填入自己的artifactId

-DarchetypeArtifactId=maven-archetype-webapp 表示要生成的工程类型是web工程,这个不做修改。

关于groupId和artifactId的概念,可以阅读如jiangshachina的《Maven入门--概念与实例》:http://www.blogjava.net/jiangshachina/archive/2006/09/01/67080.html

运行好后,会生成一个基础的web工程。

 

 

2.  补全标准mvn目录:

在src/main/下创建java/com/sw/app/文件夹

在src/下创建test/java/com/sw/app/文件夹,以及test/resources/文件夹

 

 

二、集成springmvc+velocity

1.  引入springmvc的jar包

在pom.xml中加入依赖

<!-- spring -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring</artifactId>

    <version>2.5.4</version>

</dependency>

 

<!-- spring mvc -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>2.5.4</version>

</dependency>

<!-- velocity -->
<dependency>

<groupId>velocity</groupId>
<artifactId>velocity-dep</artifactId>
 <version>1.4</version>
</dependency>


所要依赖jar包的groupId、artifactId和版本号,可以到http://www.mvnrepository.com/ 查询。

 

2.  配置web.xml

    打开src/main/webapp/WEB-INF/web.xml,填入下面的配置:

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4"

    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 

    <display-name>app</display-name>

 

    <!-- Welcome page -->

    <welcome-file-list>

        <welcome-file>index.html</welcome-file>

    </welcome-file-list>

   

    <!-- Config file location -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>

            classpath:META-INF/spring/**/*.xml

        </param-value>

    </context-param>

 

    <!-- Spring MVC DispatcherServlet -->

    <servlet>

        <servlet-name>app</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <!-- Dispatcher servlet mapping for the main web user interface. -->

    <servlet-mapping>

        <servlet-name>app</servlet-name>

        <url-pattern>*.htm</url-pattern>

    </servlet-mapping>

 

    <!-- To support chinese gbk encoding -->

    <filter>

        <filter-name>CharacterSetEncoding Filter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

            <param-name>encoding</param-name>

            <param-value>GBK</param-value>

        </init-param>

        <init-param>

            <param-name>forceEncoding</param-name>

            <param-value>true</param-value>

        </init-param>

    </filter>

    <filter-mapping>

        <filter-name>CharacterSetEncoding Filter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

   

    <!--Spring ApplicationContext -->

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

 

</web-app>

    其中app是工程名称。

3.  补全spring目录:

在src/main/resources下创建META-INF/spring文件夹

 

4.  添加app-servlet.xml文件

app是工程名称,在src/main/webapp/WEB-INF文件夹下创建app-servlet.xml文件,填入如下配置:

<?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:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

    <!-- Autodetect POJOs labeled with the @RequestMapping annotation -->

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

   

    <!-- Autodetect methods labeled with the @RequestMapping annotation -->

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

   

    <!-- Autodetect POJOs labeled with the @Controller annotation -->

    <context:component-scan base-package="com.sw" />

 

    <!-- Configurer that sets up a shared VelocityEngine for Velocity views -->

    <bean id="velocityConfigurer"

        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">

        <property name="velocityProperties">

            <props>

                <prop key="input.encoding">GBK</prop>

                <prop key="output.encoding">GBK</prop>

            </props>

        </property>

        <property name="resourceLoaderPath" value="WEB-INF/views/" />

    </bean>

    <!-- Simple ViewResolver for Velocity, appending ".vm" to logical view names -->

    <bean id="viewResolver"

        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">

        <property name="contentType" value="text/html;charset=gbk" />

        <property name="suffix" value=".vm" />

    </bean>

</beans>

 

5.  补全velocity目录:

在src/main/webapp/WEB-INF下创建views文件夹。

 

 

三、集成ibatis+mysql

 

1.  引入jar包

    打开pom.xml,添加如下依赖:

<!-- ibatis -->

     <dependency>

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

       

        <dependency>

            <groupId>commons-dbcp</groupId>

            <artifactId>commons-dbcp</artifactId>

            <version>1.2.1</version>

        </dependency>

       

        <!-- mysql -->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.14</version>

        </dependency>

 

2.  增加数据库配置:

在src/main/resources/META-INF/spring文件夹下新建db文件夹,新增db.xml文件,内容如下:

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

        <property name="url" value="jdbc:mysql://localhost:3306/sw"/>

        <property name="username" value="root"/>

        <property name="password" value="root"/>

    </bean>

   

    <bean id="transactionTemplate"

        class="org.springframework.transaction.support.TransactionTemplate">

        <property name="transactionManager">

            <ref local="transactionManager" />

        </property>

    </bean>

 

    <bean id="transactionManager"

        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

   

    <bean id="baseDAO"

        class="org.springframework.orm.ibatis.support.SqlMapClientDaoSupport"

        abstract="true">

        <property name="sqlMapClient">

            <ref local="sqlMapClient" />

        </property>

        <property name="dataSource">

            <ref local="dataSource" />

        </property>

    </bean>

 

   

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

        <property name="configLocation" value="classpath:sqlmap/sqlmap.xml"/>

    </bean>

   

</beans>

 

3.  增加sqlmap配置:

在src/main/resources文件夹下建立sqlmap文件夹,新建sqlmap.xml:

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

    <settings cacheModelsEnabled="true" enhancementEnabled="false" lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000" maxTransactions="3000" useStatementNamespaces="false"/>

   

    <sqlMap resource="sqlmap/xxx-sqlmap-mapping.xml"/>

</sqlMapConfig>

 

xxx为具体表名。

 

4.  增加xxx数据库表sqlmap配置,举例:

在src/main/resources/sqlmap文件夹下增加xxx-sqlmap-mapping.xml:

<?xml version="1.0" encoding="GB2312" ?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

 

<sqlMap>

    <resultMap id="user" class="com.sw.app.dal.dataobject.UserDO">

        <result property="id" column="ID" javaType="long" jdbcType="INT" nullValue="0"/>

        <result property="name" column="NAME" javaType="java.lang.String" jdbcType="VARCHAR"/>

    </resultMap>

   

    <insert id="user_insert">

        <![CDATA[

            insert into user (name) values (#name#)

        ]]>

        <selectKey resultClass="long" keyProperty="id" >

            SELECT @@IDENTITY AS ID

        </selectKey>

    </insert>

   

    <select id="user_selectyByName" resultMap="user">

        <![CDATA[

            SELECT id,name FROM user WHERE name=#name#

        ]]>

    </select>

   

</sqlMap>

 

5.  增加dao.xml:

在src/main/resources/META-INF/spring/dal文件夹下增加dao.xml,举例:

<?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:osgi="http://www.springframework.org/schema/osgi"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"

    default-autowire="byName">

   

    <bean id="userDAO" class="com.sw.app.dal.dao.impl.UserDAOImpl" parent="baseDAO"/>

   

</beans>

 

 

四、配置运行环境

1.  配置pom.xml:

    <build>

        <finalName>app</finalName>

        <plugins>

            <!-- Configuring the Jetty Plugin -->

            <plugin>

                <groupId>org.mortbay.jetty</groupId>

                <artifactId>maven-jetty-plugin</artifactId>

                <configuration>

                    <scanIntervalSeconds>10</scanIntervalSeconds>

                </configuration>

            </plugin>

           

            <!-- Configuring the Mave Eclipse Plugin -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-eclipse-plugin</artifactId>

                <configuration>

                    <downloadSources>true</downloadSources>

                    <source>1.7</source>

                    <target>1.7</target>

                </configuration>

            </plugin>

 

            <!-- Configuring the Mave Compiler Plugin -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                    <source>1.7</source>

                    <target>1.7</target>

                </configuration>

            </plugin>

        </plugins>

    </build>

 

2.  导入eclipse

在根目录下运行mvn eclipse:eclipse,然后打开eclipse导入。

 

3.  启动

在根目录下运行mvn jetty:run,输入http://localhost:8080/app/***.htm,即可访问页面。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值