Spring学习笔记(四)基于xml的IOC案例
1. 创建数据库
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 ) ;
2. 创建账户实体类
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public void setId ( Integer id) {
this . id = id;
}
public void setName ( String name) {
this . name = name;
}
public void setMoney ( Float money) {
this . money = money;
}
public Integer getId ( ) {
return id;
}
public String getName ( ) {
return name;
}
public Float getMoney ( ) {
return money;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}' ;
}
}
3. 创建接口
public interface IAccountDao {
List < Account > findAllAccount ( ) ;
Account findAccountById ( Integer accountId) ;
void saveAccount ( Account account) ;
void updateAccount ( Account account) ;
void deleteAccount ( Integer accountId) ;
}
public interface IAccountService {
List < Account > findAllAccount ( ) ;
Account findAccountById ( Integer accountId) ;
void saveAccount ( Account account) ;
void updateAccount ( Account account) ;
void deleteAccount ( Integer accountId) ;
}
4. 创建实现类
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner ( QueryRunner runner) {
this . runner = runner;
}
public List < Account > findAllAccount ( ) {
try {
return runner. query ( "select * from account" , new BeanListHandler < Account > ( Account . class ) ) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
public Account findAccountById ( Integer accountId) {
try {
return runner. query ( "select * from account where id = ?" , new BeanHandler < Account > ( Account . class ) , accountId) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
public void saveAccount ( Account account) {
try {
runner. update ( "insert into account(name, money) values(?,?)" , account. getName ( ) , account. getMoney ( ) ) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
public void updateAccount ( Account account) {
try {
runner. 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 accountId) {
try {
runner. update ( "delete from account where id = ?" , accountId) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao ( IAccountDao accountDao) {
this . accountDao = accountDao;
}
public List < Account > findAllAccount ( ) {
return accountDao. findAllAccount ( ) ;
}
public Account findAccountById ( Integer accountId) {
return accountDao. findAccountById ( accountId) ;
}
public void saveAccount ( Account account) {
accountDao. saveAccount ( account) ;
}
public void updateAccount ( Account account) {
accountDao. updateAccount ( account) ;
}
public void deleteAccount ( Integer accountId) {
accountDao. deleteAccount ( accountId) ;
}
}
5. 创建测试类
public class AccountServiceTest {
@Test
public void testFindAll ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
IAccountService iAccountService = applicationContext. getBean ( "accountService" , IAccountService . class ) ;
List < Account > accounts = iAccountService. findAllAccount ( ) ;
for ( Account account : accounts) {
System . out. println ( account) ;
}
}
@Test
public void testFindOne ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
IAccountService iAccountService = applicationContext. getBean ( "accountService" , IAccountService . class ) ;
Account account = iAccountService. findAccountById ( 1 ) ;
System . out. println ( account) ;
}
@Test
public void testSave ( ) {
Account account = new Account ( ) ;
account. setName ( "test" ) ;
account. setMoney ( 12345f ) ;
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
IAccountService iAccountService = applicationContext. getBean ( "accountService" , IAccountService . class ) ;
iAccountService. saveAccount ( account) ;
}
@Test
public void testUpdate ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
IAccountService iAccountService = applicationContext. getBean ( "accountService" , IAccountService . class ) ;
Account account = iAccountService. findAccountById ( 4 ) ;
account. setMoney ( 23456f ) ;
iAccountService. updateAccount ( account) ;
}
@Test
public void testDelete ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
IAccountService iAccountService = applicationContext. getBean ( "accountService" , IAccountService . class ) ;
iAccountService. deleteAccount ( 4 ) ;
}
}
6. 配置 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"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id = " accountService" class = " com.greyson.service.impl.AccountServiceImpl" >
< property name = " accountDao" ref = " accountDao" > </ property>
</ bean>
< bean id = " accountDao" class = " com.greyson.dao.impl.AccountDaoImpl" >
< property name = " runner" ref = " runner" > </ property>
</ bean>
< bean id = " runner" 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" > </ property>
< property name = " user" value = " root" > </ property>
< property name = " password" value = " HotteMYSQL" > </ property>
</ bean>
</ beans>