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>

查看全部
qq_36142320
HermioneZhou
2019/03/19 13:23
  • spring
  • java
  • 点赞
  • 收藏
  • 回答
    私信

3个回复