你的位置:首页 > Java教程

[Java教程]Maven项目管理:SpringMVC+Mybatis+Velocity整合笔记


Maven创建项目

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

配置Spring MVC

  1. 导入Spring MVC 需要的包
    在pom.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
      <!-- 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.在WEB-INF 目录下 添加baseweb-servlet.文件,启用注解和添加自动包扫描

    1
    2
    3
    4
    5
    <!-- 启用springmvc注解 -->
    <context:annotation-config />

    <!-- 配置扫描路径 -->
    <context:component-scan base-package="net.admol.baseweb"></context:component-scan>

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

添加Velocity

  1. 在pom.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!-- 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.文件加入velocity配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <!-- 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.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.在 /src/main/webapp/WEB-INF 目录下添加一个 config 目录,在config下添加一个名为velocity-toolbox.的文件:

    1
    2
    3
    4
    5
    6
    7
    8
    <?<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文件,内容如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <!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

    1
    2
    3
    4
    5
    6
    7
    8
    @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页面:

    1
    $!test

    启动tomcat ,测试结果:
    整合velocity测试结果整合velocity测试结果

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

整合Mybatis

  1. 引入包
    在pom.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
      <!-- 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的数据库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!-- 创建表 -->
    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.配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <?<beans "http://www.springframework.org/schema/beans"
    "http://www.w3.org/2001/"http://www.springframework.org/schema/aop"
    "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. <property name="mapperLocations" value="classpath:sqlmap/*.</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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath*:spring/*. </param-value>
    </context-param>
  4. 生成do、DAO、Mapping
    在 /src/main/resources 下新建folder : sqlmap,
    新建包net.admol.baseweb.dal.daonet.admol.baseweb.dal.dateobject
    配置 Mybatis-generator. 点击查看代码
    下载相应包:mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.31.jar并与generatorConfig.文件位于同一目录下,
    然后执行命令java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig. 更多命令使用

  5. 测试
    修改 Test.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    @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:

1
2
3
4
5
6
$!test

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

 

启动romcat,输入http://localhost:8080/baseweb/test.htm,测试结果:
整合Mybatis测试结果整合Mybatis测试结果
成功访问数据库!