Search code examples
ibatismybatis

does the mapper interface necessary


I am new in mybatis,and following mybatis3-user-guide.pdf.

I set up my first application.

However I found that I am not exactly know about the mapper interface.

By now,this is all the configuration for my application(take model User for example):

mybatis config.xml:

<configuration>
    <typeAliases>
        <typeAlias alias="User" type="com.king.mapper.User" />
    </typeAliases>
    <mappers>
        <mapper resource="com/king/mapper/UserMapper.xml" />
    </mappers>
</configuration>

UserMapper.xml:

<mapper namespace="com.king.mapper.UserMapper">
    <select id="selectById" parameterType="int" resultMap="userMap">
        select * from users where id = #{id}
    </select>
    <select id="selectAll" resultType="hashmap">
        select * from users order by created_at desc
    </select>

    <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
        insert into users (name,created_at,updated_at) values (#{name},current_timestamp,current_timestamp)
    </insert>
    <update id="update" parameterType="User">
        update users set name = #{name},updated_at=current_timestamp where id = #{id}
    </update>
    <delete id="delete" parameterType="int">
        delete from users where id = #{id}
    </delete>

    <resultMap id="userMap" type="User">
        <result property="createDate" column="created_at" />
        <result property="updateDate" column="updated_at" />
    </resultMap>
</mapper>

Dao:

public abstract class AbstractSimpleDaoImpl<T> extends SqlSessionDaoSupport implements IDao<T> {
    @Override
    public T query(int id) {
        return getSqlSession().selectOne(getMapperNamespace() + ".selectById", id);
    }
    @Override
    public List<T> list() {
        return getSqlSession().selectList(getMapperNamespace() + ".selectAll");
    }
    @Override
    public int add(T entity) {
        return getSqlSession().insert(getMapperNamespace() + ".insert", entity);
    }
    @Override
    public int update(T entity) {
        return getSqlSession().update(getMapperNamespace() + ".update", entity);
    }
    @Override
    public void delete(T entity) {
        getSqlSession().delete(getMapperNamespace() + ".delete", entity);
    }
    protected abstract String getMapperNamespace();
}

UserDao:

public class UserDao extends AbstractSimpleDaoImpl<User> {
    private static String pack = "com.king.mapper.UserMapper"; 
    @Override
    protected String getMapperNamespace() {
        return pack;
    }
}

It worked. However I found that my example of mybatis will refer to the mapper interface.

It seems that I have to create a Interface named UserMapper in my above example.

But I wonder if it is necessary? and when I have to use it?

BTW,in my opinion,I found that what the mapper interface do just like the what the dao does. Since the dao and the interface may have so many methods with the same name.


Solution

  • You can create mapper interface UserMapper and avoid calling methods getSqlSession()... on your Dao object. So with mapper interface your xml configuration stay same but you can avoid Dao object at all. Just define interface like this:

      public interface UserMapper {
    
        public List<User> selectAll();
    
        public User selectById(@Param("id") int id);
    
        // rest is ommited
      }
    

    Names of methods must match with id of select/update/insert/detele in mapper file. That's it.