mybatis spring mvc application, getting Invalid bound statement (not found)
Asked Answered
N

18

20

this is my first mybatis spring mvc application using spring 3.2.4, mybatis-spring-1.2.1

When i try to call my webservice i get the error::

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.apache.ibatis.binding.BindingException: Invalid bound 
statement (not found): 
org.mydomain.formulary.drugmaster.dao.DrugMasterDao.getDrugsWithAlert

I must be missing something obvious. Thanks for any help

Here are my associated files: applicationContext.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="formularyDb" />
    <property name="configLocation"  value="file:/web/sites/drugformulary-spring/config/mybatis-config.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.mydomain.formulary.mappers" />
</bean>
<bean id="DrugMasterDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="org.mydomain.formulary.drugmaster.dao.DrugMasterDao" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

mapper file --> /classes/org/mydomain/formulary/mappers/drugmasterDao.xml

<mapper namespace="org.mydomain.formulary.drugmaster.dao.DrugMasterDao">

<select id="getDrugsWithAlert" parameterType="int" resultType="org.mydomain.formulary.drug_master.model.DrugMasters">
    Select drug_id,drug_name,drug_alert_date,drug_alert_source, rownum
    from (select drug_id,drug_name,to_char(drug_alert_datetime,'MM/DD/YYYY') as drug_alert_date ,drug_alert_source, rownum
    from drug_master
    where drug_status ='A' and length(drug_alert) > 0
    order by drug_alert_datetime DESC )
    where
    <if test="_parameter != null">
        rownum &lt; #{count}
    </if>
</select>
</mapper>

mapper file --> /classes/org/mydomain/formulary/drugmaster/dao/DrugMasterDao.java

public interface DrugMasterDao {
    public List<DrugMasters> getDrugsWithAlert(int count);
}

controller file --> /classes/org/mydomain/formulary/drugmaster/controller/DrugMasterController.java

@Controller
public class DrugMasterController {
@Autowired
DrugMasterService drugMasterService;


@RequestMapping(value = "/drugmaster/withalerts/count/{count}", method = RequestMethod.GET)
public String withAlerts(ModelMap model, @PathVariable int count) {

    List<DrugMasters> drugs = drugMasterService.getDrugsWithAlert(count);

    return null/*for now*/;

}
}    

service file --> /classes/org/mydomain/formulary/drugmaster/service/DrugMasterServiceImpl.java

@Service
public class DrugMasterServiceImpl implements DrugMasterService {

    @Autowired
    DrugMasterDao drugMasterDao;

    public List<DrugMasters> getDrugsWithAlert(int count){
        return drugMasterDao.getDrugsWithAlert(count);
    }
}

mybatis-configfile -->

<configuration>
<settings>
    <setting name="cacheEnabled" value="false" />
    <setting name="lazyLoadingEnabled" value="false" />
</settings>
</configuration>

enter image description here

