title: Spring3+mybatis3 generic dao(or mapper)
body: <p>I have integrated spring3 + mybatis3,and it worked good.</p>
  
  <p>Then I found that many SQLs will be written like this:</p>
  
  <pre><code>select * from table1 where id=#{id}
  select * from table2 where id=#{id}
  </code></pre>
  
  <p>We don't need repeat as a programmer!</p>
  
  <p>So,Can we defined a genric dao or mapper to avoid this repeat? Provide a demo is better. </p>
  
  <p>Some links can help is kindly too.</p>
  
  <p>It bothers me for a long time,need a hand.</p>
  
  <p>I want to write my code like this:</p>
  
  <p><strong>Test.java</strong>:</p>
  
  <pre><code>just a enity.
  </code></pre>
  
  <p><strong>TestMapper.java</strong>:</p>
  
  <pre><code>public interface TestMapper extends GenericMapper {
      public void testMethod1(String..);
      //other methods here
  }
  </code></pre>
  
  <p><strong>GenericMapper.java</strong>:</p>
  
  <pre><code>public interface GenericMapper&lt;T, PK&gt; {
      public T select(PK id);
      public boolean update(T t);
      public boolean delete(PK id);
      public boolean insert(T t);
  }
  </code></pre>
  
  <p><strong>defind bean in spring-xx.xml</strong>:</p>
  
  <pre><code>&lt;bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"&gt;
      &lt;property name="sqlSessionFactory" ref="sqlSessionFactory" /&gt;
      &lt;property name="mapperInterface" value="com.jackson.mapper.TestMapper" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Call in my service layer like this:
  // Please**NOTICE**:method <strong>select</strong> is defined in GenericMapper.</p>
  
  <p><strong>TestService.java</strong>:</p>
  
  <pre><code>public TestMapper testMapper;
  
  public Test get(Integer id) {
      Test test = testMapper.select(id);   
      doSmth(test);
  }
  </code></pre>
  
  <p>Just a few minutes ago,Someone said we can use a Interceptor interface. And I am trying now. </p>
  
  <p>Thanks!</p>
  
  <p>--jackson</p>