在用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 c3p00.9.5.2
修改之后的:
c3p0 c3p00.9.1.2
然后就成功运行了.......



