SSM学习之路——spring第二天_用注解的ioc实现基础crud操作

和前一篇只是有细微的差别,只是将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&amp;characterEncoding=utf8&amp;useUnicode=true"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

四、测试类无需修改,运行成功

发布了23 篇原创文章 · 获赞 0 · 访问量 596

猜你喜欢

转载自blog.csdn.net/SixthMagnitude/article/details/104124339