SSM学习-3.添加SpringMVC的配置

1.在项目中新建springMVC配置文件applicationContext.xml

  

2.基本配置

  [1].配置数据源

  [2].配置和mybatis的整合

  [3].配置mybatis扫描器

  [4].事务控制配置

  applicationContext.xml具体配置如下:

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
     <!-- spring的配置文件,这里主要配置和业务逻辑有关的 -->
     <context:property-placeholder  location="classpath:dbconfig.properties"/>
     <!-- 配置数据源 -->
     <bean id="poolerDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name="jdbcUrl" value="${jdbc.jdbcURL}"></property>
         <property name="driverClass" value="${jdbc.driverClass}"></property>
         <property name="user" value="${jdbc.user}"></property>
         <property name="password" value="${jdbc.password}"></property>
     </bean>
     
     <!-- 配置和mybatis的整合 -->
     <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <!-- 指定mybatis全局配置文件的位置 -->
         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
         <!-- 指定数据源 -->
         <property name="dataSource" ref="poolerDataSource"></property>
         <!-- 指定mybatis mapper文件的位置 -->
         <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
     </bean>
     
     <!-- 配置扫描器,将mybatis接口的实现加入到Ioc容器 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <!-- 扫描所有dao接口的实现,加入到Ioc容器中 -->
         <property name="basePackage" value="com.atguigu.crud.dao"></property>
     </bean>
     
     <!-- 事务控制的配置 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <!-- 控制数据源 -->
         <property name="dataSource" ref="poolerDataSource"></property>    
     </bean>
     
     <!-- 开启基于注解的事务,使用XML配置形式的事务 -->
     <aop:config>
         <!-- 切入点表达式 -->
         <aop:pointcut expression="execution(* com.atguigu.crud.service..*(..))" id="txPoint"/>
         <!-- 配置事务增强 -->
         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
     </aop:config>
     
     <!-- 配置事务增强,事务如何切入 -->
     <tx:advice id="txAdvice">
         <tx:attributes>
             <!-- 所有方法都是事务方法 -->
             <tx:method name="*"/>
             <!-- 以get开头的方法都是查询 -->
             <tx:method name="get*" read-only="true"/>
         </tx:attributes>
     </tx:advice>
     
</beans>

 

   dbconfig.properties的配置如下:

jdbc.jdbcURL=jdbc:mysql://localhost:mysql端口号/数据库名
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=用户名
jdbc.password=密码

   applicationContext.xml的配置如下:

<?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>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.atguigu.crud.bean"/>
    </typeAliases>
</configuration>

 

posted @ 2017-09-06 17:56  学而时习之,不亦说乎?  阅读(322)  评论(0编辑  收藏  举报