在用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:\Code\SpringCode\SSM\target\classes\com\rwg\mapper\AccountMapper.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-->
<!--扫描 com.rwg 下的全部组件-->
<context:component-scan base-package="com.rwg"></context:component-scan>
<!--掌握 基于 AspectJ 的 注解 的 AOP实现 配置-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--配置文件参数化(参数占位符)-->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!--基本配置-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
然后后面加上这个:
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource ds) throws IOException {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 配置数据源
bean.setDataSource(ds);
return bean;
}
就相当于xml配置文件的这个
<!-- 工厂bean:生成SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源属性-->
<property name="dataSource" ref="dataSource"></property>
<!--mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
问题二:
之后再次运行发现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依赖有问题,版本太低了
原来的:
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
修改之后的:
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
然后就成功运行了.......