MyBatis mapper interfaces extend other interface,it does not work

41 views
Skip to first unread message

sence

unread,
May 14, 2012, 2:26:35 AM5/14/12
to mybati...@googlegroups.com
Hi !!

I have a java project use MyBatis and springMVC,may mappers have the same
methods,I want put them in a generic interfaces,so that other mappers will
extend that interface

this is my code:

Base.java:
public abstract Class Base{
....
}

User.java
public class User extends Base implements Serializable{
....
}

BaseMapper.java:
public interface BaseMapper<T extends Base> {
public void save(T t) throws Exception;
}
BaseMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;>
<mapper namespace="com.stamp.base.persistence.BaseMapper">
</mapper>

UserMapper.java:
public interface UserMapper extends BaseMapper<User>{

}

UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;>
<mapper namespace="com.stamp.user.persistence.UserMapper">
<insert id="save" parameterType="com.stamp.user.entity.User">
INSERT INTO tsc_user(login_name ,password) VALUES
(#{loginName},#{password})
</insert>
</mapper>

this is scan mappers in spring xml:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.stamp.*.persistence" />
</bean>
I am sure spring xml is correct

In service I declar BaseMapper<User> baseMapper and use annotation
@Autowire injection it
when I test my project
exception is throwed :
Could not autowire field: private com.stamp.base.persistence.BaseMapper
com.stamp.base.service.impl.BaseServiceImpl.baseMapper; nested exception is
java.lang.NullPointerException

what's with it ? I find in this link
http://groups.google.com/group/mybatis-user/browse_thread/thread/c16f90236101ab83
http://groups.google.com/group/mybatis-user/browse_thread/thread/c16f90236101ab83
it seems it OK!

regards.

sence.

--
View this message in context: http://mybatis-user.963551.n3.nabble.com/MyBatis-mapper-interfaces-extend-other-interface-it-does-not-work-tp3985262.html
Sent from the mybatis-user mailing list archive at Nabble.com.
Reply all
Reply to author
Forward
0 new messages