非原创,转自
https://blog.csdn.net/noaman_wgs/article/details/54287094
SSM项目中配置问题
创建Spring+SpringMVC+Mybatis项目时候,首先需要配置一些对应的配置文件,下面简要介绍下这些配置文件所需的最简单需求。
1 Mybatis.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:tx=
"http://www.springframework.org/schema/tx"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
<!-- 1. 数据源 : DriverManagerDataSource -->
-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
-
<property name="url" value="jdbc:mysql://localhost:3306/*"/>
-
<property name="username" value="*"/>
-
<property name="password" value="*"/>
-
</bean>
-
-
<!--
-
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
-
-->
-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource"/>
-
</bean>
-
<!--
-
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
-
-->
-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
-
<property name="basePackage" value="com.*.*.mapper" />
-
</bean>
-
-
<!-- 4. 事务管理 : DataSourceTransactionManager -->
-
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name="dataSource" ref="dataSource"/>
-
</bean>
-
-
<!-- 5. 使用声明式事务 -->
-
<tx:annotation-driven transaction-manager="txManager"/>
-
-
</beans>
第
1步连接数据库中带*的地方为自己填写内容,第3步带*的地方即value值为mapper接口所在的包名,自己填写。
2 springmvc-servlet.xml
此配置文件配置了SpringMVC框架所需的内容。带*的部分均为自己填写内容。
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:context=
"http://www.springframework.org/schema/context"
-
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
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/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
<!--开启注解-->
-
<mvc:annotation-driven />
-
<!-- 静态资源可直接访问 -->
-
<mvc:default-servlet-handler />
-
-
<!-- 扫描含有Controller注解所在的包 -->
-
<context:component-scan base-package="com.*.*.controller" />
-
-
<!-- jsp视图路径配置 -->
-
<bean id="jspViewResolver"
-
class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/jsp/" />
-
<property name="suffix" value=".jsp" />
-
</bean>
-
</beans>
3 applicationContext.xml
此为Spring所需的配置文件。此处注意:带*部分是项目名称,不具体到包。如:com.exe.ssm,(这是整个项目名,下面有很多包)
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:context=
"http://www.springframework.org/schema/context"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
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">
-
-
<!-- 自动加载,过滤掉带有@Controller注解的类 -->
-
<context:component-scan base-package="com.*.*">
-
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
-
</context:component-scan>
-
<!--导入需要的文件资源-->
-
<import resource="MyBatis.xml" />
-
-
</beans>
4 web.xml
web.xml中至少含有4个部分的内容,listener/context-param/servlet/servlet-mapping内容(我是指在SSM项目中所需要的这些内容)。
-
<?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"
-
version=
"3.0">
-
-
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
-
</listener>
-
-
<!-- Spring and Spring MVC -->
-
<context-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:applicationContext.xml
</param-value>
-
</context-param>
-
-
<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:springmvc-servlet.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>
-
-
-
-
</web-app>
listener必须有,缺少则会报错。之后也可以加入filter等内容。
web.xml的加载顺序是: <context-param>-> <listener> -> <filter> -> <servlet>
关于web.xml,可以详细看下这篇文章:【http://blog.csdn.net/believejava/article/details/43229361】