Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 512
randy Avatar asked Dec 06 '13 15:12

randy


7 Answers

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.

like image 181
Dariusz Avatar answered Oct 23 '22 19:10

Dariusz


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.

like image 24
Giovanni Perea Avatar answered Oct 23 '22 17:10

Giovanni Perea


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>
like image 24
Zheng Xiaodong Avatar answered Oct 23 '22 17:10

Zheng Xiaodong


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.

like image 36
rak22 Avatar answered Oct 23 '22 17:10

rak22


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"));
like image 3
Ayano Avatar answered Oct 23 '22 19:10

Ayano


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

like image 3
user218867 Avatar answered Oct 23 '22 19:10

user218867


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

like image 2
Marco S. Avatar answered Oct 23 '22 17:10

Marco S.