1. 程式人生 > >SSM+Maven專案有關配置檔案說明

SSM+Maven專案有關配置檔案說明

SSM專案配置檔案說明
Spring的配置檔案:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- 防止二次掃描控制器 -->
<context:component-scan base-package="com.xc.ssm">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:exclude-filter type="annotation" expression="org.springframework.web.method.ControllerAdviceBean"/>
</context:component-scan>

<!-- Spring的配置檔案,這裡主要是配置業務邏輯有關的 -->
<!-- 如資料來源,事務控制... -->

<!-- 引入資料庫屬性檔案 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<!-- 配置資料庫連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.user}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="driverClass" value="${jdbc.driver}"></property>
	<property name="jdbcUrl" value="${jdbc.url}"></property>
	<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

<!-- 配置Spring和Mybatis的整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 指定連線池 -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 指定mybatis-config.xml 全域性配置檔案的位置 -->
	<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	<!-- 指定XXMapper.xml 檔案的位置 -->
	<property name="mapperLocations" value="classpath:com/xc/ssm/mapper/*.xml"></property>
</bean>

<!-- 配置掃描器:將mybatis介面的實現加入到IOC容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 掃描所有的Mapper介面或DAO介面的實現,加入到IOC容器中 -->
	<property name="basePackage" value="com.xc.ssm.mapper"></property>
</bean>
<!-- 或者使用 -->
<!-- <mybatis-spring:scan base-package="com.xc.ssm.mapper"></mybatis-spring> -->

<!-- 配置一個可以批量操作的SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
	<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>

<!-- 事務控制的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 控制資料來源 -->
	<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 開啟基於註解的事務,使用XML配置形式的事務(必要的主要的都是使用配置方式) -->
<aop:config>
	<!-- 配置切入點表示式 -->
	<aop:pointcut expression="execution(* com.xc.ssm.service..*(..))" id="txPoint"/>
	<!-- 配置事務增強 -->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

<!-- 配置事務增強,事務如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<!-- 所有方法都是事務方法 -->
		<tx:method name="*"/>
		<!-- 以get開始的所有方法 -->
		<tx:method name="get" read-only="true"/>
	</tx:attributes>
</tx:advice>

<!-- Spring配置檔案的核心點(資料來源、與mybatis的整合,事務控制) -->

SpringMVC的配置檔案:springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- SpringMVC 配置檔案:包含網站跳轉邏輯的控制,配置 -->

<!-- 禁用掉預設的過濾原則:use-default-filters="false":只掃描指定包下面的Controller -->
<context:component-scan base-package="com.xc.ssm" use-default-filters="false">
	<!-- 只掃描控制器 -->
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="annotation" expression="org.springframework.web.method.ControllerAdviceBean"/>
</context:component-scan>

<!-- 配置檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

<!-- 兩個標準配置 -->
<!-- 將springMVC不能處理的請求交給tomcat -->
<mvc:default-servlet-handler/>
<!-- 能支援SPringMVC更高階一些的功能,如JSR303校驗,快捷的ajax...對映動態請求 -->
<mvc:annotation-driven></mvc:annotation-driven>

Mybatis的全域性配置檔案:mybatis-config.xml

資料庫屬性檔案:dbconfig.properties
jdbc.user=root
jdbc.password=lixc0426
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

web.xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>

<welcome-file-list>
	<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 1.啟動Spring的容器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 2.配置SpringMVC 前端控制器,攔截所有請求 -->
<!-- The front controller of this Spring Web application, responsible for 
	handling all application requests -->
<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:springmvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
	<servlet-name>springDispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 3.字元編碼過濾器,一定要放在所有過濾器之前 。CharacterEncodingFilter-->
<filter>
	<filter-name>CharacterEncodingFilter</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>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 4. 使用REST風格的URL:將普通的post請求轉為指定的put或delete請求。
HiddenHttpMethodFilter HttpPutFormContentFilter-->
<filter>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
	<filter-name>HttpPutFormContentFilter</filter-name>
	<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>HttpPutFormContentFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

pom.xml配置檔案

4.0.0
com.xc.ssm
ressm
0.0.1-SNAPSHOT
war
ressm

<properties>
	<spring-version>5.1.1.RELEASE</spring-version>
</properties>

<dependencies>

	<!-- 引入專案依賴的jar包 -->

	<!-- spring-webmvc -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring-version}</version>
	</dependency>

	<!-- springmvc-jdbc:事務 -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${spring-version}</version>
	</dependency>

	<!-- spring-test:測試元件 -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>${spring-version}</version>
		<scope>test</scope>
	</dependency>

	<!-- spring-aspects:面向切面程式設計 -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
		<version>${spring-version}</version>
	</dependency>

	<!-- 引入mybatis:資料庫操作 -->
	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.6</version>
	</dependency>

	<!-- 引入整合spring整合mybatis的適配包——mybatis-spring -->
	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.3.1</version>
	</dependency>

	<!-- 引入資料庫連線池和連線驅動 -->
	<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
	<dependency>
		<groupId>com.mchange</groupId>
		<artifactId>c3p0</artifactId>
		<version>0.9.5.2</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.38</version>
	</dependency>

	<!-- 引入mybatis的分頁外掛PageHelper -->
	<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
	<dependency>
		<groupId>com.github.pagehelper</groupId>
		<artifactId>pagehelper</artifactId>
		<version>5.1.2</version>
	</dependency>

	<!-- 其他:jstl/junit/servlet-api -->
	<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
	<!-- https://mvnrepository.com/artifact/jstl/jstl -->
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/junit/junit -->
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
	<dependency> 
		<groupId>javax.servlet</groupId>
	 	<artifactId>javax.servlet-api</artifactId> 
		<version>3.1.0</version>
		 <scope>provided</scope> 
	</dependency>

	<!-- <dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency> -->

	<!-- 返回Json字串的支援 -->
	<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
		<version>2.9.6</version>
	</dependency>

	<!-- JSR303資料校驗支援 ,tomcat7及以上的伺服器; tomcat7以下的伺服器,el表示式,額外給伺服器的lib包中替換新的標註el -->
	<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
	<dependency>
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>6.0.12.Final</version>
	</dependency>

	<!-- MBG逆向工程生成Bean -->
	<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
	<dependency>
		<groupId>org.mybatis.generator</groupId>
		<artifactId>mybatis-generator-core</artifactId>
		<version>1.3.5</version>
	</dependency>


</dependencies>