mybatis spring mvc 应用程序,获取无效绑定语句(未找到)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20427210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:23:16  来源:igfitidea点击:

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

springspring-mvcmybatis

提问by randy

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

这是我第一个使用 spring 3.2.4 的 mybatis spring mvc 应用程序,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

这是我的关联文件: 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

映射文件 --> /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

映射文件 --> /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

控制器文件 --> /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

服务文件 --> /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 -->

mybatis-configfile -->

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

enter image description here

在此处输入图片说明

回答by Dariusz

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.

我在寻找我的错误时用谷歌搜索了这个答案。它实际上与 OP 的问题无关,但例外是相同的,并且这个问题在 google 中非常明显。

In my case I forgot to change the mapper namespace

就我而言,我忘记更改映射器命名空间

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

Which resulted in the same problem.

这导致了同样的问题。

回答by Giovanni Perea

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

在阅读此页面中的配置后,我遇到了同样的问题。 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.

我看到我的配置是正确的。所以调试我的应用程序。发现我的 *mappers.xml 文件不在路径中。那个期望必须是。

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.

我的 Maven 项目中的同一文件夹 src "java" 中有 XML 文件。所以当我构建我的应用程序时,文件没有复制到 classes 文件夹。所以我必须将 xml 文件移动到文件夹“资源”。并解决问题。

回答by Zheng Xiaodong

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

因为你的xml在mybatis中没有加载,MapperScannerConfigurer只扫描界面,不扫描xml。有两种方式:

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

回答by rak22

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

我的 spring-boot mybatis 应用程序遇到了类似的问题。问题是mybatis 找不到配置文件。添加后

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.

在 application.properties 文件中,问题得到解决。看起来问题总是围绕配置文件/映射器文件和语句名称。

回答by Marco S.

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.

我用Giovanni Perea解决方案的变体解决了同样的问题(谢谢)。我有 .xml 映射器文件和 .java 映射器文件在同一个文件夹中,我使用 maven 和 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):

在我的解决方案中,我在 maven-resources-plugin 中添加了一个执行,用于将所有 .xml 映射器文件复制到正确的位置(.class 映射器文件的同一文件夹):

<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

使用 maven-resources-plugin 的更多示例:包括和排除文件和目录

If you do not use maven-resources-plugin see: https://stackoverflow.com/a/12446666/2473158

如果您不使用 maven-resources-plugin,请参阅:https: //stackoverflow.com/a/12446666/2473158

回答by Junchen Liu

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

很可能是 java 方法名称和 XML 块的名称不匹配

e.g mapper.getUser()  

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

getUser obviously different from getUsee

getUser 明显不同于 getUsee

回答by Hailin Tan

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

除了上面提到的@Dariusz,我还忘记添加映射器位置。

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

回答by Eric Wang

(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 singledir but with name contains ..

在我的情况,那么原因是IDEA,当映射文件I输入创建多级封装mybatis.mapper,只创建一个单一的目录但名称中包含.

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

虽然我实际上应该mybatis/mapper一次输入以创建多级目录。

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

在这两种情况下,目录mybatis.mapper与 IDEA 的项目视图中显示的相同,所以我花了一段时间才弄清楚为什么......

回答by Palla

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

将 mapper/drugmasterDao.xml 重命名为 mapper/DrugMasterDao.xml ,名称必须与 *Dao.java 文件名匹配,否则会抛出错误或者我们必须明确地创建 Ibatismapping.xml 并在那里添加映射器配置

回答by Ayano

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

就我而言,我有多个DataSource并且应该为每个SessionFactory.

SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mysqlDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath:mappers/**/*Mapper.xml"));