ssm集合框架配置文件的编写(spring)

<?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"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
">
    <!-- 启用注解 -->
    <context:annotation-config />
    <!-- 扫描指定包中的所有的对象 -->
    <context:component-scan base-package="com.etc.service" />

    <!-- 这个Bean用于读取数据库连接相关的属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:config/jdbc.properties"/>
    </bean>
    <!-- 配置数据源及连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
       <!-- ${driver}取属性文件中: driver键的值  -->
       <property name="driverClassName" value="${driver}"/>
       <property name="url" value="${url}"/>
       <property name="username" value="${username}"/>
       <property name="password" value="${password}"/>
       <property name="initialSize" value="${initialSize}"/>
       <property name="maxActive" value="${maxActive}"/>
       <property name="maxIdle" value="${maxIdle}"/>
       <property name="minIdle" value="${minIdle}"/>       
    </bean>
   
    <!-- 根据数据源dataSource,配置MyBatis访问数据库,加载映射文件。 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 数据源对象 -->
       <property name="dataSource" ref="dataSource"/>
       
       <!-- 加载所有的映射文件  *表示通配符-->
       <property name="mapperLocations" value="classpath:com/etc/mapping/*.xml"/>
       
       <!-- 如果有MyBatis的主配置文件,则加载主配置文件,否则就要加载实体类的别名 -->
       <property name="configLocation" value="classpath:config/mybatis-config.xml"/>
       
       <!--如果没有mybatis的主配置文件 扫描entity层中所有的实体,给实体对象取别名 -->
       <!-- <property name="typeAliasesPackage" value="com.etc.entity"/> -->
    </bean>
    
    <!-- 启用事务注解 transaction-manager="transactionManager"引用事务管理Bean -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置事务管理Bean -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 事务处理需要依赖数据源对象 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架是由SpringSpringMVC和MyBatis三个框架组合而成,其中Spring和MyBatis需要进行XML配置。下面是它们的配置文件: 1. Spring配置文件 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置MyBatis SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.example.model"/> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启Spring注解扫描 --> <context:component-scan base-package="com.example"/> </beans> ``` 2. MyBatis配置文件 sqlMapConfig.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="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> </settings> <!-- 配置MyBatis Mapper映射文件 --> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration> ``` 其中,mapper/UserMapper.xml是自己编写的Mapper映射文件,用于定义SQL语句和映射关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值