dao层:
1 package cn.mepu.dao.imp; 2 3 import cn.mepu.dao.AccountDao; 4 import cn.mepu.domain.Account; 5 import org.apache.commons.dbutils.QueryRunner; 6 import org.apache.commons.dbutils.handlers.BeanHandler; 7 import org.apache.commons.dbutils.handlers.BeanListHandler; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Repository; 10 11 import java.util.List; 12 13 /** 14 * @author shkstart 15 * @create 2019-11-08 10:28 16 */ 17 @Repository("accountDao") 18 public class AccountDaoImp implements AccountDao { 19 20 @Autowired 21 private QueryRunner runner; 22 23 24 @Override 25 public List<Account> findAllAccount() { 26 try { 27 return runner.query("select * from account",new BeanListHandler<Account>(Account.class)); 28 } catch (Exception e) { 29 throw new RuntimeException(e); 30 } 31 } 32 33 @Override 34 public Account findAccountById(Integer accountId) { 35 try { 36 return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId); 37 } catch (Exception e) { 38 throw new RuntimeException(e); 39 } 40 } 41 42 @Override 43 public void saveAccount(Account acc) { 44 try { 45 runner.update("insert into account(name,money) values(?,?)" , acc.getName(),acc.getMoney()); 46 } catch (Exception e) { 47 throw new RuntimeException(e); 48 } 49 } 50 51 @Override 52 public void updateAccount(Account acc) { 53 try { 54 runner.update("update account set name=? , money=? where id = ? " , acc.getName(),acc.getMoney(),acc.getId()); 55 } catch (Exception e) { 56 throw new RuntimeException(e); 57 } 58 } 59 60 @Override 61 public void deleteAccount(Integer accountId) { 62 try { 63 runner.update("delete from account where id = ? " , accountId ); 64 } catch (Exception e) { 65 throw new RuntimeException(e); 66 } 67 } 68 }
service层:
1 package cn.mepu.service.imp; 2 3 4 import cn.mepu.dao.AccountDao; 5 import cn.mepu.domain.Account; 6 import cn.mepu.service.AccountService; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9 10 import java.util.List; 11 12 /** 13 * @author shkstart 14 * @create 2019-11-08 10:12 15 */ 16 @Service("accountService") 17 public class AccountServiceImp implements AccountService { 18 @Autowired 19 private AccountDao accountDao; 20 21 @Override 22 public List<Account> findAllAccount() { 23 return accountDao.findAllAccount(); 24 } 25 26 @Override 27 public Account findAccountById(Integer accountId) { 28 return accountDao.findAccountById(accountId); 29 } 30 31 @Override 32 public void saveAccount(Account acc) { 33 accountDao.saveAccount(acc); 34 } 35 36 @Override 37 public void updateAccount(Account acc) { 38 accountDao.updateAccount(acc); 39 } 40 41 @Override 42 public void deleteAccount(Integer accountId) { 43 accountDao.deleteAccount(accountId); 44 } 45 }
servlet层:
1 package cn.mepu.service; 2 3 import cn.mepu.domain.Account; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import java.util.List; 9 10 /** 11 * @author shkstart 12 * @create 2019-11-08 10:45 13 */ 14 public class AccountServiceTest { 15 @Test 16 public void testFindAll(){ 17 //1.获取容器 18 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 19 //2.得到业务层对象 20 AccountService service = (AccountService) ac.getBean("accountService"); 21 //3.执行方法 22 List<Account> accounts = service.findAllAccount(); 23 for (Account account : accounts) { 24 System.out.println("account = " + account); 25 } 26 27 } 28 29 @Test 30 public void testFindOne(){ 31 //1.获取容器 32 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 33 //2.得到业务层对象 34 AccountService service = (AccountService) ac.getBean("accountService"); 35 //3.执行方法 36 Account account = service.findAccountById(1); 37 System.out.println(account); 38 } 39 40 @Test 41 public void testSave(){ 42 //1.获取容器 43 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 44 //2.得到业务层对象 45 AccountService service = (AccountService) ac.getBean("accountService"); 46 //3.执行方法 47 service.saveAccount(new Account(1,"DDD",1234)); 48 } 49 50 @Test 51 public void testUpdate(){ 52 //1.获取容器 53 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 54 //2.得到业务层对象 55 AccountService service = (AccountService) ac.getBean("accountService"); 56 //3.执行方法 57 service.updateAccount(new Account(1,"DDD",2345)); 58 } 59 60 @Test 61 public void testDelete(){ 62 //1.获取容器 63 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 64 //2.得到业务层对象 65 AccountService service = (AccountService) ac.getBean("accountService"); 66 //3.执行方法 67 service.deleteAccount(4); 68 } 69 }
bean.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!--告知spring加载容器时扫描要扫描的包配置所需要的约束不在beans中,而是称为context名称空间的约束中--> 10 <context:component-scan base-package="cn.mepu"></context:component-scan> 11 <!-- 配置注入QueryRunner scope保证线程安全--> 12 <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> 13 <!-- 注入数据源--> 14 <constructor-arg name="ds" ref="dataSource"></constructor-arg> 15 </bean> 16 17 <!-- 配置数据源--> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <!-- 连接数据的必备信息--> 20 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 21 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/javaee"></property> 22 <property name="user" value="root"></property> 23 <property name="password" value="root"></property> 24 </bean> 25 26 </beans>