1.引言:spring层主要做两件事情
1.1创建一个spring-dao.xml,关联数据库配置文件(context:property-placeholder location="xxx"),配置数据源,SqlSessionFactory(注入数据源,绑定mybatis核心配置文件mybatis-config.xml),扫描dao接口(注入sqlSessionFactory,给出需要扫描的dao包)
1.2创建一个spring-service.xml,配置扫描service层下的包,将所有的业务类注入到spring,声明式事务
2.步骤:
2.1在resources文件夹下新建一个spring.xml文件,命名为spring-dao.xml,右键resources-》new-》file-》输入文件名spring-dao.xml,点击ok。(这里需要注意的是关联的数据库配置文件以及sqlsessionFactory绑定mybatis配置文件要对,通过MapperScannerConfigurer扫描的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"> <!--关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!--配置连接池--> <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> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.xiaoma1.dao"/> </bean> </beans>
2.2同样的方法创建一个名为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层下的包--> <context:component-scan base-package="com.xiaoma1.service"/> <!--将所有的业务类注入到spring--> <bean id="bookServiceImpl" class="com.xiaoma1.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--声明式事务--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> </beans>
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!