10.0、ssm整合-spring层
spring-dao.xml配置文件 / spring-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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--关联数据库连接-->
<context:property-placeholder location="classpath:database.properties" />
<!--连接池
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}" />
</bean>
<!--SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--自动扫描包,实现dao接口可以自动的注入到spring中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入selsessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要扫描的包-->
<property name="basePackage" value="com.hkl.dao"/>
</bean>
</beans>
spring-service.xml / spring-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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描Service包-->
<context:component-scan base-package="com.hkl.service" />
<!--将我们所有的ServiceImpl层注入进Spring中,这个过程可以用注解实现也可以用配置来实现-->
<bean id="UserAddServiceImpl" class="com.hkl.service.impl.UserAddServiceImpl">
<property name="userAddDao" ref="userAddDao"/>
</bean>
<!--开启声明式事务,保证数据操作的一致性-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<!--aop的事务支持-->
</beans>