1.新建maven项目,配置好路径(可参考http://www.cnblogs.com/wang-zl/p/5176630.html)
2.在WEB-INF目录下创建applicationContext.xml文件
3.配置applicationContext.xml:
表头
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> </beans>
配置数据库连接池
<!-- 配置数据源 --> <bean id="dataSourceMySQL" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- <bean id="dataSourceMySQL" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/commanddatabase?characterEncoding=utf8"/> <property name="username" value="root" /> <property name="password" value="" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="3" /> <!-- 连接池的最大值 --> <property name="maxActive" value="20" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="5" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="3" /> </bean>
配置工厂,扫描sql.xml文件
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceMySQL"></property> <property name="mapperLocations" value="classpath:cn/springmvc/sql/*.xml"></property> </bean>
扫描Dao包
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.springmvc.dao"></property> <!-- 可以不写 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> </bean>
配置视图解析器,用于查找jsp页面
<!-- viewresolver 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"> </property> <property name="suffix" value=".jsp"> </property> </bean>
扫描controller包
<!-- 扫描service,controller --> <context:component-scan base-package="cn.springmvc"/>
配置注解
<!-- handlermapping @RequestMapping,@ResponseBody,@ExceptionHanlder --> <mvc:annotation-driven/>
配置restful,如不使用可以不配置
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
3.配置web.xml文件
表头,创建maven项目是生成(3.0)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"/>
制定首页
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
静态加载
<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping>
配置restful访问方式
<!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>confReloadCheckInterval</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
拦截器配置(不拦截静态资源)
<!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <servlet-name>springmvc</servlet-name> </filter-mapping>
4.配置pom.xml文件
项目创建时生成
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maven-spring-mybaties-restful</groupId> <artifactId>maven-spring-mybaties-restful</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> </project>
配置动态引用包方法(方便灵活,在版本变化时直接修改就可以一次盖顶)
<properties> <spring.version>4.1.1.RELEASE</spring.version> <jackson.version>2.4.0</jackson.version> <mybatis.version>3.2.7</mybatis.version> <druid.version>1.0.16</druid.version> </properties>
配置jdk默认插件
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
配置servlet插件(<scop>provided</scop>依赖只有在当JDK 或者一个容器已提供该依赖之后才使用:此处为避免tomcat加载冲突)
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies>
依赖注入Spring、mybaties、数据库包(特别说明:数据库连接池引用阿里巴巴德鲁伊连接池,功能强大。想具体了解可baidu)
<!-- org.apache.commons.dbcp.BasicDataSource 开始 <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> org.apache.commons.dbcp.BasicDataSource 结束 --> <!-- 阿里巴巴德鲁伊连接池 start--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- 阿里巴巴德鲁伊连接池 end--> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.tuckey</groupId> <artifactId>urlrewritefilter</artifactId> <version>4.0.4</version> </dependency> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!--spring security 安全访问控制解决方案的安全框架 <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.3.RELEASE</version> </dependency> spring security 安全访问控制解决方案的安全框架 --> <!-- spring end --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency>
在依赖注入过程中出现常见问题:如缺少servlet插件导致项目启动报错,缺少tomcat插件等,可在problem中查找问题,一 一解决(耐心细致一步步解决)
5.编写简单项目测试,目录结构如下:
右键项目名称,Run As-->run configuration:compile tomcat:run(命令可创建保存,下次直接启动)
项目启动成功:
在浏览器输入上图url(注意在url后面加”/“,这是因为配置文件)
点击hello按钮,页面跳转:
运行成功!
仅供参考!如有转载请写好原文出处!非常非常感谢!
非常乐意接受您的宝贵意见谢谢
提示:在配置文件中要仔细的填好自己的项目包名!
项目可自行编写!尽情发挥自己的创造力吧!
附上源码下载地址:https://github.com/superwangl/maven-spring-mybaties-restful.git