Spring-005-jdbcTemplete配置连接池和dao层

  1. spring配置c3p0连接池

    1. 需要c3p0jar包 mchange-commons-java
    2. 原始连接池做法
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testspring");
        dataSource.setUser("root");
        dataSource.setPassword("");
    
    1. 使用配置文件的做法
    
    
    
        
        
        
        
        
    
    
  2. dao层使用 jdbcTemplete
    1.创建Service和Dao 配置service和dao对象,在service注入dao对象

        
        
        
        
        
    

    2.创建jdbcTemplete对象 把jdbcTemplete对象注入到dao里面

    public class UserDao {
    //得到JdbcTemplete对象
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void add() {
        // TODO Auto-generated method stub
        
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    1. 在jdbcTemplete中注入DataSource对象

因为源代码中有set方法 并且有DataSource对象 所以可以在配置文件中注入对象。


Spring-005-jdbcTemplete配置连接池和dao层_第1张图片
image

    
    
    

你可能感兴趣的:(Spring-005-jdbcTemplete配置连接池和dao层)