HermioneZhou 2019-03-19 13:23 采纳率: 0%
浏览 353

java Spring和mybatis整合时,junit测试时报的错。搞了二个小时了,还没解决。求助

这是我的Junit的代码:
@Test
public void test1(){
SqlSessionFactoryBuilder ssfb= new SqlSessionFactoryBuilder();

     System.out.println("ssfb:"+ssfb);

    InputStream is= Test1.class.getClassLoader().getResourceAsStream("spring-mybatis.xml");
    System.out.println("is:"+is);
            SqlSessionFactory ssf = ssfb.build(is);
            System.out.println("dd");

     session = ssf.openSession();
     dao = session.getMapper(Aoo2Dao.class);

     Aoo2 a2 = new Aoo2();

     a2.setId(19);
     a2.setName("钻石王老五");
     a2.setAge(37);
     a2.setPhone("13500990888");
     dao.add(a2);
     //提交事务
       session.commit();
       session.close();
 }
 ,运行报错。错误是PersistenceException,文档根元素beans必须匹配
 DOCTYPE根null。求解答。

 我的spring-mybatis.xml文件是:
 <?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="  
   http://www.springframework.org/schema/beans   
   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
   http://www.springframework.org/schema/mvc   
   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
   http://www.springframework.org/schema/tx   
   http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-4.3.xsd
   http://www.springframework.org/schema/data/jpa 
   http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">


    <util:properties 
    id="db" location="classpath:db.properties"   />

    <!-- 配置数据库连接池 -->
    <bean id="ds" 
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
        <property name="driverClassName"  
        value="#{db.driver}"/>
        <property name="url"  
        value="#{db.url}"/>
        <property name="username"   
        value="#{db.user}"/>
        <property name="password"  
        value="#{db.password}"/>
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <!-- 
        该bean的作用是用来代替MyBatis配置文件
     -->
     <bean  
     class="org.mybatis.spring.SqlSessionFactoryBean">
         <!-- 指定连接池 -->
         <property name="dataSource" ref="ds"/>
         <!-- 指定映射文件位置 -->
         <property name="mapperLocations"
         value="classpath:entity/*.xml"/>
     </bean>
     <!-- 配置MapperScannerConfigurer -->
     <!-- 
         该bean会扫描指定包及其子包下面的所有的Mapper
         映射器(接口),然后生成符合该接口要求的对象
         (通过调用SqlSession的getMapper方法),接下来,
         会将这些对象(即Mapper映射器的实现对象)添加到
         Spring容器里面(默认的id是首字母小写之后的接口名)。
      -->
      <bean 
      class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 指定要扫描的包 -->
            <property name="basePackage" value="dao"/>
            <!-- 指定只扫描带有该注解的接口 -->
         <property name="annotationClass" value="annotations.MyBatisRepository"/> 

      </bean>

  • 写回答

3条回答

  • L星火燎原 2019-03-19 13:53
    关注

    有可能是你的beans重复了,你把Mapper.xml贴出来看看,最好带上行号。

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号