IDEA版SSM入门到实战(Maven+MyBatis+Spring+SpringMVC) -Spring管理第三方bean

第一章 Spring管理第三方bean

1.1 Spring管理druid步骤
  • 导入jar包

    
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.10version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.37version>
    
            dependency>
    
  • 编写db.properties配置文件

    #key=value
    db.driverClassName=com.mysql.jdbc.Driver
    db.url=jdbc:mysql://localhost:3306/db220106
    db.username=root
    db.password=root
    
  • 编写applicationContext.xml相关代码

    
    <context:property-placeholder location="classpath:db.properties">context:property-placeholder>
    
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driverClassName}">property>
        <property name="url" value="${db.url}">property>
        <property name="username" value="${db.username}">property>
        <property name="password" value="${db.password}">property>
    bean>
    
  • 测试

    @Test
    public void testDruidDataSource() throws Exception{
        //获取容器对象
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("applicationContext_druid.xml");
    
        DruidDataSource dataSource = ioc.getBean("dataSource", DruidDataSource.class);
        System.out.println("dataSource = " + dataSource);
    
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);
    
    }
    

你可能感兴趣的:(intellij-idea,maven,mybatis)