mybatis spring mvc application, getting Invalid bo

2019-01-19 11:57发布

问题:

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>

回答1:

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.



回答2:

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.



回答3:

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>


回答4:

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



回答5:

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.



回答6:

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



回答7:

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>


回答8:

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.



回答9:

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



回答10:

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



回答11:

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



回答12:

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.



回答13:

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.


回答14:

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


回答15:

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

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


回答16:

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