Norton answered 6/12, 2013 at 15:9 Comment(3)
Oops now mybaits-config.xml missing. Basically make sure your configLocation is being read( I would prefer it to be placed in classpath, next make sure your mapper xmls are being read. These errors are due to missing mapper files For example I've added mapper location in sessionfactory <property name="mapperLocations" value="classpath*:sqlmap/*.xml" />Jubilation
i have added mybaits-config.xml, but it is pretty simple. when you say "make sure your mapper xmls are being read" , is there any way via debugging to see if they are being read?Norton
Because of path resolution issues( when mybatis cannot find mapper file), Hence it would be better to check if your mapper file is referredJubilation
E
26

I googled this answer when looking for my error. It's actually unrelated to OP's problem, but the exception is the same and this question's very visible in google.

In my case I forgot to change the mapper namespace

<mapper namespace="pl.my.package.MyNewMapper">

Which resulted in the same problem.

Ephod answered 21/7, 2015 at 8:14 Comment(1)
Make sure your *Mapper.xml exists in your namespace folder!Transvestite
P
7

I had the same problem so after reading the configurations in this page. https://mybatis.github.io/spring/mappers.html#scan

I saw that my configuration was correct. so debugging my application. found that my *mappers.xml files where not in the path. that expect must be.

I had the XML files in the same folder src "java" in my maven project. so when I build my applications the file were not copy to classes folder. So I have to move the xml files to folder "resources". and the fix the problem.

Patriliny answered 30/4, 2015 at 19:4 Comment(0)
P
6

Because your xml not load in mybatis, MapperScannerConfigurer only scan interface, not xml. Have two way:

<mappers>
    <mapper resource="org/mydomain/formulary/mappers/drugmasterDao.xml"/>
</mappers>

or

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath*:org/mydomain/**/*.xml"/>
</bean>
Paul answered 7/5, 2017 at 15:7 Comment(0)
F
4

i had similar problem for my spring-boot mybatis application. The issue was mybatis couldn't find the configuration file. After adding

mybatis.config-location=classpath:mybatis-config.xml

in the application.properties file, issue got resolved. Looks like issue is always around configuration files/mapper files and statement names.

Facies answered 12/1, 2017 at 0:39 Comment(0)
B
4

(There could be many reasons, my case is a bit rare & weird, it's hard to discover, so I'd like to add an answer here, just in case someone did the same thing as me.)

In my case, then reason is in IDEA, when create multi-level package for mapper file I input mybatis.mapper, which only create a single dir but with name contains ..

While I should actually input mybatis/mapper to create multi-level dir at once.

In these 2 cases, the dir are shown the same as mybatis.mapper in the project view of IDEA, so it took me quiet a while to figure out why ...

Ballarat answered 18/6, 2019 at 10:49 Comment(0)
S
3

In my case, I had multiple DataSource and should set mappler locations for each SessionFactory.

SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mysqlDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath:mappers/**/*Mapper.xml"));
Shaftesbury answered 16/7, 2018 at 2:41 Comment(1)
That was what I was looking for! ThanksWenzel
B
2

I resolved the same problem with a variant of the solution of Giovanni Perea(thank you). I have the .xml mapper files in the same folder with .java mapper files and I using maven with maven-resources-plugin.

In my solution I have add an execution in maven-resources-plugin for copy all the .xml mapper file to the correct location(same folder of the .class mapper files):

<execution>
    <id>copy-mappers-xml</id>
    <phase>validate</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <outputDirectory>${project.build.directory}/classes/com/myapplication/mapper</outputDirectory>
        <resources>          
            <resource>
                <directory>${project.basedir}/src/main/java/com/myapplication/mapper/</directory>
                <filtering>false</filtering>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>              
     </configuration>
</execution>

More examples with maven-resources-plugin: Including and excluding files and directories

If you do not use maven-resources-plugin see: https://mcmap.net/q/341276/-why-xml-files-in-eclipse-project-39-s-source-directory-is-not-copied-to-target-classes-directory

Bolo answered 7/10, 2015 at 13:11 Comment(0)
A
2

In my case it was a typo error in the id of the mapper xml statement e.g.

<select id="getDtaa" resultType="Data">

List<T> getData()

After changing the id in the XML to the correct function name it worked.

Arndt answered 23/4, 2017 at 10:37 Comment(0)
K
2

most likely the java method name and the XML block's name mismatches

e.g mapper.getUser()  

 <select id="getUsee" resultMap="student">
    ...........
 <>

getUser obviously different from getUsee

Knuth answered 11/5, 2017 at 10:45 Comment(0)
A
2

Except for @Dariusz mentioned above, I've also forgotten to add mapper location.

<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>
Agriculture answered 5/10, 2018 at 2:7 Comment(0)
C
1

in the spring configuration, taking a xml way for example, make sure that the mapper.xml files are located at the place assigned as the value of the property named mapperLocations. Once I had it at mappers/anotherfolder/*.xml. And that causes the pain.

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="somepackage.model"/>
    <property name="mapperLocations" value="classpath*:mappers/*.xml"/>
</bean>
Cerumen answered 12/6, 2016 at 15:16 Comment(1)
I hitted this issue again, and this time this answer helped me. One more comment, in a case that u defined your mappers in the myBatis configuration file, instead of the spring configuration file, u must ensure that the location is right in lines like : <mappers> <mapper resource="com/ceteng/mybatis/model/User.xml"/> </mappers>Annikaanniken
E
1

Rename mapper/drugmasterDao.xml to mapper/DrugMasterDao.xml , the name has to match the *Dao.java file name otherwise it throws error or we have to create Ibatismapping.xml explictly and add mapper configuration there

Escobar answered 11/4, 2018 at 22:32 Comment(0)
C
0

Check if there is any overload method in mapper. Try to use a separate name instead.

Crumple answered 21/11, 2015 at 7:43 Comment(0)
A
0

I have also encountered this problem in my development. In generall, if you are using xml configuration files for spring,myBatis and etc., then this problem is mostly caused by some mistake in your xml configuration files.

The most voted answer by Dariusz demonstrated that there maybe some problems in the myBatis xml configuration file, while in my case, I found that problems in spring xml configuration file can also result in this problem.

In the situation of this post, in the applicationContext.xml(which should be a configuration file of Spring), we can see a basePackage configuration for the MapperScannerConfigurer:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.mydomain.formulary.mappers" /> </bean>

so if the value for this property is wrong(the package name is not right), it will also result in this problem.

Annikaanniken answered 28/9, 2016 at 5:26 Comment(0)
D
0

2 Points to cover

  1. Check whether the method name is the same in both the XML and Java methods.
  2. Mapper:scan covers the required package. If you are using annotation then @MapperScan(com.xxx) must be in the defined path.
Dkl answered 6/2, 2018 at 15:33 Comment(0)
P
0

In my case, using a query in an annotation, i didn't give enough credit to the fact that the XML in the annotation gets interpreted as XML.

So if you have

@Select("select * from table where id <> 'blabla'")

The <> is messing up the XML. You need to put it in a CDATA section

@Select("select * from table where id <![CDATA[ <> ]]> 'blabla'")
Prosecutor answered 15/11, 2018 at 13:30 Comment(0)
G
0

I think the issue is that MyBatis can't find the XML mapper configuration file. In my project using Spring Boot with MyBatis-3, I configured the mapper location in the application.properties as shown below:

mybatis.mapper-locations=classpath:mappers/*.xml

If you're using application.yml instead of .properties:

mybatis:
  mapper-locations: "classpath:mappers/*.xml"
Goldagoldarina answered 29/12, 2022 at 2:19 Comment(0)
R
-1

DrugMasters attributes defines must associate with the drug_id,drug_name,drug_alert_date,drug_alert_source, rownum .

Rhubarb answered 20/3, 2014 at 3:17 Comment(1)
Please describe it more specific.Boastful

© 2022 - 2024 — McMap. All rights reserved.