1. 程式人生 > 其它 >ssm整合:Spring層

ssm整合:Spring層

1.配置Spring整合Mybatis,我們這裡的資料來源使用c3p0連線池

2.編寫Spring整合Mybatis的相關配置檔案: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:aop="http://www.springframework.org/schema/aop"
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">

<!-- 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="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要掃描的包-->
<property name="basePackage" value="com.luo.dao"/>
</bean>
</beans>

3.spring整合service層

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:aop="http://www.springframework.org/schema/aop"
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">

<!-- 1.掃描service下的包-->
<context:component-scan base-package="com.luo.service"/>
<!-- 2.將我們所有的業務類,注入到Spring中,可以通過配置,或者註解實現-->
<bean id="BookServiceImpl" class="com.luo.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>

 applicationContext.xml: