标签:mys enc nts pre junit date ice row test
()前期工作,在mysql中创建数据库相关
create database cong use cong; 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);
1.创建maven工程
2.在pom.xml中添加相关依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.cong</groupId> 8 <artifactId>spring_person_ioc_xml</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>jar</packaging> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-context</artifactId> 15 <version>5.0.2.RELEASE</version> 16 </dependency> 17 <!-- dbutils的依赖 --> 18 <dependency> 19 <groupId>commons-dbutils</groupId> 20 <artifactId>commons-dbutils</artifactId> 21 <version>1.4</version> 22 </dependency> 23 <dependency> 24 <groupId>mysql</groupId> 25 <artifactId>mysql-connector-java</artifactId> 26 <version>5.1.6</version> 27 </dependency> 28 <!-- 连接池的依赖 --> 29 <dependency> 30 <groupId>c3p0</groupId> 31 <artifactId>c3p0</artifactId> 32 <version>0.9.1.2</version> 33 </dependency> 34 <dependency> 35 <groupId>junit</groupId> 36 <artifactId>junit</artifactId> 37 <version>4.10</version> 38 </dependency> 39 </dependencies> 40 </project>
3.在java目录下创建com.cong.pojo.Account类
1 package com.cong.pojo; 2 3 public class Account { 4 private int id; 5 private String name; 6 private float money; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public float getMoney() { 25 return money; 26 } 27 28 public void setMoney(float money) { 29 this.money = money; 30 } 31 32 @Override 33 public String toString() { 34 return "Account{" + 35 "id=" + id + 36 ", name=‘" + name + ‘\‘‘ + 37 ", money=" + money + 38 ‘}‘; 39 } 40 }
4.在java目录下创建com.cong.dao.AccountDao接口
1 package com.cong.dao; 2 3 import com.cong.pojo.Account; 4 5 import java.util.List; 6 7 public interface AccountDao { 8 List<Account> findAllAccount();//find all 9 Account findAccountById(int id);//find one 10 void saveAccount(Account account);//save 11 void updateAccount(Account account);//update 12 void deleteAccount(int id);//delete 13 }
5.在dao目录下创建实现类AccountDaoImpl
1 package com.cong.dao; 2 3 import com.cong.pojo.Account; 4 import org.apache.commons.dbutils.QueryRunner; 5 import org.apache.commons.dbutils.handlers.BeanHandler; 6 import org.apache.commons.dbutils.handlers.BeanListHandler; 7 8 import java.util.List; 9 10 public class AccountDaoImpl implements AccountDao { 11 private QueryRunner runner; 12 13 public void setRunner(QueryRunner runner) { 14 this.runner = runner; 15 } 16 17 @Override 18 public List<Account> findAllAccount() { 19 try { 20 return runner.query("select * from account", new BeanListHandler<Account>(Account.class)); 21 } catch (Exception e) { 22 throw new RuntimeException(e); 23 } 24 } 25 26 @Override 27 public Account findAccountById(int id) { 28 try{ 29 return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),id); 30 }catch (Exception e) { 31 throw new RuntimeException(e); 32 } 33 } 34 35 @Override 36 public void saveAccount(Account account) { 37 try { 38 runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney()); 39 } catch (Exception e) { 40 throw new RuntimeException(e); 41 } 42 } 43 44 @Override 45 public void updateAccount(Account account) { 46 try { 47 runner.update("update account set name = ?,money = ? where id =?",account.getName(),account.getMoney(),account.getId()); 48 } catch (Exception e) { 49 throw new RuntimeException(e); 50 } 51 } 52 53 @Override 54 public void deleteAccount(int id) { 55 try { 56 runner.update("delete from account where id = ?",id); 57 } catch (Exception e) { 58 throw new RuntimeException(e); 59 } 60 } 61 }
6.在java目录下创建com.cong.service.AccountService接口
1 package com.cong.service; 2 3 import com.cong.pojo.Account; 4 5 import java.util.List; 6 7 public interface AccountService { 8 List<Account> findAllAccount();//find all 9 Account findAccountById(int id);//find one 10 void saveAccount(Account account);//save 11 void updateAccount(Account account);//update 12 void deleteAccount(int id);//delete 13 }
7.在service目录创建实现类AccountServiceImpl
1 package com.cong.service; 2 3 import com.cong.dao.AccountDao; 4 import com.cong.pojo.Account; 5 6 import java.util.List; 7 public class AccountServiceImpl implements AccountService { 8 //业务层都是调用持久层的,所以需要有一个持久层的对象 9 private AccountDao accountDao; 10 11 public void setAccountDao(AccountDao accountDao) { 12 this.accountDao = accountDao; 13 } 14 15 @Override 16 public List<Account> findAllAccount() { 17 return accountDao.findAllAccount(); 18 } 19 20 @Override 21 public Account findAccountById(int id) { 22 return accountDao.findAccountById(id); 23 } 24 25 @Override 26 public void saveAccount(Account account) { 27 accountDao.saveAccount(account); 28 } 29 30 @Override 31 public void updateAccount(Account account) { 32 accountDao.updateAccount(account); 33 } 34 35 @Override 36 public void deleteAccount(int id) { 37 accountDao.deleteAccount(id); 38 } 39 }
8.在resources下面创建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.cong.service.AccountServiceImpl"> <!-- 注入dao --> <property name="accountDao" ref="accountDao"></property> </bean> <!--配置Dao对象--> <bean id="accountDao" class="com.cong.dao.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/cong"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> </beans>
9.在test.java目录下创建Test类进行单元测试
1 import com.cong.pojo.Account; 2 import com.cong.service.AccountService; 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 import java.util.List; 8 9 public class AccountServiceTest { 10 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 11 AccountService as = (AccountService) context.getBean("accountService"); 12 @Test 13 public void testFindAll(){16 List<Account> list = as.findAllAccount(); 17 for (Account account : list) { 18 System.out.println(account.toString()); 19 } 20 } 21 @Test 22 public void testFindOne(){25 Account account = as.findAccountById(1); 26 System.out.println(account.toString()); 27 } 28 @Test 29 public void testSave(){ 30 Account account = new Account(); 31 account.setName("cong"); 32 account.setMoney(5); 33 as.saveAccount(account); 34 } 35 @Test 36 public void testUpdate(){ 37 Account account = new Account(); 38 account.setName("rainbow"); 39 account.setMoney(50000); 40 account.setId(3); 41 as.updateAccount(account); 42 } 43 @Test 44 public void testDelete(){ 45 as.deleteAccount(4); 46 } 47 }
10.testFindAll方法测试结果(测试了一轮,所以有点不同)
11.完整项目结构
spring框架学习-个人案例(借助dbutils实现数据库操作)
标签:mys enc nts pre junit date ice row test
原文地址:https://www.cnblogs.com/ccoonngg/p/11223340.html