MyBatis-Spring整合文件配置及三种数据源的配置

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!--配置数据源 内部配置 -->
<!--jar位置: Referenced Libraries/commons-dbcp.jar -->
<context:component-scan base-package="*"></context:component-scan>

<bean id="data" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>

<!--jar位置:Spring 3.1.1 Libraries --><!--第二种配置数据源的方式 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:data.properties"></property>
</bean>
<bean id="data2" 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>

<!--jndi方式配置数据源 --><!--第三种配置数据源的方式 -->
<!--使用JNDI的方式配置数据源,前提是必须在应用服务器上配置好数据源。以Tomcat为例:1、配置数据源需要把数据库驱动文件放到
Tomcat的lib目录下,2、修改Tomcat的conf目录下的cintext.xml文件,配置数据源代码如下
<Context>
<Resource name="abc" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root"
password="" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/test(数据库路径)?useUnicode=true&amp;characterEncoding=utf-8"
/>
</Context>

-->
<!-- 在spring中配置:通过JNDI配置数据源      jar位置:Spring3.1.1 Libraries/org...context-3.1.1.RELEASE.jar/
<bean id="data3" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/abc"></property>
</bean>
-->

 

<!--配置 SqlSessionFactoryBean -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="data"></property>

<property name="mapperLocations" >
<list>
<value>classpath:dao/**/*.xml</value>
</list>
</property>

</bean>

 

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="*"></property>
</bean>

<!-- 未使用到声明事务配置  -->
</beans>

posted on 2017-10-11 16:16  艾子琼  阅读(423)  评论(0编辑  收藏  举报

导航