1、创建配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild
jdbc.username=root
jdbc.password=123456
2、编写Spring核心配置文件【Spring-dao.xml】,整合dao层
<?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">
<!--1.关联db.properties ,这样的话数据库配置文件就可以通过spring来读取-->
<context:property-placeholder location="classpath:db.properties"/>
<!--2、连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1 把上面的dataSource引进来-->
<property name="dataSource" ref="dataSource"/>
<!--3.2 绑定Mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4.配置dao接口扫描包,动态实现Dao接口注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--4.1 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSeesionFactory"/>
<!--要扫描的dao包-->
<property name="basePackage" value="com.itheima.dao"/>
</bean>
</beans>
3、编写Spring核心配置文件【Spring-service.xml】,整合service层
<?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">
<!--指定要扫描的包,这个包下的注解就会生效 -->
<!--1.扫描service下的包-->
<context:component-scan base-package="com.itheima.servce"/>
<!--2.将我们的所有业务类,注入到spring,可以通过配置,或者注解实现-->
<bean id="BookServiceImpl" class="com.itheima.servce.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--3.声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--3.1注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--4.AOP事务支持-->
</beans>