天天看點

淺談Spring之用純注解方式整合DbutilsSpring配置類實體類dao層service層測試類

要想使用純注解開發,還需了解以下幾個注解

  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);
        }
    }
}