问个mybatis和spring security等结合出现的方法没找到问题

似故人来 发布于 2015/03/27 11:37
阅读 10K+
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

问题很简单

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): spring.security.maven.dao.T_userMapper.selectByUserName

这种问题不外乎命名空间这类写错了,可是怎么找不到。

说明下mapper.xml和mapper.java是自动生成的

先看spring对mybatis配置文件吧

<!-- spring与mybatis整合配置,扫描所有dao --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
 <property name="basePackage" value="spring.security.maven.dao" />  
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 加载dao
 </bean> 
<!-- mybatis文件配置,扫描所有mapper文件 --> 
<bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean"  p:dataSource-ref="dataSource" 
 p:mapperLocations="classpath*:spring/security/maven/daomain/*Mapper.xml"/> 加载mapper
T_userMappper映射的map

<mapper namespace="spring.security.maven.dao.T_userMapper" >
。。。。。。省去n多方法

<select id="selectByUserName" resultMap="BaseResultMap" parameterType="java.lang.String" 
 SELECT *FROM  from t_user   where user_name = #{userName,jdbcType=VARCHAR}
</select>
</mapper>
java

package spring.security.maven.dao;
 import spring.security.maven.daomain.T_user
 public interface T_userMapper {
    T_user selectByUserName(String userName)
  int deleteByPrimaryKey(Integer id)
  int insert(T_user record);  
 int insertSelective(T_user record)
  T_user selectByPrimaryKey(Integer id)
  int updateByPrimaryKeySelective(T_user record)
  int updateByPrimaryKey(T_user record);
 }
请问下这是什么情况

再贴点bug

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): spring.security.maven.dao.T_userMapper.selectByUserName
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51)
at com.sun.proxy.$Proxy22.selectByUserName(Unknown Source)
at spring.security.maven.serviceImp.UserServiceImp.selectByUserName(UserServiceImp.java:20)
at TestUser.getUser(TestUser.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
拜托各位了


加载中
0
wskery001
wskery001
不扫描  mybatis.cfg.xml么?
似故人来
似故人来
spring接管后没他事了,我之前写过这样的,可行,可最近再写一次不知道哪里出问题了
似故人来
似故人来
不用的
0
似故人来
似故人来

问题解决了,不是代码问题是编译器问题,当然也也许是可以配置的

我用的是intellij idea ,创建的是maven项目。maven的结构就不多了其中java是放置源码的resource是资源文件的,那么按照我们的以前学习mybatis的方法是建议把Mapper.xml的文件放在daomain里面和实体在一起的,淡然这不是一定的路径写对了,爱放哪里原则上没问题的 。可是idea明确了源码和资源文件的放置位置(maven项目),也就是说源码就要会呗编译,当我把xml文件放在java目录下的daomian我再猜想是不是被编译了,才导致找不到方法,于是我把Mapper的映射文件放到资源文件下,改下加载Mpper.xml的路径,结果可以了。问题解决了。其实之前我有一次也是这样解决的,可是我当时不以为然,因为我们一贯是放在daomian下面的。

但是真的是这样嘛?我也不确定,也只是大三的小菜鸟,如果大家有好的解决方法邪王不宁赐教。谢谢

0
若只如初见_blog
若只如初见_blog
如果是Intellij idea的话,需要在pom.xml文件中加入, 会将src/main/java下的xml文件随同java编译后的class文件一同copy到相应的class目录
<build>
    <resources>
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.xml</include>
             </includes>
         </resource>
    </resources>
</build>
0
c
clxhhsy

IDEA中,如果你把xml文件放在java目录下,编译和打包时是不会打包进WEB-INF/classes下的,需要在pom文件中配置:

<build>
    <!--mybatis逆向工程生成代码-->
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>${mybatis_general.version}</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    <!--打包时包含源代码包下的资源文件-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>



0
ovO半风
ovO半风
可以,终于找到问题所在了,mapper.xml放到java包下,maven编译通过这个配置解决了我的问题
0
啵啵君--火星IT男
啵啵君--火星IT男


博主写得很棒,这里推荐大家一个专注于Java开发的个人博客Queen's Blog(黛玛Queen),每天更新文章,干货满满哦,不容错过,需要的点这里咯。
http://www.marsitman.com/mybatis/mybatis-oracle-getid.html

返回顶部
顶部