zoukankan      html  css  js  c++  java
  • mybatis设定全局变量

    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>
  • 相关阅读:
    failed to push some refs to 'git@github.com:laniu/liuna.git'报错原因
    ECMAScript和JavaScript的关系
    js面试总结
    第16章 脚本化css
    代理模式
    SQL
    VS
    Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
    SQL
    C#
  • 原文地址:https://www.cnblogs.com/voctrals/p/8615214.html
Copyright © 2011-2022 走看看