Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand right, I can offer next variant: create MyBatis <code>TypeHandler</code> for <code>Strategy</code> Type, where get the appropriate strategy bean from spring context. I hope I have not forgotten anything.</p> <p>Details: </p> <pre><code>MyClass { private Strategy strategy; } StrategyA implements Strategy - "a" id in DB StrategyB implements Strategy - "b" id in DB </code></pre> <p>1) spring-context.xml:</p> <pre><code>&lt;bean id="strategyA" class="test.mybatis.inject.StrategyA" scope="singleton"&gt;&lt;/bean&gt; &lt;bean id="strategyB" class="test.mybatis.inject.StrategyB" scope="singleton"&gt;&lt;/bean&gt; </code></pre> <p>2) Create Spring context provider: </p> <pre><code>public class ApplicationContextProvider implements ApplicationContextAware { private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { context = ctx; } } </code></pre> <p>3) Add provider bean description to spring-context.xml:</p> <pre><code>&lt;bean id="applicationContextProvder" class="test.mybatis.inject.ApplicationContextProvider"/&gt; </code></pre> <p>4) Create StrategyTypeHandler:</p> <pre><code>@MappedTypes(value={Strategy.class}) public class StrategyTypeHandler implements TypeHandler&lt;Strategy&gt; { ... public Strategy getResult(ResultSet rs, String columnName) throws SQLException { switch(rs.getString(columnName)){ //for Java 1.7 &amp; above case "a": return (StrategyA)ApplicationContextProvider.getApplicationContext().getBean(StrategyA.class); case "b": return (StrategyB)ApplicationContextProvider.getApplicationContext().getBean(StrategyB.class); } return null; } } </code></pre> <p>5) MyClassMapper.xml: </p> <pre><code>&lt;result column="strategy_id" property="strategy" jdbcType="VARCHAR" typeHandler="test.mybatis.inject.StrategyTypeHandler"/&gt; </code></pre> <p>Run.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload