java之Mybatis-Spring Java配置@MapperScan注解

duanxz 阅读:57 2023-09-06 19:40:43 评论:0

我正在尝试设置我的 mybatis-spring,如下例所示:

1) stackoverflow 上一个答案的代码,下面的一些答案 ( MyBatis-Spring + @Configuration - Can't autowire mapper beans )

@Configuration  
@MapperScan("org.mybatis.spring.sample.mapper")  
public class AppConfig  
{    
    @Bean    
    public DataSource dataSource()  
    {      
      return new EmbeddedDatabaseBuilder().addScript("schema.sql").build();    
    }    
   @Bean    
   public DataSourceTransactionManager transactionManager()  
   {      
        return new DataSourceTransactionManager(dataSource());    
   }    
   @Bean    
   public SqlSessionFactory sqlSessionFactory() throws Exception  
   {      
       SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 
       sessionFactory.setDataSource(dataSource());     
       return sessionFactory.getObject();    
   }  
} 

2)他们文档中的代码 ( http://www.mybatis.org/spring/mappers.html )

用法:

public class FooServiceImpl implements FooService { 
 
  private UserMapper userMapper; 
 
  public void setUserMapper(UserMapper userMapper) { 
    this.userMapper = userMapper; 
  } 
 
  public User doSomeBusinessStuff(String userId) { 
    return this.userMapper.getUser(userId); 
  } 
} 

使用@MapperScan 注册映射器:

@Configuration 
@MapperScan("org.mybatis.spring.sample.mapper") 
public class AppConfig { 
 
  @Bean 
  public DataSource dataSource() { 
    return new EmbeddedDatabaseBuilder().addScript("schema.sql").build() 
  } 
 
  @Bean 
  public SqlSessionFactory sqlSessionFactory() throws Exception { 
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 
    sessionFactory.setDataSource(dataSource()); 
    return sessionFactory.getObject(); 
  } 
} 

我的代码不起作用,如下所示:

我的带有嵌套 AppConfig 的应用程序:

@SpringBootApplication 
@MapperScan(basePackages="com.tjwhalen.game.service.dao") 
public class Application { 
 
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    } 
 
    //@MapperScan(basePackages="com.tjwhalen.game.service.dao") 
    public class AppConfig { 
 
        @Autowired 
        DataSource datasource; 
 
        @Bean 
        public DataSourceTransactionManager transactionManager() { 
            return new DataSourceTransactionManager(datasource); 
        } 
 
        @Bean 
        public SqlSessionFactory sqlSessionFactory() throws Exception { 
            SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); 
            sqlSessionFactory.setDataSource(datasource); 
            //sqlSessionFactory.setTypeAliasesPackage("com.tjwhalen.game.model"); 
            return (SqlSessionFactory) sqlSessionFactory.getObject(); 
        } 
 
        @Bean 
        public ItemSummaryDbService itemSummaryDbService() { 
            return new ItemSummaryDbServiceImpl(); 
        } 
    } 
} 

我的服务:

public class ItemSummaryDbServiceImpl implements ItemSummaryDbService { 
 
    @Autowired 
    private ItemSummaryMapper itemSummaryMapper; 
 
    public void setItemSummaryMapper(ItemSummaryMapper itemSummaryMapper) { 
        this.itemSummaryMapper = itemSummaryMapper; 
    } 
 
    public void writeItemSummarys(List<ItemSummary> itemSummarys) { 
 
        for(ItemSummary itemSummary : itemSummarys) { 
            itemSummaryMapper.insertItemSummary(itemSummary); 
        } 
    } 
 
    public List<ItemSummary> lookupItemSummarys() { 
        return itemSummaryMapper.selectItemSummarys(); 
    } 
} 

@MapperScan 注释指示的包中的我的映射器:

package com.tjwhalen.game.service.dao; 
 
import java.util.List; 
 
import com.tjwhalen.game.model.ItemSummary; 
 
public interface ItemSummaryMapper { 
 
    public void insertItemSummary(ItemSummary itemSummary); 
    public List<ItemSummary> selectItemSummarys(); 
} 

我的用法:

public class LoadItems implements CommandLineRunner { 
 
    private final static Logger logger = LoggerFactory.getLogger(LoadItems.class); 
 
    @Autowired 
    private ItemSummaryDbService service; 
 
