spring+springmvc+mybatis的配置文件

来源:互联网 发布:centos 7.3安装nginx 编辑:程序博客网 时间:2024/06/05 02:02

web.xml文件配置ssm
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <welcome-file-list>    <welcome-file>login.jsp</welcome-file>  </welcome-file-list>    <!-- 1.监听,数据库配置spring-mybatis.xml -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:spring-mybatis.xml</param-value>  </context-param>  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 2.spring-mvc。xml,web容器包含spring -->  <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:spring-mvc.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>  <!-- 3.字符编码,utf-8 -->  <filter>  <filter-name>encodingFilter</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>  </filter>  <filter-mapping>  <filter-name>encodingFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>  </web-app>
spring-mvc.xml配置源码
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/contexthttp://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    ">    <!-- 扫描包 -->    <context:component-scan base-package="com.aaa.controller"></context:component-scan>    <!-- 注解 -->    <mvc:annotation-driven/>    <!--视图解析器 -->   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >      <property name="prefix" value="/"></property>      <property name="suffix" value=".jsp"></property>   </bean></beans>
spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
   <!-- 扫描包 -->
  <context:component-scan base-package="com"></context:component-scan>
  <!-- 配置数据源 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
     </bean>
     <!-- 3.sqlSessionFactory工厂,加上mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/><!-- classpath代表src -->
<property name="mapperLocations" value="classpath:com/aaa/mapper/*.xml"></property>
</bean>
    <!-- 4.扫描与*mapper.xml对应的接口包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.aaa.dao"></property>
</bean>

  <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 加载数据源 -->
      <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启事务扫描 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans>




阅读全文
1 0
原创粉丝点击