# ssm整合:Spring層 ###### tags: `ssm整合` ## 1.整合dao層 ![](https://i.imgur.com/su1Pki3.png) ```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 https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.關聯數據庫配置文件--> <!--可以取得數據庫的配置文件,知道帳號與密碼等等--> <context:property-placeholder location="classpath:database.properties"/> <!--2.連接池 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.sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--綁定Mybatis的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--配置dao接口掃描包,動態實現dao接口可以注入到Spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入sqlSessionFactory--> <property name="sqlSessionTemplateBeanName" value="sqlSessionFactory"/> <!--要掃描的dao包--> <property name="basePackage" value="com.kuang.dao"/> </bean> </beans> ``` ![](https://i.imgur.com/3FtRqbG.png) ## 2.整合Service層 ![](https://i.imgur.com/PdHbW7w.png) ```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 https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.掃描service下的包--> <context:component-scan base-package="com.kuang.service"/> <!--2.將我們的所有業務類,注入到Spring,可以透過配置,或者註解實現--> <bean id="BookServiceImpl" class="com.kuang.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--3.聲明式事務配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入數據源--> <property name="dataSource" ref="dataSource"/> </bean> <!--4.aop事務支持--> </beans> ``` ### 整合要點 要將三個整合用的文件,整合到一塊 因為這三個文件彼此間要有關係,才能進行整合 ![](https://i.imgur.com/qCbVCHK.png)