Maven项目管理:SpringMVC+Mybatis+Velocity整合笔记

MelEQQL 4年前

来自: http://www.cnblogs.com/admol/p/5246032.html

Maven创建项目

略…

具体过程可参考 用Maven创建第一个web项目

配置Spring MVC

  1. 导入Spring MVC 需要的包在pom.xml 文件下加入:

       <!-- spring mvc begin -->  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-webmvc</artifactId>   <version>3.2.6.RELEASE</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context-support</artifactId>   <version>3.2.6.RELEASE</version>  </dependency>  <!-- spring mvc end -->
  2. 添加baseweb-servlet.xml文件

    在WEB-INF 目录下 添加 baseweb-servlet.xml 文件,启用注解和添加自动包扫描

    <!-- 启用springmvc注解 -->  <context:annotation-config />    <!-- 配置扫描路径 -->  <context:component-scan base-package="net.admol.baseweb"></context:component-scan>

更多详细可参考 使用springMVC实现简单的登录例子

添加Velocity

  1. 在pom.xml 文件下加入:

    <!-- velocity begin -->  <dependency>   <groupId>org.apache.velocity</groupId>   <artifactId>velocity</artifactId>   <version>1.6.2</version>  </dependency>  <dependency>   <groupId>org.apache.velocity</groupId>   <artifactId>velocity-tools</artifactId>   <version>1.3</version>  </dependency>  <!-- velocity end -->
  2. 在 baseweb-servlet.xml 文件加入velocity配置:

    <!-- velocity view config -->  <bean id="velocityViewResolver"   class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">   <property name="cache" value="true" />   <property name="exposeSpringMacroHelpers" value="true" />   <property name="requestContextAttribute" value="true" />   <property name="exposeSessionAttributes" value="true" />   <property name="prefix" value=""></property>   <property name="order" value="1"></property>   <property name="allowSessionOverride" value="true"></property>   <property name="viewNames">   <list>   <value>*.vm</value>   <value>*.htm</value>   </list>   </property>   <property name="contentType" value="text/html; charset=UTF-8"></property>   <property name="toolboxConfigLocation" value="/WEB-INF/config/velocity-toolbox.xml"></property>   <property name="viewClass"   value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" />   <property name="layoutUrl" value="layout/layout.vm" />  </bean>    <bean id="velocityConfig"   class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">   <property name="resourceLoaderPath" value="/WEB-INF/velocity/" />   <property name="velocityProperties">   <props>   <prop key="input.encoding">UTF-8</prop>   <prop key="output.encoding">UTF-8</prop>   <prop key="parser.pool.size">100</prop>   <prop key="velocimacro.library">macros/macros.vm</prop>   <prop key="velocimacro.library.autoreload">true</prop>   </props>   </property>  </bean>
  3. 添加 velocity-toolbox.xml 文件

    在  /src/main/webapp/WEB-INF 目录下添加一个  config 目录,在 config 下添加一个名为 velocity-toolbox.xml 的文件:

    <?xml version="1.0"?>  <toolbox>    <tool>   <key>dateTool</key>   <scope>request</scope>   <class>org.apache.velocity.tools.generic.DateTool</class>   </tool>  </toolbox>
  4. 添加layout.vm

    在  /src/main/webapp/WEB-INF 目录下添加一个  velocity 目录, velocity 目下添加一个 layout 目录,目录下新建一个 layout.vm 文件,内容如:

    <!DOCTYPE HTML>  <html>  <head>   <meta charset="UTF-8">    <title>admol</title>  </head>  <body class="warp">   <div class="container">   <div class="content">   ${screen_content}   </div>   </div>   </body>  </html>
  5. 配置macros.vm(非必须)

    在 /WEB-INF/velocity/macros/ 目录添加  macros.vm 文件

  6. 测试Test.java

    @Controller  public class Test {   @RequestMapping(value = "/test.htm")   public String test(ModelMap mdelMap) {   mdelMap.addAttribute("test", "hello , this is velocity!");   return "/testView/testVelocity.vm";   }  }

    编写页面

    新建 /velocity/testView/testVelocity.vm 页面:

    $!test

    启动tomcat ,测试结果:

    整合velocity测试结果

