Maven项目管理:SpringMVC+Mybatis+Velocity整合笔记
Maven创建项目
略…具体过程可参考用Maven创建第一个web项目
配置Spring MVC
-
导入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 -->
-
添加
baseweb-servlet.xml
文件在WEB-INF 目录下,启用注解和添加自动包扫描<!-- 启用springmvc注解 --> <context:annotation-config /> <!-- 配置扫描路径 --> <context:component-scan base-package="net.admol.baseweb"></context:component-scan>
更多详细可参考使用springMVC实现简单的登录例子
添加Velocity
-
在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 -->
-
在
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>
-
添加 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>
-
添加
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>
-
配置
macros.vm
(非必须)
在/WEB-INF/velocity/macros/
目录添加macros.vm
文件
-
测试
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,访问test页面路径,访问结果略;
整合Mybatis
-
引入包在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>
-
准备数据库创建一个名为
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');
-
添加配置文件
在/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>
-
自动生成DO、DAO、Mapping xml等文件
在
/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
-
测试修改
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
启动Tomcat,输入页面访问路径http://localhost:8080/baseweb/test.htm
,测试结果:整合Mybatis测试结果成功访问数据库!
图略!