最近看見網(wǎng)上整理的Spring+SpringMVC+MyBatis+Maven太low,正好今天有時(shí)間,于是整理了下守呜,那么開始吧!
1、使用的工具
- STS下載地址
http://www.springsource.org/downloads/sts-ggts
- Tomcat
- JDK
- Mybatis generator
-
若使用Eclipse開發(fā)(需要配置Maven,Maven配置好后原杂,在命令行下輸入mvn -v,顯示下圖代表配置成功)
2、Generator的結(jié)構(gòu)
- 首先得有數(shù)據(jù)庫(kù)表
- src文件夾為生成模型的位置(dao,mapping,model)
-
generator.xml配置
- mybatis-generator-core-1.3.2.jar核心包
- 生成語句復(fù)制您机,右鍵在此處打開命令行(注意路徑不能用中文)
-
生成結(jié)果如下圖所示
3穿肄、打開Eclipse新建Maven項(xiàng)目
- 如圖紅色箭頭所示,選擇默認(rèn)的空間际看,next
- 最后Finish
- 最后生成項(xiàng)目結(jié)構(gòu)如下圖所示
- 項(xiàng)目建立后被碗,首先我會(huì)把編碼改成utf-8,省的以后有麻煩。
- 你會(huì)發(fā)現(xiàn)項(xiàng)目目錄沒有顯示出來仿村,那么我們選擇屬性如圖
- 這時(shí)你會(huì)發(fā)現(xiàn)目錄還是沒有變化,那么接下來我們?cè)谡{(diào)試兴喂,默認(rèn)是J2SE-1.5
- 那么我們?cè)诰庉嬛付ㄒ幌滤遥鐖D
- 這時(shí)在回過頭來看項(xiàng)目目錄,以完整衣迷。
4畏鼓、項(xiàng)目建立完畢,引入Spring和MyBatis需要的jar包
- 打開pom.xml
- 如果引入的時(shí)候壶谒,記不住groupId和版本號(hào)云矫,其實(shí)在Eclipse里面有一個(gè)如圖所示,當(dāng)然這種方法不好(請(qǐng)看下一種)
- 那么我們選擇手寫汗菜,打開瀏覽器輸入
http://search.maven.org/
- 記住spring核心包spring-core
- mybatis
- mybatis-spring
- mysql-connector-java
- junit
- 數(shù)據(jù)源druid
- org.aspectj
- 記住后面還有jar包需要加入
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dc_zaichi.mybatis</groupId>
<artifactId>mybatis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
</dependencies>
<build>
</build>
</project>
5让禀、配置文件
- spring
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
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
">
<!-- 引入屬性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自動(dòng)掃描(自動(dòng)注入) -->
<context:component-scan base-package="dc_zaichi.service" />
</beans>
- spring-mybatis
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- JNDI方式配置數(shù)據(jù)源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
<!-- 配置數(shù)據(jù)源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="0" />
<!-- 連接池最大使用連接數(shù)量 -->
<property name="maxActive" value="20" />
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="20" />
<!-- 連接池最小空閑 -->
<property name="minIdle" value="0" />
<!-- 獲取連接最大等待時(shí)間 -->
<property name="maxWait" value="60000" />
<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接陨界,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間巡揍,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打開removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 -->
<property name="logAbandoned" value="true" />
<!-- 監(jiān)控?cái)?shù)據(jù)庫(kù) -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- myBatis文件,這步很重要 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動(dòng)掃描entity目錄, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:dc_zaichi/mapping/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dc_zaichi.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 攔截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* dc_zaichi.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置druid監(jiān)控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>dc_zaichi.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>
- log4j.properties文件
log4j.rootLogger=DEBUG,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=mybatis.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
- config.properties配置文件
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=sypro
#jdbc_password=sypro
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/dc_zaichi?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=helei
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
#jdbc_username=sa
#jdbc_password=123456
#hibernate.dialect=org.hibernate.dialect.DerbyDialect
#driverClassName=org.apache.derby.jdbc.EmbeddedDriver
#validationQuery=SELECT 1
#jdbc_url=jdbc:derby:sy;create=true
#jdbc_username=sypro
#jdbc_password=sypro
#jndiName=java:comp/env/dataSourceName
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
sessionInfoName=sessionInfo
uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached
6菌瘪、這樣我們已經(jīng)將Spring和mybatis完全整合了腮敌,接下來上代碼
- 寫接口,查詢一個(gè)資產(chǎn)俏扩,肯定要返回一個(gè)Asset_Check
package dc_zaichi.service;
import dc_zaichi.model.Asset_Check;
public interface AssetCheckService {
public Asset_Check getAssetCheckById(String id);
}
- 接下來寫實(shí)現(xiàn)類
package dc_zaichi.service.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dc_zaichi.dao.Asset_CheckMapper;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
//AssetCheckServiceImpl把他放到spring上下文環(huán)境中
@Service("AssetCheckService")
public class AssetCheckServiceImpl implements AssetCheckService{
//AssetCheckServiceImpl處理業(yè)務(wù)邏輯后糜工,業(yè)務(wù)邏輯調(diào)用數(shù)據(jù)庫(kù),就是dao,也就是mappper,要使用這個(gè)mapper录淡,一定要通過spring注入捌木,首先要有g(shù)et和set方法,那么如何將Asset_CheckMapper 注入到services里面呢赁咙?在set方法上加@Autowired注解
//當(dāng)然了钮莲,這個(gè)service還是一個(gè)普通的類免钻,因?yàn)闆]有放到spring上下文環(huán)境當(dāng)中,自動(dòng)掃描包后需要一個(gè)serviced的注解告訴掃描的那個(gè)地方崔拥,這個(gè)類是一個(gè)bean极舔。這樣spring上下文在啟動(dòng)的時(shí)候,就能加入到spring環(huán)境當(dāng)中了
private Asset_CheckMapper asset_CheckMapper;
public Asset_CheckMapper getAsset_CheckMapper() {
return asset_CheckMapper;
}
//
@Autowired
public void setAsset_CheckMapper(Asset_CheckMapper asset_CheckMapper) {
this.asset_CheckMapper = asset_CheckMapper;
}
public Asset_Check getAssetCheckById(String id) {
// TODO Auto-generated method stub
return asset_CheckMapper.selectByPrimaryKey(id);
}
}
7链瓦、測(cè)試拆魏,因?yàn)槭?strong>Maven項(xiàng)目, 在src/test/java目錄下建立包名和測(cè)試類,通過junit測(cè)試
package mybatis.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
public class TestMyBatis {
@Test
public void test1(){
ApplicationContext ac=
new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
AssetCheckService bean = (AssetCheckService) ac.getBean("AssetCheckService");
//這個(gè)bean就是實(shí)現(xiàn)類中的那個(gè)service注解
Asset_Check assetCheckById = bean.getAssetCheckById("1");
System.out.println(assetCheckById.getAcName());
}
}
- 打印如下
8慈俯、設(shè)想下渤刃,測(cè)試類中有一個(gè)測(cè)試方法我們可以這樣寫, 如果有100個(gè)測(cè)試方法贴膘,那么這個(gè)速度難以忍受卖子,因?yàn)閟pring在初始化的時(shí)候,即耗資源又耗時(shí)間刑峡。那么我們可以這樣寫
package mybatis.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
public class TestMyBatis {
private ApplicationContext ac;
AssetCheckService bean;
@Before
public void before(){
ac=new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
bean = (AssetCheckService) ac.getBean("AssetCheckService");
}
@Test
public void test1(){
Asset_Check assetCheckById = bean.getAssetCheckById("1");
System.out.println(assetCheckById.getAcName());
}
}
@Before的作用:在當(dāng)前這個(gè)類所有測(cè)試方法之前去執(zhí)行的這么一個(gè)方法洋闽。將所有初始化的東西放到這個(gè)放到這個(gè)方法之內(nèi)。那么只會(huì)執(zhí)行一次
到此突梦,說明spring和mybatis整合成功
接下里再看一種測(cè)試方法:添加spring-test的jar包依賴
package mybatis.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml",
"classpath:spring-mybatis.xml" })
public class TestMyBatis {
private AssetCheckService assetCheckService;
public AssetCheckService getAssetCheckService() {
return assetCheckService;
}
@Autowired
public void setAssetCheckService(AssetCheckService assetCheckService) {
this.assetCheckService = assetCheckService;
}
// private ApplicationContext ac;
// AssetCheckService bean;
// @Before
// public void before(){
//
// ac=new ClassPathXmlApplicationContext(new
// String[]{"spring.xml","spring-mybatis.xml"});
// bean = (AssetCheckService) ac.getBean("AssetCheckService");
// }
//
@Test
public void test1() {
Asset_Check assetCheckById = assetCheckService.getAssetCheckById("1");
System.out.println(assetCheckById.getAcName());
}
}
ok
9诫舅、 導(dǎo)入springMVC的jar包,搜索spring-webmvc
- springMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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">
<!-- 自動(dòng)掃描controller包下的所有類宫患,使其認(rèn)為spring mvc的控制器 -->
<context:component-scan base-package="dc_zaichi.controller" />
<!-- 避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動(dòng)Spring MVC的注解功能刊懈,完成請(qǐng)求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉(zhuǎn)換器 -->
</list>
</property>
</bean>
<!-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上傳文件大小限制為31M娃闲,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
</beans>
- 接下來虚汛,我們建立一個(gè)包,和類如下圖
- 建立Controller類如下圖所示
package dc_zaichi.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
@Controller
// 加入到springmvc的上下文當(dāng)中
@RequestMapping("/assetCheckController")
// 這個(gè)東西就是訪問地址的一個(gè)配置
public class AssetCheckController {
// 比如說執(zhí)行 http://127.0.0.1:8080/mybatis/assetCheckController.do 訪問此控制器
private AssetCheckService assetCheckService;
public AssetCheckService getAssetCheckService() {
return assetCheckService;
}
@Autowired
public void setAssetCheckService(AssetCheckService assetCheckService) {
this.assetCheckService = assetCheckService;
}
@RequestMapping("/showAssetCheck")
public String showAssetCheck(String id, HttpServletRequest request) {
// http://localhost:8080/mybatis/assetCheckController.do?id=1
// 后臺(tái)接的時(shí)候畜吊,接到這個(gè)id,并且去數(shù)據(jù)庫(kù)查找這個(gè)資產(chǎn)泽疆,也就是AssetCheckController訪問數(shù)據(jù)庫(kù),不能直接
// 訪問玲献,得通過業(yè)務(wù)邏輯service
// 訪問此控制器里的一個(gè)方法
// http://127.0.0.1:8080/mybatis/assetCheckController.showAssetCheck.do
Asset_Check assetCheckById = assetCheckService.getAssetCheckById(id);
System.out.println(assetCheckById.getAcName());
// 把a(bǔ)ssetCheckById存到Request中
request.setAttribute("Asset_Check", assetCheckById);
return "showAssetCheck";// 返回一個(gè)jsp,不用howAssetCheck.jsp,已經(jīng)在spring-mvc中配置好
}
}
如何啟動(dòng)SpringMVC,修改web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<display-name>mybatis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>
<filter>
<description>字符集過濾器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集編碼</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>spring監(jiān)聽器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring內(nèi)存溢出監(jiān)聽器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring mvc servlet -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置session超時(shí)時(shí)間殉疼,單位分鐘 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
10、頁(yè)面
11捌年、ok