spring-mybatis-freemarker springMVC环境搭建

2025-05-22 18:49:12

1、首先创建maven项目(本帖以IDEA为开发环境)。点击file-->new-->project;在弹出的界面中,左侧选中maven,右上方选择需要的JDK,选择完毕点击next。

spring-mybatis-freemarker springMVC环境搭建spring-mybatis-freemarker springMVC环境搭建

2、定义属性,引入spring、mybatis、mysql、freemarker需要的jar。<properties> <!-- 项目属性 --> <encoding>UTF-8</encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <JDK.version>1.7</JDK.version> <spring.version>4.3.13.RELEASE</spring.version> <mybatis-spring.version>1.3.2</mybatis-spring.version> <mybatis.version>3.4.6</mybatis.version> <mysql-driver.version>5.1.18</mysql-driver.version> <freeMarker.version>2.3.23</freeMarker.version> <servlet.version>4.0.1</servlet.version></properties><dependencies> <!--核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!--管理bean--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!--上下文--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!--面向对象--> <dependency> <groupId>org.springframework </groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!--el表达式--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!--JDBC支持--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--MVC控制层框架--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> </dependency> <!--测试包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--mybatis-spring 适配器--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!--mybatis jar--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- freemarker jar --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freeMarker.version}</version> </dependency> <!--以下为工具包 可以不引入--> <dependency> <!--Gson 对象转json 使用本地包--> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/gson-2.8.0.jar</systemPath> </dependency> <dependency> <!--Json json转对象--> <groupId>org.nutz</groupId> <artifactId>nutz</artifactId> <version>1.r.60</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <!--工具包结束--></dependencies>jar引入完成

3、在src/main/下,新建目录 webapp。然后先建立好我们需要的package、directory、xml、properties等。如图所示,根据需要进行。

spring-mybatis-freemarker springMVC环境搭建

10、测试freemarker是否整合成功。1、在mvcViews目录下,新建一个html,然后重命名为index.ftl。2、在controller包下,新建一个HomeController类。3、重启tomcat,访问http://127.0.0.1:8080/spring/pc/index?name=张三文件内容如下图:

spring-mybatis-freemarker springMVC环境搭建spring-mybatis-freemarker springMVC环境搭建
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