一天一点做项目(2)

 第四步:写单元测试类

Code:
  1. package com.jackyan.practice.store.junit;   
  2. import com.jackyan.practice.store.service.ProductBeanService;   
  3. import com.jackyan.practice.store.service.mysql.ProductBeanServiceMysqlImpl;   
  4. import junit.framework.TestCase;   
  5.   
  6. public class ProductServiceTest extends TestCase {   
  7.     public ProductBeanService pbs;   
  8.     public ProductServiceTest(String name){   
  9.         super(name);   
  10.     }   
  11.     protected void setUp()throws Exception{   
  12.         super.setUp();   
  13.         pbs=new ProductBeanServiceMysqlImpl();   
  14.            
  15.     }   
  16.     protected void tearDown()throws Exception{   
  17.         super.tearDown();   
  18.     }   
  19.     public void testGetAllProductSimpleInformation()throws Exception{   
  20.         assertNotNull(pbs.getAllProductSimpleInformation());   
  21.     }   
  22.     public void testGetProductById()throws Exception{   
  23.         assertNotNull(pbs.getProductById(1));   
  24.     }   
  25. }  

 

第五步 编写工具类(其实这一步应该拿到第三步里

Code:
  1. package com.jackyan.practice.store.util;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.DataInputStream;   
  5. import java.io.FileInputStream;   
  6. import java.sql.Connection;   
  7. import java.sql.SQLException;   
  8. import java.util.Properties;   
  9. import javax.sql.DataSource;   
  10. import org.apache.commons.dbcp.BasicDataSourceFactory;   
  11.   
  12. public class ConnectionFactory {   
  13.     private static ConnectionFactory factory= new ConnectionFactory();   
  14.     private static DataSource source =null;   
  15.     private static String path="E://source//store//src//com//jackyan//practice//store//util//dbconfig.properties";   
  16.     static{   
  17.         try{   
  18.             /*还是老问题  
  19.               prop.load(ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties"));  
  20.               jvm无法自动搜索到配置文件。  
  21.             */  
  22.             Properties prop=new Properties();   
  23.             prop.load(new DataInputStream(new BufferedInputStream(new FileInputStream(path))));   
  24.             source=BasicDataSourceFactory.createDataSource(prop);          
  25.         }catch(Exception e){   
  26.             System.out.println("数据库连接池初始化失败");   
  27.             e.printStackTrace();   
  28.         }   
  29.     }   
  30.     private ConnectionFactory(){   
  31.     }   
  32.     public static ConnectionFactory getFactory(){   
  33.         return factory;   
  34.     }   
  35.     public static Connection getConnection(){   
  36.         try {   
  37.             return  source.getConnection();   
  38.         } catch (SQLException e) {   
  39.             System.out.println("从数据库连接池获取链接时出现异常");   
  40.             return null;   
  41.         }   
  42.     }   
  43.     public static DataSource getDataDource(){   
  44.         return source;   
  45.     }   
  46. }  

 

Code:
  1. package com.jackyan.practice.store.util;   
  2.   
  3. import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;   
  4.   
  5. public class Utils {   
  6.     private static SimpleJdbcTemplate jdbcTemplate=new SimpleJdbcTemplate(ConnectionFactory.getDataDource());   
  7.     private Utils(){   
  8.     }   
  9.     public static SimpleJdbcTemplate getJdbcTemplate() {   
  10.         return jdbcTemplate;   
  11.     }   
  12. }  

数据库连接池(dbcp)的配置文件dbconfig.properties

Code:
  1. #暂时使用dbcp连接池   
  2. driverClassName=com.mysql.jdbc.Driver   
  3. url=jdbc:mysql://localhost:3306/test   
  4. username=root   
  5. password=yanshujun   
  6.   
  7. #初始连接数   
  8. initialSize=2  
  9.   
  10. #最大连接数   
  11. maxActive=5  
  12.   
  13. #最大空闲连接数   
  14. maxIdle=2  
  15.   
  16. #最小空闲连接数   
  17. minIdle=0  
  18.   
  19. #超时等待最大时间(毫秒)   
  20. maxWait=10000  

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值