<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- c3p0配置信息 代替MyBatis的配置文件config.xml--> <bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 在spring容器中创建myBatis的核心对象SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置SqlSessionFactory信息,这个是必须属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 加载myBatis的配置文件 <property name="configLocation" value="classpath:config.xml"></property> --> <!-- 加载mapper.xml的文件路径 --> <property name="mapperLocations" value="com/tz/mapper/*.xml"></property> </bean> <!-- Spring 产生mybatis最终需要的动态的mapper对象,EmployeeMapper 1.在dao接口加上实现类,继承sqlDaoSuppertSqlSession对象,实现sqlSession对象 2.不需要自己写dao实现类,框架生成dao实现类 3.基于第二种方式,如果有多个dao,那么就要配置多个bean,太麻烦了,所以直接全部配置好 --> <!-- 通过myBatis给定的工厂类来创建我们的对应实现类 MapperScannerConfigurer批量生产多个实现类对象 --> <!-- 批量生产dao对象在spring容器中id默认值为接口名字首字母小写 --> <!-- 注意在这里没有必要创建多个sqlSessionFactory对象每个实现类一个对象,我们只需一个sqlSessionFactory对象来实现我们的dao对象 这里只需要加sqlSessionFactoryBeanName --> <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name=" " value="sqlSessionFactory"></property> <!-- 批量生产多个实现类对象,如果有多个可以写 value="com.tz.dao,com.tz.mappers" --> <property name="basePackage" value="com.tz.dao "></property> </bean> <!-- MapperFactoryBean就是生成我们自己的接口实现类对象 只需要用mapperInterface指定name是哪个接口就会创建哪个接口的字节码文件来实现对象, --> <!-- <bean id="employeeDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 通过MapperFactoryBean创建出来value所对应的接口实现类对象 <property name="mapperInterface" value="com.tz.dao.EmployeeDao"></property> 实现的EmployeeDao也需要同数据库进行关联,所以也是需要创建sqlSesionFactory对象 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!-- <bean id="employeeDao" class="com.tz.dao.Impl.EmployeeDaoImpl"> dao访问数据库需要sqlSessionFactory,需要进行关联 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <bean id="employeeService" class="com.tz.service.Impl.EmployeeServiceImpl"> <property name="dao" ref="employeeDao"></property> </bean> </beans>