首页 > 代码库 > mybatis springmvc velocity环境搭建

mybatis springmvc velocity环境搭建

前言

轻量级ORM框架MyBatis完美的配合SpringMVC web框架实现了后台action的开发,结合Java模版引擎velocity实现了Java代码与前端代码的隔离。

搭建过程

后台配置mybatis

添加依赖

Spring 3.2.4-RELEASE

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

mybatis

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.3</version>
        </dependency>

连接池dbcp

       <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>


配置

mybatis-config.xml,此处只是示例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <typeAliases>
        <!--系统中持久层对象-->
        <typeAlias type="com.buglife.webutil.model.Test" alias="Test"/>
    </typeAliases>
    <mappers>
        <!--系统中持久层对象对应的Mapper映射XML文件-->
        <mapper resource="com/buglife/webutil/mapper/TestMapper.xml" />
    </mappers>
</configuration>

applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!--加载JDBC配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <tx:annotation-driven/>
    <context:component-scan base-package="com.buglife.webutil.web.service.impl"></context:component-scan>

    <!--配置数据源(连接池)-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="http://www.mamicode.com/${jdbc.driver}"/>
        <property name="url" value="http://www.mamicode.com/${jdbc.url}"/>
        <property name="username" value="http://www.mamicode.com/${jdbc.username}"/>
        <property name="password" value="http://www.mamicode.com/${jdbc.password}"/>
    </bean>
    <bean id="sessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="http://www.mamicode.com/classpath:config/mybatis-config.xml"/>
        <property name="typeAliasesPackage" value="http://www.mamicode.com/com.buglife.webutil.model"/>
    </bean>

    <!--配置事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!--通过扫描Mapper所在包名自动注入Mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="http://www.mamicode.com/com.buglife.webutil.dao"/>
    </bean>
</beans>

自动生成Model和Mapper

创建generatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!--加载数据库连接驱动包-->
    <classPathEntry
            location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
    <context id="context1" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/webutil?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="root" password="root" />
                        
       <!--Model存放位置-->
        <javaModelGenerator targetPackage="com.buglife.webutil.model"
                            targetProject="src/main/java" />
       <!--Mapper xml文件存放位置-->
        <sqlMapGenerator targetPackage="com.buglife.webutil.mapper"
                         targetProject="src/main/resources" />
        <!--Mapper 接口存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.buglife.webutil.dao" targetProject="src/main/java" />
        
        <!--配置需要用到的数据表-->
        <table schema="webutil" tableName="test" />
    </context>
</generatorConfiguration>

添加mybatis-generate插件

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                    <!--generatorConfig.xml存放位置-->
                    <configurationFile>src\main\resources\config\generatorConfig.xml</configurationFile>
                </configuration>
                <executions>
                    <!--自动生成Mapper和model的生命周期段-->
                    <execution>
                        <id>mapper-generate</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这时执行maven的命令mvn install则可以生成mybatis所需的model和Mapper


前端搭建velocity

添加依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${version.spring}</version>
        </dependency>
        <!--支持velocity的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${version.spring}</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>

在SpringMVC中配置velocity

<!-- 配置velocity引擎 -->
    <bean id="velocityConfigurer"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <!-- 模板存放的路径 -->
        <property name="resourceLoaderPath" value="http://www.mamicode.com/WEB-INF/views/"/>
        <!-- Velocity配置文件 -->
        <property name="configLocation" value="http://www.mamicode.com/classpath:velocity.properties"/>
    </bean>

    <!-- 配置视图的显示 -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="prefix" value="http://www.mamicode.com/"/>
        <!-- 视图文件的前缀,即存放的路径 -->
        <property name="suffix" value="http://www.mamicode.com/.vm"/>
        <!-- 视图文件的后缀名 -->
        
        <!--<property name="toolboxConfigLocation" value="http://www.mamicode.com/WEB-INF/tools.xml"/>-->
        <!--toolbox配置文件路径-->
        <property name="dateToolAttribute" value="http://www.mamicode.com/date"/>
        <!--日期函数名称-->
        <property name="numberToolAttribute" value="http://www.mamicode.com/number"/>
        <!--数字函数名称-->
        <property name="contentType" value="http://www.mamicode.com/text/html;charset=UTF-8"/>
        <property name="exposeSpringMacroHelpers" value="http://www.mamicode.com/true"/>
        <!--是否使用spring对宏定义的支持-->
        <property name="exposeRequestAttributes" value="http://www.mamicode.com/true"/>
        <!--是否开放request属性-->
        <property name="requestContextAttribute" value="http://www.mamicode.com/rc"/>
        <!--request属性引用名称-->
        
        <!--指定默认layout文件-->
        <property name="layoutUrl" value="http://www.mamicode.com/layout/default.vm"/>
    </bean>

velocity配置文件velocity.properties,主要配置输入输出编码格式

#指定宏定义位置
velocimacro.library = /WEB-INF/macros/macros.vm
input.encoding=UTF-8
output.encoding=UTF-8

最后看下web.xml中的配置情况

    <servlet>
        <servlet-name>velocity</servlet-name>
        <servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class>
        <init-param>
            <param-name>org.apache.velocity.tools.deprecationSupportMode</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>org.apache.velocity.tools.cleanConfiguration</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>org.apache.velocity.tools.userCanOverwriteTools</param-name>
            <param-value>false</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>velocity</servlet-name>
        <url-pattern>*.vm</url-pattern>
    </servlet-mapping>


示例

可访问源码







mybatis springmvc velocity环境搭建