Spring整合MyBatis的applicationContext.xml配置文件.使用了c3p0

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
		
	<!-- 加载自定配置文件(内部装载数据库登录信息) -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 配置数据源(连接池) -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${db.url}"></property>
		<property name="driverClass" value="${db.driver}"></property>
		<property name="user" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
	 </bean>
	<!-- 配置sqlSessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
		<property name="configLocation" value="SqlMapConfig.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置dao或者mapper,以下内容三选1 -->
        
         <!--原始Dao方式-->
	<bean id="userDao" class="cn.ty.ssm.dao.UserDaoImpl">
	        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<!-- Mapper方式 -->
	<bean id="bookMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="cn.ty.ssm.dao.BookMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
       <!-- 批量扫描Mapper方式 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
                <!--指定扫描整个包,多个包用英文","分隔符-->
                <property name="basePackage" value="cn.ty.ssm.dao"></property>
		<!-- 下面这种也可以
		<property name="basePackage" value="cn/ty/ssm/dao"></property> -->
		<!-- 此处是sqlSessionFactoryBeanName,与上面不同-->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>	
</beans>


Spring整合了Mybatis之后的applicationContext.xml.刚学了这两个,整理了一下配置文件.使用不同的连接池(数据源)只需要换不同的连接池实现类,各个连接池对于4大参数的命名可能不同,不过结构大体是相同的.

配置 datasource 四种方式,换不同的连接池(数据源)只用改<!-- 配置数据源(连接池) -->


附:db.properties中内容,这个文件放在classpath下

db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/bookstore
db.username = root
db.password =(数据库密码)

猜你喜欢

转载自blog.csdn.net/weixin_42072135/article/details/80707620