我们还需要一个实体类用于封装查询结果,简单起见,省略了get和set方法,以及toString方法:
public class Account implements Serializable { private Integer id; private String name; private float money; //还有set方法和get方法,以及toString方法没有显示 } 2.编写接口实现类 public class AccountDaoImpl implements IAccountDao { private QueryRunner queryRunner; //由于需要通过配置文件对QueryRunner进行注入,所以需要提供set方法 public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } @Override public List<Account> findAllAccounts() { try { return queryRunner.query("select * from account", new BeanListHandler<>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } } @Override public Account findAccountById(Integer id) { try { return queryRunner.query("select * from account where id = ?", new BeanHandler<>(Account.class), id); }catch (Exception e){ throw new RuntimeException(e); } } @Override public void saveAccount(Account account) { try { queryRunner.update("insert into account(name, money) values (?,?)", account.getName(), account.getMoney()); }catch (Exception e){ throw new RuntimeException(e); } } @Override 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); } } @Override public void deleteAccountById(Integer id) { try { queryRunner.update("delete from account s where id = ?", id); }catch (Exception e){ throw new RuntimeException(e); } } } public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; //set方法用于注入 public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } @Override public List<Account> findAllAccounts() { return accountDao.findAllAccounts(); } @Override public Account findAccountById(Integer id) { return accountDao.findAccountById(id); } @Override public void saveAccount(Account account) { accountDao.saveAccount(account); } @Override public void updateAccount(Account account) { accountDao.updateAccount(account); } @Override public void deleteAccountById(Integer id) { accountDao.deleteAccountById(id); } } 3.编写配置文件前面的部分都不用太关心,只需要了解即可。QueryRunner是java编程中的数据库操作实用工具类DBUtils中的一个核心类,可以用于连接数据库执行SQL语句。beans.xml文件是Spring的配置文件,重点应该放在配置文件上。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!-- 配置service --> <bean id = "accountService"> <!-- 注入dao --> <property ref="accountDao"></property> </bean> <!-- 配置dao --> <bean id = "accountDao"> <!-- 注入queryRunner --> <property ref="queryRunner"></property> </bean> <!-- 配置queryRunner,必须是多例对象,否则会出现线程安全问题 --> <bean id = "queryRunner" scope="prototype"> <!-- 注入数据源 --> <constructor-arg ref="dataSource"></constructor-arg> </bean> <!-- 配置数据源 --> <bean> <!-- 配置连接数据库的必备信息 --> <property value="com.mysql.cj.jdbc.Driver"></property> <property value="jdbc:mysql://localhost:3306/spring"></property> <property value="root"></property> <property value="12345678"></property> </bean> </beans>配置文件中需要注意的是,因为每一个连接都会用到QueryRunner对象,所以必须是多例模式,否则就会出现线程安全问题。此外,在配置数据源时,property中的value可以从其他文件中读取。该配置文件,基本上综合了之前讲的用xml进行依赖注入。
例如:
首先在src/main/resources目录下编写:jdbc.properties文件
jdbc.driver = com.mysql.cj.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/spring jdbc.user = root jdbc.password = 12345678然后对beans.xml文件进行修改:
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean> <!-- 配置连接数据库的必备信息 --> <property value="${jdbc.driver}"></property> <property value="${jdbc.url}"></property> <property value="${jdbc.user}"></property> <property value="${jdbc.password}"></property> </bean>