我已经集成了spring3 + mybatis3,它运行良好。
然后我发现很多SQL会像这样编写:
select * from table1 where id=#{id}
select * from table2 where id=#{id}
我们不需要重复作为程序员!
那么,我们可以定义一个genric dao或mapper来避免这种重复吗?提供演示更好。
有些链接也可以提供帮助。
困扰我很长一段时间,需要一只手。
我想写这样的代码:
Test.java :
just a enity.
TestMapper.java :
public interface TestMapper extends GenericMapper {
public void testMethod1(String..);
//other methods here
}
GenericMapper.java :
public interface GenericMapper<T, PK> {
public T select(PK id);
public boolean update(T t);
public boolean delete(PK id);
public boolean insert(T t);
}
在spring-xx.xml中定义bean :
<bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="mapperInterface" value="com.jackson.mapper.TestMapper" />
</bean>
在我的服务层中调用如下: //请**注意**:方法选择在GenericMapper中定义。
TestService.java :
public TestMapper testMapper;
public Test get(Integer id) {
Test test = testMapper.select(id);
doSmth(test);
}
就在几分钟前,有人说我们可以使用Interceptor接口。而我现在正在努力。
谢谢!
- 杰克逊
答案 0 :(得分:0)
其中一个选项是使用mybatis-generator。
答案 1 :(得分:0)
http://classnotfound.net/blog/mybatisspringgenerics/
我认为本教程正是您所需要的。我不确定他正在运行什么版本的Java(或编辑器配置)但是我不得不添加Dao方法并禁止未经检查的castings(T)和原始类型警告以使其在使用java 8的eclipse mars 2上工作。 / p>