1,通过xml的方式:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="configurationProperties"> <props> <prop key="dbType">oracle</prop> </props> </property> </bean>
2,通过bean的方式:
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sqf = new SqlSessionFactoryBean(); ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()); sqf.setDataSource(dataSource()); sqf.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); Properties properties = new Properties(); properties.put("dbType", "oracle"); sqf.setConfigurationProperties(properties); return sqf.getObject(); }
使用:
1,java类中获取全局变量:
sqlSessionFactory.getConfiguration().getVariables().getProperty("dbType");
2,mapper中使用全局变量:
<select id="test"> SELECT '${dbType}' FROM dual </select>
或
<select id="findList" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM tb a where a.del_flag = 0 <if test="'${testRun}' == 'no'"> and a.state = '1' </if> </select>