一、媒介
Mybatis作为一个优秀的存储进程和高级映射的耐久层框架,今朝在项目实践中运用的较量遍及,最近做项目时候发明白一种之前没见过的设置方法,这里总结下常用的设置方法以便备忘查找。
二、Spring中Mybatis的设置方案一
2.1 大都据源设置案例
(1)数据源设置 <bean id="dataSourceForA" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${db1_url}" /> <property name="username" value="$db1_user}" /> <property name="password" value="${db1_passwd}" /> <property name="maxWait" value="${db1_maxWait}" /> <property name="maxActive" value="28" /> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="300000" /> <property name="testOnBorrow" value="false" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="filters" value="stat" /> </bean> (2)建设sqlSessionFactory <bean id="sqlSessionFactoryForA" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:com/**/mapper1/*Mapper*.xml" /> <property name="dataSource" ref="dataSourceForA" /> <property name="typeAliasesPackage" value="com.zlx.***.dal" /> </bean> (3)设置扫描器,扫描指定路径的mapper生成数据库操纵署理类 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.zlx1.***.dal.***.mapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactoryForA" /> </bean> (4)数据源设置 <bean id="dataSourceForB" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${db_url}" /> <property name="username" value="$db_user}" /> <property name="password" value="${db_passwd}" /> <property name="maxWait" value="${db_maxWait}" /> <property name="maxActive" value="28" /> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="300000" /> <property name="testOnBorrow" value="false" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="filters" value="stat" /> </bean> (5)建设sqlSessionFactory <bean id="sqlSessionFactoryForB" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:com/**/mapper/*Mapper*.xml" /> <property name="dataSource" ref="dataSourceForB" /> <property name="typeAliasesPackage" value="com.zlx.***.dal" /> </bean> (6)设置扫描器,扫描指定路径的mapper生成数据库操纵署理类 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.zlx.***.dal.***.mapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactoryForB" /> </bean>
2.1 道理简朴先容
2.1.1 SqlSessionFactory道理