栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring和mybatis纯注解形式整合遇到的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring和mybatis纯注解形式整合遇到的问题

        在用xml文件配置之后感觉过于麻烦, 然后研究了一下纯注解的方式来实现, 然后遇到了一些问题, 下面将问题列出(由于自己是小白, 所述术语不当, 还望谅解指正):

问题一:

        org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService': Unsatisfied dependency expressed through field 'accountMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountMapper' defined in file [E:CodeSpringCodeSSMtargetclassescomrwgmapperAccountMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

之后发现是config配置文件没有写mybatis核心工厂

原config的配置文件:

@Configuration
@ComponentScan(basePackages = {"com.rwg"})
@MapperScan("com.rwg.mapper")
@PropertySource("classpath:db.properties")
public class Configation {

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource getDataSource() throws PropertyVetoException {

        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);

        return dataSource;
    }

}

相当于原xml文件的这个

    
    
    

    
    

    
    

    
        
        
        
        
        
    

然后后面加上这个:

@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource ds) throws IOException {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        
        // 配置数据源
        bean.setDataSource(ds);
        
        return bean;
    }

就相当于xml配置文件的这个

    
    
        
        
        
        
    

问题二:

之后再次运行发现sql语句只执行了一条

log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2021-10-30_19:40:34.556 DEBUG [main] [c.r.m.A.reduceMoney]:159 - ==>  Preparing: update t_account set money = money - ? where userId = ? 
2021-10-30_19:40:34.592 DEBUG [main] [c.r.m.A.reduceMoney]:159 - ==> Parameters: 100.0(Double), 10(Integer)
2021-10-30_19:40:34.630 DEBUG [main] [c.r.m.A.reduceMoney]:159 - <==    Updates: 1

java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract

然后搜索之后发现自己pom.xml的c3p0依赖有问题,版本太低了

原来的:


    com.mchange
    c3p0
    0.9.5.2

修改之后的:


    c3p0
    c3p0
    0.9.1.2

然后就成功运行了.......

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/361294.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号