randy
randy

Reputation: 1877

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

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

Upvotes: 20

Views: 86339

Answers (19)

joseluisbz
joseluisbz

Reputation: 1648

I had a similar problem.

My mappers were located in main -> resources -> mappers. Then in my application.yml I placed these lines:

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

Upvotes: 0

Hieu Nguyen
Hieu Nguyen

Reputation: 27

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"

Upvotes: 0

Eric
Eric

Reputation: 24870

(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 ...

Upvotes: 4

reallynice
reallynice

Reputation: 1301

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'")

Upvotes: 0

Hailin Tan
Hailin Tan

Reputation: 1109

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

<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>

Upvotes: 2

Ayano
Ayano

Reputation: 541

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"));

Upvotes: 3

Palla
Palla

Reputation: 1262

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

Upvotes: 1

Deepak
Deepak

Reputation: 821

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.

Upvotes: 0

Junchen Liu
Junchen Liu

Reputation: 5614

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

Upvotes: 2

Zheng Xiaodong
Zheng Xiaodong

Reputation: 153

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>

Upvotes: 6

Stefan Michev
Stefan Michev

Reputation: 5093

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.

Upvotes: 2

rak22
rak22

Reputation: 338

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.

Upvotes: 4

ZhaoGang
ZhaoGang

Reputation: 4915

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.

Upvotes: 0

Tiina
Tiina

Reputation: 4765

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>

Upvotes: 1

hailinzeng
hailinzeng

Reputation: 995

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

Upvotes: 0

Giovanni Perea
Giovanni Perea

Reputation: 348

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.

Upvotes: 7

Marco S.
Marco S.

Reputation: 551

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://stackoverflow.com/a/12446666/2473158

Upvotes: 2

Dariusz
Dariusz

Reputation: 22241

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.

Upvotes: 26

wentao_tang
wentao_tang

Reputation: 1

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

Upvotes: -1

Related Questions