spring-mybatis整合配置文件(spring-mybatis.xml)

spring-mybatis整合主要有以下配置:

1.读取jdbc配置文件(jdbc.properties),文件中是jdbc连接数据库的参数。
               例如:mysql:(database_name处填写数据库名)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/database_name
username=root
password=root

oracle:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
user=system
pwd=root

 其中classpath指的是src/main/resources下的路径:

          

		<!-- 读取jdbc配置文件 -->
		<util:properties id="jdbc" location="classpath:conf/jdbc.properties"/>

2.配置数据库连接池,其中“jdbc”指的是读取jdbc配置文件时的id

注意:(1)property中name的值是在MyBatis的底层写好的(封装在BasicDataSource类中),不可更改(区分大小写)
                           常用的name值有:driverClassName(JDBC驱动类名)、
                                                             url(驱动用来连接数据库的地址)、
                                                             username(数据库用户名)、
                                                             password(数据库密码)、
                                                             maxActive(最大连接数量)、
                                                             initialSize(初始化连接数)
                                                             ……
                       (2)value中的值要和jdbc.properties文件中的key一致

		<!-- 配置数据库连接池 -->
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
			<property name="driverClassName" value="#{jdbc.driver}"/>
			<property name="url" value="#{jdbc.url}"/>
			<property name="username" value="#{jdbc.username}"/>
			<property name="password" value="#{jdbc.password}" />
			<property name="maxActive" value="#{jdbc.maxActive}"/>
		</bean>

3.配置MyBatis的SessionFactory,改配置让SessionFactory去指定路径下查找含有sql语句的xml文件(mapper)

		<!-- 配置MyBatis的SessionFactory -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"/>
			<property name="mapperLocations" value="classpath:mapper/*.xml"/>
		</bean>

4.配置Mapper接口组件扫描,将持久层的dao接口和MyBatis绑定

		<!-- Mapper接口组件扫描 -->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="cn.tedu.note.dao"/>
		</bean>

spring-mybatis.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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	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
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
		
		<!-- 配置spring-mybatis.xml -->
		<!-- 读取jdbc配置文件 -->
		<util:properties id="jdbc" location="classpath:conf/jdbc.properties"/>
		
		<!-- 配置数据库连接池 -->
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
			<property name="driverClassName" value="#{jdbc.driver}"/>
			<property name="url" value="#{jdbc.url}"/>
			<property name="username" value="#{jdbc.username}"/>
			<property name="password" value="#{jdbc.password}" />
		</bean>
		
		<!-- 配置MyBatis的SessionFactory -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"/>
			<property name="mapperLocations" value="classpath:mapper/*.xml"/>
		</bean>
		
		<!-- Mapper接口组件扫描 -->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="cn.tedu.note.dao"/>
		</bean>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_33204709/article/details/81136262