当前位置: 移动技术网 > IT编程>开发语言>Java > Spring通过纯注解配置实现数据库CRUD操作

Spring通过纯注解配置实现数据库CRUD操作

2020年08月01日  | 移动技术网IT编程  | 我要评论
问题引出纯注解配置是相对注解+xml文件练习中,仍然有部分配置需要使用applicationContext.xml,那么能不能使用注解替换掉所有的xml呢?Spring提供了一些新注解,可以达到这个目标。请注意:Spring提供的这部分新注解,并非为了完全代替掉XML,只是提供了另外一种配置方案环境跟上文xml+注解方式配置环境相同修改的地方之一:xml配置文件被核心控制类取代import com.mchange.v2.c3p0.ComboPooledDataSource;import

问题引出
纯注解配置是相对注解+xml文件练习中,仍然有部分配置需要使用applicationContext.xml,那么能不能使用注解替换掉所有的xml呢?Spring提供了一些新注解,可以达到这个目标。

请注意:Spring提供的这部分新注解,并非为了完全代替掉XML,只是提供了另外一种配置方案

在这里插入图片描述
环境跟上文xml+注解方式配置环境相同
修改的地方之一:
xml配置文件被核心控制类取代

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

//表名该类为核心配置类
@Configuration
@ComponentScan("com.gg.dao.impl")
@PropertySource("classpath:db.properties")
public class AppConfig {
    //如果是普通值,可以直接@Value("张三")引入
    //从属性文件中获取值,对于基本数据类型的值,我们定义成员变量然后用value注解引入
    @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注解用于方法上,对方法的返回值进行注解,特别用于第三方jar包的注入
    @Bean("dataSource")
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
    //当方法中需要携带config中已经注入的参数时,需要用Qualifier("beanName")指定
    @Bean("connection")
    public Connection getConnection(@Qualifier("dataSource") DataSource dataSource) throws SQLException {
        Connection connection = dataSource.getConnection();
        return connection;
    }

    //
    @Bean("queryRunner")
    public QueryRunner getQueryRunner(@Qualifier("dataSource") DataSource dataSource){
        QueryRunner queryRunner = new QueryRunner(dataSource);
        return queryRunner;
    }
} 

测试类引入核心控制类有一点区别

import com.gg.dao.impl.AccountService1;
import com.gg.dao.impl.AppConfig;
import com.gg.domain.Account;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.sql.SQLException;

public class AllMappingAccount1 {
    @Test

    public void Test1() throws SQLException {
        //扫描
        AnnotationConfigApplicationContext appConfig = new AnnotationConfigApplicationContext(AppConfig.class);
        //获得指定对象
        AccountService1 accountService = appConfig.getBean("accountService",AccountService1.class);
        //调用方法
        Account account = accountService.findAccountById(1);
        //打印结果
        System.out.println(account);
    }
} 

测试结果
在这里插入图片描述

本文地址:https://blog.csdn.net/lierenbiji21/article/details/108249810

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网