本节内容
我们对上一节中实现的项目进行改写,将其修改称为注解的方式进行配置。
但是我们要注意的是由于数据库的相关类都是封装在别人jar包中的类,因此无法改写别人的代码,对这种类型的配置只能通过xml的方式进行配置,或者是我们后续学习的使用config class类进行配置。
bean.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在bean约束中, 而是在一个名称为context的名称空间和约束中--> <context:component-scan base-package="com.lujuan"/> <!--没有办法改--> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> <constructor-arg name="ds" ref = "dataSource"> </constructor-arg> </bean> <!--没有办法改--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"> </property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?characterEncoding=utf8"> </property> <property name="user" value="root"> </property> <property name="password" value="Admin@123"> </property> </bean> </beans>
dao-impl 文件
@Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Resource(name = "queryRunner") private QueryRunner queryRunner;//这是commons.dbutils提供的 public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } public List<Account> findAllAccount() { List<Account> reV = null; try{ //查询之后顺便映射为实体类 reV = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } return reV; } public Account findAccountById(Integer id) { Account reV = null; try{ reV = queryRunner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), id); }catch (Exception e){ throw new RuntimeException(e); } return reV; } public void addAccount(Account account) { try{ queryRunner.update("insert into account(name, money) values (?, ?)", account.getName(), account.getMoney()); }catch (Exception e){ throw new RuntimeException(e); } } public void deleteAccountById(Integer id) { try{ queryRunner.update("delete from account where id = ?", id); }catch (Exception e){ throw new RuntimeException(e); } } public void updateAccount(Account account) { try{ queryRunner.update("update account set name= ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId()); }catch (Exception e){ throw new RuntimeException(e); } } }
主要是增加了上面两行内容,主要是bean定义了依赖注入
service-impl修改
@Service(value="accountService") public class AccountServiceImpl implements IAccountService { @Resource(name = "accountDao") private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } public List<Account> findAllAccount() { return accountDao.findAllAccount(); } public Account findAccountById(Integer id) { return accountDao.findAccountById(id); } public void addAccount(Account account) { accountDao.addAccount(account); } public void deleteAccountById(Integer id) { accountDao.deleteAccountById(id); } public void updateAccount(Account account) { accountDao.updateAccount(account); } public void saveAccount() { System.out.println("AccountServiceImpl: saveAccount"); } @PostConstruct public void init(){ System.out.println("init......"); } @PreDestroy public void destroy(){ System.out.println("destroy......"); } }
主要bean定义和依赖注入。
至此更改还是很简单的。
本节完结。