MyBatis mapper interface extends other interfaces ,but it can't work

348 views
Skip to first unread message

4708...@qq.com

unread,
May 14, 2012, 2:10:23 AM5/14/12
to mybatis-user
Hi team!!

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 "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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 "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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
it seems it OK!

regards.

sence.

Rick R

unread,
May 14, 2012, 11:04:00 PM5/14/12
to mybati...@googlegroups.com
Are you sure the issue is related to you using the generic interface implementation?

I also need to annotate my Mappers with @Repository to have them picked up as a candidate for auto-wiring.
--
Rick R

AntPort

unread,
May 15, 2012, 3:48:58 AM5/15/12
to mybati...@googlegroups.com
Can you post your service implementation (and test class)?
Reply all
Reply to author
Forward
0 new messages