spring案列使用xml方式和注解方式实现单表的CURD操作02_3.2

一,注解方式实现单表的CURD操作

我们先来看看项目目录:
在这里插入图片描述
可以看到之前的bean.xml配置文件没有了而是多了一个配置类SpringConfiguration
这里的源代码也就是在之前xml方式上做了一点点修改,我们来看下:
在这里插入图片描述

上面两个配置我们可以用下面两个注解配置代替:

在这里插入图片描述

在这里插入图片描述
可以看到两个注解;
我们之前说过compent,它是用于创建对象的。它的作用是用于把当前类对象存入spring容器中
下面这三个也是
在这里插入图片描述
而Autowired是自动注入。

重点是bean.xml配置里的那些相应配置我们如何使用注解来代替:

在这里插入图片描述
我们先新建一个配置类(该类是一个配置类,它的作用和bean.xml是一样的):
之后我们需要用到两个新注解:
Configuration 作用:指定当前类为配置类
ComponentScan 作用:用于通过注解指定spring在创建容器时要扫描的包
属性:

  •       value:它和basePackges的作用是一样的。都是用于指定创建容器时要扫描的包。   我们使用此注解,就等同于在xml中配置了:     
    
  •                    <context:component-scan base-package="com.itheima" ></context:component-scan>
    

在这里插入图片描述
之后就是如何在实体类中配置runner,还有就是这时我们这个QueryRunner是单列不再是多列
我们需要用到一个新注解Bean
在这里插入图片描述
在这里插入图片描述

当我们在编写QueryRunner不写Bean注解时,它和bean.xml里面的效果不是一样的,我们需要用于把当前方法的返回值作为bean对象存入spring的ioc容器中。将他放入spring的容器中去就必须用到这个注解
Bean:

  • 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中。
  • 属性:
  •   name:用于指定bean的id,当不写时,默认值是当前方法的名称
    
  • 细节:
  •   当我们使用注解配置时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
    
  •   查找的方式和Autowired注解的作用是一样的
    

最后就是数据源的配置:
我们也是需要用到Bean这个注解:
在这里插入图片描述
在这里插入图片描述

要源码的…
现在我们需要去修改一下代码关于连接数据源的:
可以看到上面我们写的是固定的配置,那当我们要修改的时候就会比较麻烦
这时我们需要新建一个properties配置文件
在里面编写我们的连接数据库代码
jdbcConfig.properties

jdbc.driver =com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/eesy
jdbc.username = root
jdbc.password = 18205071

之后我们需要修改我们的配置类SpringConfiguration

@ComponentScan(basePackages = "com.itheima")   //注意这里的路径是类路径
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {

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

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

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

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

    /**
     * 用于创建一个 QueryRunner对象
     * @param dataSource
     * @return
     *
     */
    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return  new QueryRunner(dataSource);
    }

    /**
     * 创建数据源对象
     * @return
     */
    @Bean("dataSource")
    public  DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(drvier);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return  ds;
        } catch (Exception e) {
            throw  new RuntimeException(e);
        }

    }

}

这样就可以了,这篇写的有点不好理解,建议去看视频学习

我们来看看测试代码部分:
原来是:

package com.itheima.test;

import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import config.SpringConfiguration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class AccountServiceTest {
    @Test
    public void  testFindAll(){
        //1,获取容器
  //      ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //2,得到业务层对象
        IAccountService as= ac.getBean("accountService",IAccountService.class);
        //3,执行方法
        List<Account> accounts = as.findAllAccount();
        for (Account account : accounts) {
            System.out.println(account);
        }


    }
    @Test
    public void  testFindOne(){}
    @Test
    public void  testSave(){}
    @Test
    public void  testUpdate(){
        //1,获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2,得到业务层对象
        IAccountService as= ac.getBean("accountService",IAccountService.class);
        Account account = as.findAccountById(3);
        account.setMoney(5000f);
        as.updateAccount(account);
    }
    @Test
    public void  testDelete(){
        //1,获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2,得到业务层对象
        IAccountService as= ac.getBean("accountService",IAccountService.class);
        as.deleteAccount(3);
    }
}

我们可以看到有几行都是重复的代码,这时我们可以想办法把这些重复代码修改一下。
用Autowired注解会报错,那么这是为什么?不是说可以自动注入吗
在这里插入图片描述

在这里插入图片描述
那么我们应该还要用 用哪个注解呢?
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

spring整合junit配置

  •  1,导入spring整合junit的jar(坐标)
    
  •  2,使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的。
    
  •         @Runwith
    
  •  3,告知spring的运行器,spring的ioc创建是基于xml还注解的,并且说明位置
    
  •       @ContextConfiguration
    
  •         locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
    
  •         classes:指定配置类所在的位置
    
  •  细节:当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值