pom.xml文件
< dependencies>
< dependency>
< groupId> org. springframework< / groupId>
< artifactId> spring- context< / artifactId>
< version> 5.1 .5 . RELEASE< / version>
< / dependency>
< dependency>
< groupId> org. springframework< / groupId>
< artifactId> spring- test< / artifactId>
< version> 5.1 .5 . RELEASE< / version>
< / dependency>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< version> 5.1 .46 < / version>
< / dependency>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.12 < / version>
< / dependency>
< dependency>
< groupId> c3p0< / groupId>
< artifactId> c3p0< / artifactId>
< version> 0.9 .5 .5 < / version>
< / dependency>
< dependency>
< groupId> commons- dbutils< / groupId>
< artifactId> commons- dbutils< / artifactId>
< version> 1.7 < / version>
< / dependency>
< / dependencies>
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">
< ! -- 配置Service -- >
< bean id= "accountService" class= "com.itheima.service.impl.AccountServiceImpl" >
< ! -- 注入dao -- >
< property name= "accountDao" ref= "accountDao" > < / property>
< / bean>
< ! -- 配置Dao对象-- >
< bean id= "accountDao" class= "com.itheima.dao.impl.AccountDaoImpl" >
< ! -- 注入QueryRunner -- >
< property name= "runner" ref= "runner" > < / property>
< / bean>
< ! -- 配置QueryRunner-- >
< 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= "1234" > < / property>
< / bean>
< / beans>
实体类
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
}
持久层接口
public interface IAccountDao {
List< Account> findAllAccount ( ) ;
Account findAccountById ( Integer accountId) ;
void saveAccount ( Account account) ;
void updateAccount ( Account account) ;
void deleteAccount ( Integer acccountId) ;
}
持久层实现类
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner ( QueryRunner runner) {
this . runner = runner;
}
@Override
public List< Account> findAllAccount ( ) {
try {
return runner. query ( "select * from account" , new BeanListHandler < Account> ( Account. class ) ) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
@Override
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) ;
}
}
@Override
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) ;
}
}
@Override
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) ;
}
}
@Override
public void deleteAccount ( Integer accountId) {
try {
runner. update ( "delete from account where id=?" , accountId) ;
} catch ( Exception e) {
throw new RuntimeException ( e) ;
}
}
}
业务层接口
public interface IAccountService {
List< Account> findAllAccount ( ) ;
Account findAccountById ( Integer accountId) ;
void saveAccount ( Account account) ;
void updateAccount ( Account account) ;
void deleteAccount ( Integer acccountId) ;
}
业务层实现类
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao ( IAccountDao accountDao) {
this . accountDao = accountDao;
}
@Override
public List< Account> findAllAccount ( ) {
return accountDao. findAllAccount ( ) ;
}
@Override
public Account findAccountById ( Integer accountId) {
return accountDao. findAccountById ( accountId) ;
}
@Override
public void saveAccount ( Account account) {
accountDao. saveAccount ( account) ;
}
@Override
public void updateAccount ( Account account) {
accountDao. updateAccount ( account) ;
}
@Override
public void deleteAccount ( Integer acccountId) {
accountDao. deleteAccount ( acccountId) ;
}
}
测试类
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( locations = "classpath:bean.xml" )
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testFindAll ( ) {
List< Account> accounts = as. findAllAccount ( ) ;
for ( Account account : accounts) {
System. out. println ( account) ;
}
}
@Test
public void testFindOne ( ) {
Account account = as. findAccountById ( 1 ) ;
System. out. println ( account) ;
}
@Test
public void testSave ( ) {
Account account = new Account ( ) ;
account. setName ( "test" ) ;
account. setMoney ( 12345f ) ;
as. saveAccount ( account) ;
}
@Test
public void testUpdate ( ) {
Account account = as. findAccountById ( 4 ) ;
account. setMoney ( 23456f ) ;
as. updateAccount ( account) ;
}
@Test
public void testDelete ( ) {
as. deleteAccount ( 4 ) ;
}
}