https://github.com/mybatis/generator/releases

整合Mybatis

  1. 引入包在pom.xml文件加入:

       <!-- mysql -->   <dependency>   <groupId>mysql</groupId>   <artifactId>mysql-connector-java</artifactId>   <version>5.1.31</version>  </dependency>    <!-- mybatis -->   <dependency>   <groupId>org.mybatis</groupId>   <artifactId>mybatis-spring</artifactId>   <version>1.2.2</version>  </dependency>  <dependency>   <groupId>org.mybatis</groupId>   <artifactId>mybatis</artifactId>   <version>3.2.0</version>  </dependency>   <dependency>   <groupId>commons-dbcp</groupId>   <artifactId>commons-dbcp</artifactId>   <version>1.2</version>  </dependency>    <!-- spring jdbc -->  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-jdbc</artifactId>   <version>3.2.6.RELEASE</version>  </dependency>
  2. 准备数据库

    创建一个名为 demo 的数据库

    <!-- 创建表 -->  CREATE TABLE `user` (   `id` varchar(32) NOT NULL,   `user_id` varchar(32) NOT NULL,   `user_name` varchar(64) NOT NULL,   PRIMARY KEY (`id`)  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  <!-- 插入测试数据 -->  INSERT INTO `user` VALUES ('1', '1000', 'admol');
  3. 添加配置文件

    在  /src/main/resources 下新建folder : spring ,新建  spring-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:aop="http://www.springframework.org/schema/aop"   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/tx   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd   http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"   default-autowire="byName">     <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://127.0.0.1:3306/demo" />    <property name="username" value="test" />    <property name="password" value="123456" />    <!-- 初始化连接大小 -->    <property name="initialSize" value="5"></property>    <!-- 连接池最大数量 -->    <property name="maxActive" value="20"></property>    <!-- 连接池最大空闲 -->    <property name="maxIdle" value="20"></property>    <!-- 连接池最小空闲 -->    <property name="minIdle" value="1"></property>    <!-- 获取连接最大等待时间 -->    <property name="maxWait" value="60000"></property>    </bean>       <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource" />    <!-- 自动扫描mapper.xml文件 -->   <property name="mapperLocations" value="classpath:sqlmap/*.xml"></property>    </bean>      <!-- DAO接口所在包名,Spring会自动查找其下的类 -->   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="net.admol.baseweb.dal.dao" />    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>  </beans>

    在web.xml加入:

    <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>   classpath*:spring/*.xml   </param-value>  </context-param>
  4. 生成do、DAO、Mapping

    在  /src/main/resources 下新建folder :  sqlmap ,

    新建包 net.admol.baseweb.dal.dao 和 net.admol.baseweb.dal.dateobject

    配置  Mybatis-generator.xml 点击查看代码

    下载相应包: mybatis-generator-core-1.3.2.jar 和 mysql-connector-java-5.1.31.jar 并与 generatorConfig.xml 文件位于同一目录下,

    然后执行命令 java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite 更多命令使用

  5. 测试

    修改  Test.java :

    @Controller  public class Test {   @Autowired   private UserMapper userMapper;      @RequestMapping(value = "/test.htm")   public String test(ModelMap mdelMap) {   mdelMap.addAttribute("test", "hello , this is velocity!!!");   testDB(mdelMap);   return "/testView/testVelocity.vm";   }      public void testDB(ModelMap mdelMap) {   UserExample ss = new UserExample();   ss.createCriteria().andUserIdEqualTo("1000");   List<User> list = userMapper.selectByExample(ss);   mdelMap.addAttribute("list", list);   System.out.println("测试结果size:" + list.size());   }  }

修改 testVelocity.vm :

$!test    #foreach($userInfo in $!list)   </br>   $!{velocityCount}. $!userInfo.userName   #end

启动romcat,输入 http://localhost:8080/baseweb/test.htm ,测试结果:

整合Mybatis测试结果

成功访问数据库!

</div> </div>