SSM框架整合之Spring主配置文件的写法

1 . 添加相关jar包
2.搭建环境
3.编写applicationContext.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:context="http://www.springframework.org/schema/context";
    xmlns:aop="http://www.springframework.org/schema/aop";
    xmlns:tx="http://www.springframework.org/schema/tx";
    xmlns:mvc="http://www.springframework.org/schema/mvc";
    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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd";>
    <!-- 采用注解开发,开启注解功能  开启mvc注解-->
    <context:component-scan base-package="com.ruicai.ssm.*"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 1.数据连接池,首先得加载连接mysql的属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
           <property name="driverClass" value="${jdbc.driver}" />
           <property name="jdbcUrl" value="${jdbc.url}" />
           <property name="user" value="${jdbc.username}" />
           <property name="password" value="${jdbc.password}" />
    </bean>
    
    
    <!-- 2.配置mybatis的sqlSessionFactory
           得到sqlSessionFactory需要属性:typeAliasesPackage,DataSource,mapper.xml映射文件,此映射文件相当于接口的实现类
           所以需要sqlSessionFactory来生产SQLSession才能自动生成代理实现类
     -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <!-- 给指定的包取别名:包名用点,文件用/,且包不用加classpath,文件要加classpath -->
       <property name="typeAliasesPackage" value="com.ruicai.ssm.entity"/>
       
       <!-- 载入mapper.xml映射文件 -->
       <property name="mapperLocations" value="classpath:com/ruicai/ssm/mapper/*.xml"/>
    </bean>
    
    
    <!-- 3.扫描mapper包,由spring自动生成mapper代理,并注册到spring里 -->
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.ruicai.ssm.mapper"></property>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    
    <!-- 4.配置视图解析器,mvc转发路径 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/"/>
       <property name="suffix" value=".jsp"/>
    </bean>
    
    
    <!-- 5.配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值