-- 创建数据库 CREATE DATABASE mydatabase; -- 使用数据库 USE mydatabase; -- 创建账户表 CREATE TABLE account( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(40), money FLOAT )CHARACTER SET utf8 COLLATE utf8_general_ci; -- 插入数据 INSERT INTO account(name,money) VALUES('aaa',1000); INSERT INTO account(name,money) VALUES('bbb',1000); INSERT INTO account(name,money) VALUES('ccc',1000);
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_02_crud_xml</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
public class Account implements Serializable { private Integer id; private String name; private Float money; // setter/getter/构造略 }
public interface AccountDao { /** * 查询所有 * @return */ List<Account> findAll(); /** * 根据ID查询账户 * @return */ Account findAccountById(Integer id); /** * 保存账户 * @param account */ void saveAccount(Account account); /** * 更新账户 * @param account */ void updateAccount(Account account); /** * 根据ID删除指定账户 * @param id */ void deleteAccount(Integer id); }
/** * <!-- 注册AccountDao对象 --> * <bean id="accountDao" class="com.itcast.dao.impl.AccountDaoImpl"> * <!-- 注入QueryRunner对象 --> * <!-- 由于在本案例中QueryRunner对象为必须存在的对象,所以使用构造注入防止存在为空的情况(构造注入为强制注入,不允许不注入) --> * <constructor-arg name="queryRunner" ref="queryRunner"></constructor-arg> * </bean> */ @Repository(value = "accountDao") public class AccountDaoImpl implements AccountDao { @Resource(name = "queryRunner") private QueryRunner queryRunner; public AccountDaoImpl(QueryRunner queryRunner) { this.queryRunner = queryRunner; } public List<Account> findAll() { try { return queryRunner.query("SELECT * FROM account", new BeanListHandler<Account>(Account.class)); }catch (Exception e) { throw new RuntimeException(e); } } public Account findAccountById(Integer id) { try { return queryRunner.query("SELECT * FROM account WHERE id = ?", new BeanHandler<Account>(Account.class), id); }catch (Exception e) { throw new RuntimeException(e); } } 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); } } 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); } } public void deleteAccount(Integer id) { try { queryRunner.update("DELETE FROM account WHERE id = ?", id); }catch (Exception e) { throw new RuntimeException(e); } } }
public interface AccountService { /** * 查询所有 * @return */ List<Account> findAll(); /** * 根据ID查询账户 * @param id * @return */ Account findAccountById(Integer id); /** * 保存账户 * @param account */ void saveAccount(Account account); /** * 更新账户 * @param account */ void updateAccount(Account account); /** * 根据ID删除指定账户 * @param id */ void deleteAccount(Integer id); }
/** * <!-- 注册AccountService对象 --> * <bean id="accountService" class="com.itcast.service.impl.AccountServiceImpl"> * <!-- 注入AccountDao对象 --> * <property name="accountDao" ref="accountDao"></property> * </bean> */ @Service(value = "accountService") public class AccountServiceImpl implements AccountService { @Resource(name = "accountDao") private AccountDao accountDao; public AccountServiceImpl() { } // 使用注解注入时不需要相对应的setter方法 // public AccountDao getAccountDao() { // return accountDao; // } // // public void setAccountDao(AccountDao accountDao) { // this.accountDao = accountDao; // } public List<Account> findAll() { return this.accountDao.findAll(); } public Account findAccountById(Integer id) { return this.accountDao.findAccountById(id); } public void saveAccount(Account account) { this.accountDao.saveAccount(account); } public void updateAccount(Account account) { this.accountDao.updateAccount(account); } public void deleteAccount(Integer id) { this.accountDao.deleteAccount(id); } }
/** * @Configuration: * 作用:将一个类声明为一个配置类,作用等同于配置了一个applicationContext.xml * * @ComponentScan: * 作用:等同于在xml中做了如下配置: * <!-- 告知Spring容器需要扫描的包及其子包 --> * <context:component-scan base-package="com.itcast"></context:component-scan> * 属性: * value:告知Spring容器需要扫描的包及其子包 * * @Bean * 作用:将方法返回的Bean对象存放到Spring的IoC容器中,等同于在xml做了如下配置: * <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> * <!-- 注入数据源对象,此处使用c3p0的数据源 --> * <constructor-arg name="ds" ref="datasource"></constructor-arg> * </bean> * * 属性: * value:Bean的id默认情况下为方法名,可以通过@Bean注解的value属性指定Bean的id * */ @Configuration @ComponentScan(value = {"com.itcast"}) @Import(value = JdbcConfig.class) public class MyConfigration { }
/** * @Configuration: * 作用:将一个类声明为一个配置类,作用等同于配置了一个applicationContext.xml * * @ComponentScan: * 作用:等同于在xml中做了如下配置: * <!-- 告知Spring容器需要扫描的包及其子包 --> * <context:component-scan base-package="com.itcast"></context:component-scan> * 属性: * value:告知Spring容器需要扫描的包及其子包 * * @Bean * 作用:将方法返回的Bean对象存放到Spring的IoC容器中,等同于在xml做了如下配置: * <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> * <!-- 注入数据源对象,此处使用c3p0的数据源 --> * <constructor-arg name="ds" ref="datasource"></constructor-arg> * </bean> * * 属性: * value:Bean的id默认情况下为方法名,可以通过@Bean注解的value属性指定Bean的id * */ @Configuration public class JdbcConfig { /** * 用于创建QueryRunner对象 * @param dataSource * @return */ @Bean(value = "queryRunner") public QueryRunner createQueryRunner(DataSource dataSource) { return new QueryRunner(dataSource); } /** * 创建数据源对象 * @return */ @Bean(value = "dataSource") public DataSource createDataSource() { try { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydatabase"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); return comboPooledDataSource; }catch (Exception e) { throw new RuntimeException(e); } } }
public class AccountServiceTest { @Test public void testFindAll() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); List<Account> accounts = accountService.findAll(); for (Account account : accounts) { System.out.println(account); } } @Test public void testFindAccountById() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); Account account = accountService.findAccountById(1); System.out.println(account); } @Test public void testSaveAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); Account account = new Account(); account.setName("ddd"); account.setMoney(1000f); accountService.saveAccount(account); } @Test public void testUpdateAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); Account account = accountService.findAccountById(7); account.setMoney(2000f); accountService.updateAccount(account); } @Test public void testDeleteAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); accountService.deleteAccount(7); } }
testFindAll()的测试结果如下:
entity{id=1, name='aaa', money=1000.0} entity{id=2, name='bbb', money=1000.0} entity{id=3, name='ccc', money=1000.0}
testFindAccountById()的测试结果如下:
entity{id=1, name='aaa', money=1000.0}
testSaveAccount()的测试结果如下:
testUpdateAccount()的测试结果如下:
testDeleteAccount()的测试结果如下: