spring-Dao.xml:
<?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.关联数据库配置文件 约束context(上下文)主要实现外部文件的关联-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2.连接池
与之前的myBatis-spring整合不一样 之前用的是spring框架的数据库驱动
这里使用第三方 写好的连接池 其共同点都是导入依赖 利用别人的写好的类
连接池种类:
dbcp:半自动化操作,不能自动连接
c3p0:自动化操作(自动化的加载文件,并且可以自动设置到对象中!)
druid;
hikari;
-->
<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}"/>
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000"/>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3.配置SQL sessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="datasource"/>
<!--配置MyBatis全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:MyBatis-config.xml"/>
</bean>
<!--4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中-->
<!--这一步的作用在于不用写该Dao接口包的实现类--><!--当然也可以使用之前整合myBatis-spring的bean注入方法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入SQL sessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--给出需要扫描Dao接口包-->
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
spring-service.xml:
<?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">
<!--扫描service相关的bean-->
<context:component-scan base-package="com.service"/>
<!--BookserviceImpl注入到IOC容器中-->
<bean id="BookServiceImpl" class="com.service.BookServiceImpl">
<property name="BookMapper" ref="bookMapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="datasource" />
</bean>
</beans>
其中标签里含有的 ref 可以关联另一个spring下的.xml文件的bean
再比如: <property name="BookMapper" ref="bookMapper"/>
两个.xml文件并没用bean——bookMapper 但是com.service.BookServiceImpl该类下有 set方法:
public class BookServiceImpl implements BookService{
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper){
this.bookMapper=bookMapper;
}
}