1.引入外部的properties属性文件
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd">
2. 使用import引入分配置文件
<!--将分配置文件引进来-->
<import resource="classpath:applicationContext-dao.xml"/>
3.applicationContext.xml中整合mybatis
第一步:导入spring整合mybatis的依赖
<dependencies><!--mybatis和mysql依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--spring核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><!--spring整合mybatis框架--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!--druid连接池依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.6</version></dependency></dependencies>
第二步:在applicationContext.xml中整合mybatis
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 一 ::配置studentService对象--><bean id="studentService" class="com.itheima.service.impl.StudentServiceImpl"><property name="mapper" ref="studentMapper"/></bean><!-- 二 ::配置studentMapper对象【通过mybatis动态代理】--><!--配置spring整合mybatis的bean(三个)--><!--(配置3个Bean:DruidDataSource、SqlSessionFactoryBean、MapperScannerConfigurer)--><!--1 配置DruidDataSource连接池--><!--引入外部属性文件--><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--2 配置SqlSessionFactoryBean对象,作用1:封装SqlSessionFactoryBuilder,SqlSessionFactory对象的创建作用2:加载mybatis需要的环境信息--><!--因为不需要这个类的bean对象 则可以不添加唯一标识--><bean class="org.mybatis.spring.SqlSessionFactoryBean"><!--1 必须要注入连接池对象【必须】--><property name="dataSource" ref="dataSource"/><!--2 加载MybatisConfig.xml核心配置文件,如果核心配置文件中的内容都被抽取了,那么就可以不用加载【可选】--><property name="configLocation" value="classpath:MybatisConfig.xml"/><!--3 配置别名,如果是使用注解配置SQL语句,可以不用配置别名【可选】--><property name="typeAliasesPackage" value="com.itheima.bean"/><!--4 加载映射配置文件,如果映射配置文件和mapper接口在同一个包下,并且同名,那么会自动加载【可选】--><property name="mapperLocations" value="classpath:StudentMapper.xml"/></bean><!--3 配置MapperScannerConfigurer对象,作用1:扫mapper接口所在的包,创建接口的代理对象,添加到spring容器中作用2:如果映射配置文件和mapper接口在同一个包下,并且同名,那么会自动加载对应的映射配置文件--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--扫mapper接口所在的包--><property name="basePackage" value="com.itheima.mapper"/></bean>
</beans>