Maven创建项目
略…
具体过程可参考用Maven创建第一个web项目
配置Spring MVC
-
导入Spring MVC 需要的包
在pom.xml 文件下加入:
-
添加baseweb-servlet.xml文件
在WEB-INF 目录下 添加baseweb-servlet.xml
文件,启用注解和添加自动包扫描
更多详细可参考使用springMVC实现简单的登录例子
添加Velocity
-
在pom.xml 文件下加入:
-
在baseweb-servlet.xml
文件加入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-toolbox.xml 文件
在 /src/main/webapp/WEB-INF
目录下添加一个 config
目录,在config
下添加一个名为velocity-toolbox.xml
的文件:
1 2 3 4 5 6 7 8
|
<?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
文件,内容如:
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>
|
-
配置macros.vm(非必须)
在/WEB-INF/velocity/macros/
目录添加 macros.vm
文件
-
测试
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
页面:
启动tomcat ,测试结果:
整合velocity测试结果
https://github.com/mybatis/generator/releases
整合Mybatis
-
引入包
在pom.xml文件加入:
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
|
|
-
准备数据库
创建一个名为demo
的数据库
-
添加配置文件
在 /src/main/resources
下新建folder :spring
,新建 spring-dao.xml
配置文件:
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
|
<?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" />
|
在web.xml加入:
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/*.xml </param-value> </context-param>
|
-
生成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
更多命令使用
-
测试
修改 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测试结果
成功访问数据库!