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>

 

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2021-09-01
  • 2021-12-11
  • 2021-07-12
  • 2021-09-29
  • 2021-10-08
  • 2021-08-26
猜你喜欢
  • 2022-12-23
  • 2021-10-20
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
相关资源
相似解决方案