流程
1,maven 依赖
2,spring 配置文件
3,mybatis 配置文件
4,springMVC 配置文件
5,web.xml
pom依赖
1,定义版本
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spirng.version>4.3.14.RELEASE</spirng.version> <spirngmvc.version>4.3.14.RELEASE</spirngmvc.version> <mybatis.version>3.4.1</mybatis.version> <ms.version>1.3.0</ms.version> <jackson.version>2.9.5</jackson.version> <dbcp.version>1.4</dbcp.version> <mysql.version>5.1.38</mysql.version> <servlet.version>3.0.20100224</servlet.version> </properties>
2,依赖
<!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spirng.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spirng.version}</version> </dependency> <!-- Servlet --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${ms.version}</version> </dependency> <!-- springMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spirngmvc.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spirngmvc.version}</version> </dependency> <!-- Servlet --> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${ms.version}</version> </dependency> <!-- springMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spirngmvc.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spirngmvc.version}</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <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> <!-- dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${dbcp.version}</version> </dependency>
3,构建设置
<plugins> <!-- 打包时跳过测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <!-- 指定 jdk 版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> <!-- mybatis xml 文件等打包到war --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.tars</include> </includes> <filtering>false</filtering> </resource> </resources>
spring配置文件
<!-- 扫描,支持spring注解 --> <context:component-scan base-package="com.weipai.service.impl,com.weipai.pojo,com.weipai.tarsServer.account.impl" /> <!-- dbcp 数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/weipai?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="WeiPai_111111"/> </bean> <!-- mybatis 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:spring-mybatis.xml" /> <!-- 别名设置 --> <property name="typeAliasesPackage" value="com.weipai.pojo" /> </bean> <!-- mybatis mapper 文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.weipai" /> </bean>
MyBatis 配置文件
<!-- 配置延迟加载 --> <settings> <!-- 打开延迟加载 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 延迟加载的方式:按需加载 --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
springMVC 配置文件
<!-- 开启注解 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫描 --> <context:component-scan base-package="com.weipai.controllers"></context:component-scan> <!-- 配置拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 一个*:拦截当前路径(action中的方法) /stu/* ->>>> 拦截 stu 下的所有请求 两个*:拦截所有请求(action的方法) /** ->>>> 拦截所有请求 --> <mvc:mapping path="/**" /> <!-- 放开特定的方法(指定方法不拦截,别的都拦截) --> <mvc:exclude-mapping path="/stu/queryAll" /> <!-- 拦截器class --> <bean class="com.grts.interceptor.TestInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 解决中文乱码 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
web.xml
<!-- 加载Spring容器配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:servants-spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC 前端控制器 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载配置文件的路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 防止资源文件被spring MVC拦截 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>