java - Spring + MyBatis exception: Mapped Statements collection does not contain value for -
i got exception when executed app:
public class mainapp { private static applicationcontext context = null; static sqlsession session = null; public static void main(string[] args) throws illegalargumentexception { try { context = new filesystemxmlapplicationcontext( "src/main/webapp/web-inf/applicationcontext.xml"); session = (sqlsession) context.getbean("sqlsession"); ordermapper ordermapper = session.getmapper(ordermapper.class); int orderquantity = ordermapper.getallorder().size(); system.out.println("order quantity: " + orderquantity); session.commit(); session.close(); } catch (throwable e) { e.printstacktrace(); } } }
here ordermapper interface:
public interface ordermapper { public list<order> getallorder(); }
ordermapper.xml
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.thonglm1.spring.mappers.ordermapper"> <resultmap id="result" type="order"> <result property="orderid" column="cartid" /> <result property="type" column="type" /> <result property="saleid" column="saleid" /> <result property="status" column="status" /> <result property="info1" column="info1" /> <result property="info2" column="info2" /> <result property="info3" column="info3" /> </resultmap> <select id="getallorder" resultmap="result"> select cartid, type, info1 train_order; </select> </mapper>
applicationcontext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="oracle.jdbc.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@172.21.8.62:1521:shesvdev" /> <property name="username" value="test" /> <property name="password" value="test" /> </bean> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="src/main/webapp/web-inf/mybatis-config.xml" /> </bean> <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg index="0" ref="sqlsessionfactory" /> </bean> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.thonglm1.spring.mappers" /> </bean> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> </beans>
mybatis-config.xml
?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> <typealiases> <typealias type="com.thonglm1.spring.domain.order" alias="order" /> </typealiases> <mappers> <package name="com.thonglm1.spring.mappers" /> </mappers> </configuration>
and finally, exception got:
java.lang.illegalargumentexception: mapped statements collection not contain value com.thonglm1.spring.mappers.ordermapper.getallorder @ org.apache.ibatis.session.configuration$strictmap.get(configuration.java:672) @ org.apache.ibatis.session.configuration.getmappedstatement(configuration.java:507) @ org.apache.ibatis.session.configuration.getmappedstatement(configuration.java:500) @ org.apache.ibatis.binding.mappermethod.setupcommandtype(mappermethod.java:240) @ org.apache.ibatis.binding.mappermethod.(mappermethod.java:71) @ org.apache.ibatis.binding.mapperproxy.invoke(mapperproxy.java:39) @ $proxy6.getallorder(unknown source) @ mainapp.main(mainapp.java:21)
and 1 more thing, since quite new this, there bad practices in code ? or improve ? please feel free comment, i'd appreciate it.
where have specified mapper xml location? add in sqlsessionfactory's property mapperlocations
. since using maven. believe better move mapper xml file folder inside resources
directory. when add more number of xml files easier maintain. think below folder structure make sense related stuff grouped jdbc related stuff under 1 package subpackages domain, mappers, service interfaces , service interface implementation.
in case since configurations go inside web-inf/classes directory during compile/packaging, classpath, application configuration should hold good.
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="oracle.jdbc.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@172.21.8.62:1521:shesvdev" /> <property name="username" value="test" /> <property name="password" value="test" /> </bean> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="classpath:config/mybatis-config.xml" /> <property name="mapperlocations" value="classpath*:sqlmap/*.xml" /> </bean> <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg index="0" ref="sqlsessionfactory" /> </bean> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.thonglm1.spring.mappers" /> </bean> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> </beans>
and mybatis-config.xml
, default aliases in domain package class name first letter being smaller.
?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> <typealiases> <package name="com.thonglm1.spring.domain" /> </typealiases> </configuration>
Comments
Post a Comment