mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--打印日志可以看執行的sql語句--> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> </configuration>
spring.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" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--第一步,掃描service--> <context:component-scan base-package="com.guangming.service.impl"></context:component-scan> <!--第二步,加載jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--第三步,創建dbcp數據源連接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--第四步,創建mybatis的工廠類對象--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定數據源--> <property name="dataSource" ref="dataSource"></property> <!--加載mybatis的映射文件 在value中可以使用*號通配符--> <property name="mapperLocations" value="classpath:com/guangming/dao/*.xml"></property> <!--加載mybatis中的配置文件--> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> <!--第五步,在spring 的工廠中生成dao接口的實現類對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要掃描哪個包下面所有的dao接口--> <property name="basePackage" value="com.guangming.dao"></property> </bean> <!--第六步,創建spring的事物管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--第七步,聲明以注解的方式配置聲明式事物--> <tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven> </beans>