<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.hfxt.service"/>
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driver}"></property>
</bean>
<!--创建SqlSessionFactory MyBatis会话工厂对象-->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--引用mybatis的配置文件-->
<!--<property name="configLocation">
<value>classpath:mybatis-config.xml</value>
</property>-->
<!--解析dao接口的xml文件-->
<property name="mapperLocations" value="classpath:com/hfxt/dao/*Mapper.xml"></property>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.hfxt.entity"/>
</bean>
<!--定义dao组件-->
<!--<bean id="usesDao" class="com.hfxt.dao.impl.UsesDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>-->
<!--MapperScannerConfigurer用来映射dao层实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.hfxt.dao"/>
</bean>
<!--创建事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
database.properties文件内容:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/film?characterEncoding=utf-8
user=root
password=root