浅谈Spring之用纯注解方式整合Dbutils

要想使用纯注解开发,还需了解以下几个注解

  1. @Configuration:相当于applicationContext.xml,将此类作为xml配置文件
  2. @ComponentScan:相当于<context:component-scan base-package=""/>开启包扫描
  3. @PropertySource:相当于<context:property-placeholder location=""/>,导入外部配置文件
  4. @Bean:将第三方类(对象)交给ioc容器,让他去为需要的地方注入此对象
  5. @Import:相当于<import resource=""/>引入另一个xml配置

Spring配置类

@Configuration // applicationContext.xml
@ComponentScan(value = "com.lepeng") // <context:component-scan base-package=""/>
@PropertySource("classpath:jdbc.properties")// <context:property-placeholder location=""/>
public class SpringConfig {

    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    // 创建连接池(第三方对象)
    @Bean("dataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }

    // 创建queryRunner(第三方对象)
    @Bean("queryRunner")
    public QueryRunner queryRunner(DataSource dataSource) { //方法形参位置,会自动从ioc容器获取对象实例,完成注入
        QueryRunner queryRunner = new QueryRunner(dataSource);
        return queryRunner;
    }
}

这个类也就对应我们编写的xml配置文件:

    <!--开启注解组件扫描-->
    <context:component-scan base-package="com.lepeng"/>

    <!--导入外部的properties配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--创建连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--创建queryRunner-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

实体类

public class Account {
    private int id;
    private String name;
    private double money;
    //自动生成set和get方法  toString方法
}

dao层

@Repository
public class AccountDaoImpl implements AccountDao {

    //简单的jdbc封装对象,比mybatis框架更轻量级    //参数是连接池对象
    //QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    //声明一个操作数据库的CRUD对象
    @Autowired
    private QueryRunner queryRunner;

    public List<Account> findAll() throws SQLException {
        return queryRunner.query("SELECT * FROM account", new BeanListHandler<Account>(Account.class));
    }
}

service层

@Service
public class AccountServiceImpl implements AccountService {

    //声明dao对象
    @Autowired
    private AccountDao accountDao;

    public List<Account> findAll() throws SQLException {
        return accountDao.findAll();
    }
}

测试类

public class AccountTest {

    @Test
    public void test01() throws Exception {
        // 加载配置文件,初始化spring环境(ioc容器)
        // ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);

        // 获得指定类型对象实例
        AccountService accountService = app.getBean(AccountService.class);
        // 测试
        List<Account> list = accountService.findAll();
        for (Account account : list) {
            System.out.println(account);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值