SSM項目中配置問題

建立Spring+SpringMVC+Mybatis項目時候,首先須要配置一些對應的配置文件,下面簡要介紹下這些配置文件所需的最簡單需求。java

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>

2 springmvc-servlet.xml

此配置文件配置了SpringMVC框架所需的內容。帶*的部分均爲本身填寫內容mysql

  1. <?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所需的配置文件。此處注意:帶*部分是項目名稱,不具體到包。web

  1. <?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

  1. <?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等內容。spring

相關文章
相關標籤/搜索