java - Why must the interface and xml mapper file be in same package and have the same name? -
today preparing example using spring boot , using mybatis data access communication next spring-mybatis. here relevant project configuration (using maven):
src/main/java - edu.home.ltmj.controller + categorycontroller.java - edu.home.ltmj.dao + categorydao.java - edu.home.ltmj.domain + category.java src/main/resources - edu.home.ltmj.dao + categorymapper.xml
relevant content of files:
categorydao.java:
package edu.home.ltmj.dao; public interface categorydao { list<category> getallcategories(); }
categorymapper.xml:
<mapper namespace="edu.home.ltmj.dao.categorydao"> <resultmap id="categorymap" type="edu.home.ltmj.domain.category"> <id property="id" column="id" /> <result property="name" column="name" /> </resultmap> <select id="getallcategories" resultmap="categorymap"> select id, nombre category </select> </mapper>
then, inject instance of dao in request controller (for testing purposes), this:
package edu.home.ltmj.controller; @restcontroller public class categorycontroller { @autowired private categorydao dao; @requestmapping(value="/category/all", method=requestmethod.get, produces=mediatype.application_json_value) public list<categoria> getallcategories() { return dao.getallcategories(); } }
i run project , test execution using curl localhost:8080/category/all
, expected see results in json format, got exception instead:
org.apache.ibatis.binding.bindingexception: invalid bound statement (not found): edu.home.ltmj.dao.categorydao.getallcategories @ org.apache.ibatis.binding.mappermethod$sqlcommand.<init>(mappermethod.java:189) @ org.apache.ibatis.binding.mappermethod.<init>(mappermethod.java:43) @ org.apache.ibatis.binding.mapperproxy.cachedmappermethod(mapperproxy.java:58) @ org.apache.ibatis.binding.mapperproxy.invoke(mapperproxy.java:51) @ com.sun.proxy.$proxy45.getallcategories(unknown source) @ edu.home.ltmj.controller.categoryrestcontroller.getallcategories(categoryrestcontroller.java:27) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) (...)
i don't understand cause of this. there's interface categorydao
, has proper method getallcategories
matches <select id="getallcategories">
. after time of playing this, i've changed name of dao interface categorymapper
, updated namespace in categorymapper.xml. after did this, everything worked normally. also, after having same name class , xml, moved dao class , xml mapper different packages (stil using same name both: categorymapper.), updated namespace in xml file, , got same exception, message updated show name of package of dao interface. again, moved both files same package , worked again.
so, question is: why mybatis need interface , xml mapper file have same name , in same package? mybatis design or issue in spring mybatis?
do have mybatis config file?
if remember correctly same name same location xml file interface when want have setup works no configuration.
if have xml mappers somewhere else can manually specify classpath of xml files using <mappers>
element inside mybatis configuration.
from injecting mappers documentation:
if usermapper has corresponding mybatis xml mapper file in same classpath location mapper interface, parsed automatically mapperfactorybean. there no need specify mapper in mybatis configuration file unless mapper xml files in different classpath location. see sqlsessionfactorybean's configlocation property more information.
so try this:
create
mybatis-config.xml
file insidesrc/main/resources
in it:<?xml version="1.0" encoding="utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="com/test/path/etc/etc/whatevernameyouwant.xml"/> </mappers> </configuration>
where
whatevernameyouwant.xml
containscategorymapper.xml
contained.set location of config file (java configuration below or bean in
applicationcontext
file):@bean public sqlsessionfactorybean sqlsessionfactory() throws exception { sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean(); // .... sessionfactory.setconfiglocation(new classpathresource("mybatis-config.xml")); // .... return sessionfactory; }
Comments
Post a Comment