其他分享
首页 > 其他分享> > spring核心配置文件

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"
       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">
    <!--注解扫描-->
    <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>
</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包的全限定名

标签:配置文件,spring,配置,值为,核心,mybatis,id,属性
来源: https://www.cnblogs.com/qkq505/p/16452387.html