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:tx="http://www.springframework.org/schema/tx"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    注解扫描-->
    <context:component-scan base-package="com.gerigeorge.mapper,com.gerigeorge.service"/>
    <!--    durid连接池-->
    <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driver}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>
    <!--    配置sqlSessionFactory-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ds"></property>
        <property name="typeAliasesPackage" value="com.gerigeorge.entity"></property>
        <property name="mapperLocations" value="classpath:com/gerigeorge/mapper/*Mapper.xml"></property>
    </bean>
    <!--    配置dao 自动注入Mapper且自动生成id(接口名首字母小写)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="factory"></property>
        <property name="basePackage" value="com.gerigeorge.mapper"></property>
    </bean>
<!--    配置事务控制器-->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"></property>
    </bean>
<!--    事务注解扫描-->
    <tx:annotation-driven transaction-manager="tm" />
</beans>

 

说明:

spring上述配置只适配到spring整合mybatis

1.注解扫描:开启 base-package 所含包下的注释扫描: <context:component-scan base-package="com.gerigeorge.mapper,com.gerigeorge.service"/>

2.配置数据库连接池 ( id:bean的唯一标识 class:连接池类的全限定名)

  连接数据库的属性:driverClassName,url,username,password

3.配置sqlSessionFactory信息( id:bean的唯一标识 class:org.mybatis.spring.SqlSessionFactoryBean

  配置属性 dataSource  ref值为配置好的datasource的id

  配置属性 mapperLocation 值为 Mapper文件所在的类路径 可以用/*Mapper.xml进行通配

  配置属性 typeAliasesPackage 值为 实体所在包的全限定名

4.配置dao层自动注入Mapper(class:org.mybatis.spring.mapper.MapperScannerConfigurer

  配置属性 sqlSessionFactoryBeanName 值为sqlSessionFactory的id

  配置属性 basePackage 值为mapper包的全限定名

5.配置事务控制器(id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager")

  配置属性 dataSource ref值为数据库连接池id

6.开启事务注解扫描:<tx:annotation-driven transaction-manager="tm" />

posted @ 2022-07-06 20:25  grigeorge  阅读(368)  评论(0编辑  收藏  举报