    public void run(String... arg0) throws Exception { 
        logger.info("LoadItems is running"); 
 
        ArrayList<ItemSummary> list = new ArrayList<ItemSummary>(); 
        list.add(new ItemSummary(1, "one", 1)); 
        service.writeItemSummarys(list); 
 
    } 
} 

我的堆栈跟踪:

java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection;Z)Lorg/apache/ibatis/transaction/Transaction; 
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77) ~[ibatis-core-3.0.jar:na] 
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:40) ~[ibatis-core-3.0.jar:na] 
    at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:102) ~[mybatis-spring-1.3.0.jar:1.3.0] 
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:429) ~[mybatis-spring-1.3.0.jar:1.3.0] 
    at com.sun.proxy.$Proxy41.insert(Unknown Source) ~[na:na] 
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:279) ~[mybatis-spring-1.3.0.jar:1.3.0] 
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) ~[ibatis-core-3.0.jar:na] 
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35) ~[ibatis-core-3.0.jar:na] 
    at com.sun.proxy.$Proxy42.insertItemSummary(Unknown Source) ~[na:na] 
    at com.tjwhalen.game.service.impl.ItemSummaryDbServiceImpl.writeItemSummarys(ItemSummaryDbServiceImpl.java:32) ~[classes/:na] 
    at com.tjwhalen.game.loader.LoadItems.run(LoadItems.java:28) ~[classes/:na] 
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:782) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:769) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at com.tjwhalen.game.Application.main(Application.java:37) [classes/:na] 

最后是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
     <modelVersion>4.0.0</modelVersion> 
     <groupId>com.tjwhalen.game</groupId> 
     <artifactId>Runescape-App</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
 
 
    <properties> 
        <java.version>1.8</java.version> 
    </properties> 
 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.4.0.RELEASE</version> 
    </parent> 
 
    <dependencies> 
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter</artifactId> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-web</artifactId> 
        </dependency> 
 
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 
        <dependency> 
            <groupId>com.fasterxml.jackson.core</groupId> 
            <artifactId>jackson-databind</artifactId> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/postgresql/postgresql --> 
        <dependency> 
            <groupId>postgresql</groupId> 
            <artifactId>postgresql</artifactId> 
            <version>8.4-702.jdbc4</version> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 
        <dependency> 
            <groupId>org.mybatis</groupId> 
            <artifactId>mybatis-spring</artifactId> 
            <version>1.3.0</version> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core --> 
        <dependency> 
            <groupId>org.apache.ibatis</groupId> 
            <artifactId>ibatis-core</artifactId> 
            <version>3.0</version> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> 
        <!-- <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-test</artifactId> 
            <version>4.3.2.RELEASE</version> 
        </dependency> --> 
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-jdbc</artifactId> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp --> 
        <dependency> 
            <groupId>commons-dbcp</groupId> 
            <artifactId>commons-dbcp</artifactId> 
        </dependency> 
 
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 
        <dependency> 
            <groupId>org.mybatis</groupId> 
            <artifactId>mybatis</artifactId> 
            <version>3.4.0</version> 
        </dependency> 
 
<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-infrastructure --> 
<dependency> 
    <groupId>org.springframework.batch</groupId> 
    <artifactId>spring-batch-infrastructure</artifactId> 
</dependency> 
 
 
 
 
    </dependencies> 
 
 
 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins> 
    </build>  
 
 
 
</project> 

过去我遇到过很多关于不正确的 pom.xml 的问题,所以我查看了涉及数据库访问的每个依赖项并确保项目具有正确提供的依赖项。我还检查了版本是否与提供的依赖项中提到的相同。 错误消息 AbstractMethodError 表示什么?

请随时提出任何需要澄清的问题

请您参考如下方法:

我想通了。我添加了一个依赖

    <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core --> 
    <dependency> 
        <groupId>org.apache.ibatis</groupId> 
        <artifactId>ibatis-core</artifactId> 
        <version>3.0</version> 
    </dependency> 

并删除它修复。我不知道为什么会出现这种依赖关系,我仔细检查了其余的依赖关系,看看它们是否依赖 ibatis-core 而它们没有。

所以这是我的一个疏忽,并表明 AbstractMethodError 可能是一个依赖问题,这是面对它时首先应该检查的事情


标签:MyBatis
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号