和前一篇只是有细微的差别,只是将bean创建对象改为了注解创建对象
上一篇使用xml实现基础crud操作
一、对AccountDaoImpl修改
用注解注入,无需set方法
二、对AccountServiceImpl修改
用注解注入,无需set方法
三、修改bean.xml文件
约束需要修改,并使用<context:component-scan base-package="com.itheima"></context:component-scan>
指定使用注解创建对象需要扫描的包
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--创建QueryRunner对象-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--由于queryRunner对象用dataSource来初始化数据库连接,这里注入含参构造函数-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springdb?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>