| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
nf456
10年前发布

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="${jdbc.driver}"/>          <property name="url" value="${jdbc.url}"/>          <property name="username" value="${jdbc.username}"/>          <property name="password" value="${jdbc.password}"/>      </bean>      <bean id="sessionFactory"            class="org.mybatis.spring.SqlSessionFactoryBean">          <property name="dataSource" ref="dataSource"/>          <property name="configLocation" value="classpath:config/mybatis-config.xml"/>          <property name="typeAliasesPackage" value="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="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="/WEB-INF/views/"/>          <!-- Velocity配置文件 -->          <property name="configLocation" value="classpath:velocity.properties"/>      </bean>        <!-- 配置视图的显示 -->      <bean id="ViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">          <property name="prefix" value="/"/>          <!-- 视图文件的前缀,即存放的路径 -->          <property name="suffix" value=".vm"/>          <!-- 视图文件的后缀名 -->                    <!--<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml"/>-->          <!--toolbox配置文件路径-->          <property name="dateToolAttribute" value="date"/>          <!--日期函数名称-->          <property name="numberToolAttribute" value="number"/>          <!--数字函数名称-->          <property name="contentType" value="text/html;charset=UTF-8"/>          <property name="exposeSpringMacroHelpers" value="true"/>          <!--是否使用spring对宏定义的支持-->          <property name="exposeRequestAttributes" value="true"/>          <!--是否开放request属性-->          <property name="requestContextAttribute" value="rc"/>          <!--request属性引用名称-->                    <!--指定默认layout文件-->          <property name="layoutUrl" value="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>


示例

可访问源码



 本文由用户 nf456 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1420380132890.html
SpringMVC JEE框架