Eclipse中建立Maven,點擊File,選擇new一個Maven項目html
勾選Create a simple ...java
以下,補充完整,mysql
Group Id 組織名web
Artifact Id 項目名稱spring
Version 版本號sql
packaging 打包方式 , 當項目爲Java項目時,打成Jar包;當項目爲web項目時,打成war包;當項目爲其餘...數據庫
項目建立完成,在src/main/webapp/WEB-INF 下 ,新建web.xmlexpress
在pom.xml中 Add Denpendency(添加jar包),從本地倉庫中獲取須要的jar包(圖中灰色是由於已經導入)apache
Maven 中SSM整合spring-mvc
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_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
<!-- 定義一個過濾器,解決項目中編碼問題 --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 設置編碼參數 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param>
<!--6其餘地方關於編碼的設置通通失效--> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 監聽器,主要監聽spring主配置文件,web文件最初開始執行 --> <context-param>
<!--類org.springframework.web.context.context.ContextLoaderListener中的成員contextConfigLocation,存儲須要監聽的文件路徑--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:mybatis-spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- springmvc 在filter執行完畢後,執行servlet--> <servlet> <servlet-name>springDispatcherServlet</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>
<!--指在tomcat執行完畢後,servlet纔開始執行--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在「src/main/resource」下(這也是web.xml中classpath默認的路徑), 新建 spring-mvc.xml,spring-mybatis.xml,srping.xml文件
spring-mvc.xml以下
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!-- 啓用spring mvc註解 --> <context:annotation-config></context:annotation-config> <!-- 設置掃描 --> <context:component-scan base-package="com.isoft.controller"/> <!-- 設置映射器、適配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 對專項頁面的路徑解析爲:prefix前綴 suffix後綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"></bean> </beans>
org.springframework.web.context.ContextLoaderListener
spring.xml
<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 掃描 --> <context:component-scan base-package="com.isoft"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
spring-mabtis.xml
<?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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/Sufeng?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> <!-- 指定鏈接數據庫鏈接池的最小鏈接數 --> <property name="minPoolSize" value="10"></property> <!-- 指定鏈接數據庫鏈接池的最大鏈接數 --> <property name="maxPoolSize" value="20"></property> <!--初始化時獲取三個鏈接,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize" value="15"></property> <!-- 指定鏈接數據庫鏈接池的鏈接的最大空閒時間 --> <property name="maxIdleTime" value="120"></property> <!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 --> <property name="acquireIncrement" value="5"></property> <property name="maxStatements" value="100"></property> <!--每60秒檢查全部鏈接池中的空閒鏈接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60"></property> <property name="automaticTestTable" value="c3p0testtable"></property> </bean> <!-- Mybatis文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" />
<!--映射文件路徑-->
<!-- <properties name="mapperLocations" value="com/isoft/mapping/*.xml">--> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isoft.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 事務管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="select*" read-only="true" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" propagation="REQUIRED" /> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="start*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="finish*" propagation="REQUIRED" isolation="READ_COMMITTED" /> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> </beans>
mybatis-config文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局別名設置,在映射文件中只需寫別名,而沒必要寫出整個類路徑 --> <typeAliases> <typeAlias type="com.isoft.model.Person" alias="Person"/> </typeAliases> <mappers> <mapper resource="com/isoft/mapping/PersonMapper.xml" /> </mappers> </configuration>
log4j.properties 設置日誌
log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender