标签:auth style local rom jdbc lis import 加载 turn
实体类:
1 package cn.mepu.domain; 2 3 import java.io.Serializable; 4 5 /** 6 * 账户实体类 7 * @author shkstart 8 * @create 2019-11-08 10:04 9 */ 10 public class Account implements Serializable { 11 private Integer id; 12 private String name; 13 private float money; 14 15 public Account(Integer id, String name, float money) { 16 this.id = id; 17 this.name = name; 18 this.money = money; 19 } 20 21 public Account() { 22 } 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public float getMoney() { 41 return money; 42 } 43 44 public void setMoney(float money) { 45 this.money = money; 46 } 47 48 @Override 49 public String toString() { 50 return "Account{" + 51 "id=" + id + 52 ", name=‘" + name + ‘\‘‘ + 53 ", money=" + money + 54 ‘}‘; 55 } 56 }
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 9 import java.util.List; 10 11 /** 12 * @author shkstart 13 * @create 2019-11-08 10:28 14 */ 15 public class AccountDaoImp implements AccountDao { 16 17 private QueryRunner runner; 18 19 public void setRunner(QueryRunner runner) { 20 this.runner = runner; 21 } 22 23 @Override 24 public List<Account> findAllAccount() { 25 try { 26 return runner.query("select * from account",new BeanListHandler<Account>(Account.class)); 27 } catch (Exception e) { 28 throw new RuntimeException(e); 29 } 30 } 31 32 @Override 33 public Account findAccountById(Integer accountId) { 34 try { 35 return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId); 36 } catch (Exception e) { 37 throw new RuntimeException(e); 38 } 39 } 40 41 @Override 42 public void saveAccount(Account acc) { 43 try { 44 runner.update("insert into account(name,money) values(?,?)" , acc.getName(),acc.getMoney()); 45 } catch (Exception e) { 46 throw new RuntimeException(e); 47 } 48 } 49 50 @Override 51 public void updateAccount(Account acc) { 52 try { 53 runner.update("update account set name=? , money=? where id = ? " , acc.getName(),acc.getMoney(),acc.getId()); 54 } catch (Exception e) { 55 throw new RuntimeException(e); 56 } 57 } 58 59 @Override 60 public void deleteAccount(Integer accountId) { 61 try { 62 runner.update("delete from account where id = ? " , accountId ); 63 } catch (Exception e) { 64 throw new RuntimeException(e); 65 } 66 } 67 }
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 8 import java.util.List; 9 10 /** 11 * @author shkstart 12 * @create 2019-11-08 10:12 13 */ 14 15 public class AccountServiceImp implements AccountService { 16 17 private AccountDao dao; 18 19 public void setDao(AccountDao dao) { 20 this.dao = dao; 21 } 22 23 @Override 24 public List<Account> findAllAccount() { 25 return dao.findAllAccount(); 26 } 27 28 @Override 29 public Account findAccountById(Integer accountId) { 30 return dao.findAccountById(accountId); 31 } 32 33 @Override 34 public void saveAccount(Account acc) { 35 dao.saveAccount(acc); 36 } 37 38 @Override 39 public void updateAccount(Account acc) { 40 dao.updateAccount(acc); 41 } 42 43 @Override 44 public void deleteAccount(Integer accountId) { 45 dao.deleteAccount(accountId); 46 } 47 }
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:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring加载容器时扫描要扫描的包配置所需要的约束不在beans中,而是称为context名称空间的约束中--> <!-- 配置service--> <bean id="accountService" class="cn.mepu.service.imp.AccountServiceImp"> <!-- 注入dao--> <property name="dao" ref="accountDao"></property> </bean> <!-- 配置dao--> <bean id="accountDao" class="cn.mepu.dao.imp.AccountDaoImp"> <!-- 注入QueryRunner runner--> <property name="runner" ref="runner"></property> </bean> <!-- 配置注入QueryRunner scope保证线程安全--> <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/javaee"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> </beans>
标签:auth style local rom jdbc lis import 加载 turn
原文地址:https://www.cnblogs.com/aikang525/p/11819167.html