Spring-Mybatis整合
database.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?useUncode=true&characterEncoding=utf-8
uname=root
password=ok
applicationContext配置文件
<?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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<!-- <context:property-override properties-ref="database.properties" /> -->
<!-- IOC注解支持 -->
<context:component-scan base-package="dao,service" />
<!-- 方法1.导入properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties" />
</bean>
<!-- 方法2.导入properties文件 需要修改${username}为${uname} 不能和name属性值名称相同 -->
<context:property-placeholder location="classpath:database.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUncode=true&characterEncoding=utf-8" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- 创建SqlSessionFactory工厂对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载MyBatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="pojo" />
<!-- 配置映射 接口和xml文件同级一下可省略-->
<property name="mapperLocations">
<list>
<value>classpath:dao/*/*.xml</value>
</list>
</property>
</bean>
<!-- 自动实现 Mapper 接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao" />
</bean>
<!-- 事务配置 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 自动事务支持 -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- 把上面事务另存到applicationtx.xml文件 再使用import导入-->
<!-- 导入其他配置文件 -->
<import resource="applicationtx.xml" />
</beans>