单独加载DataSource,用于测试对数据库的访问。

(1、)配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tool="http://www.springframework.org/schema/tool" xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jaxws="http://cxf.apache.org/jaxws"

       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
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www..org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/tx/spring-lang.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/tx/spring-util.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/tx/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/tx/spring-jee.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">


    <bean p:ignoreResourceNotFound="false" p:fileEncoding="GBK"
        p:location="classpath:conf/config.properties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>${jdbc.driver}value>property>
      <property name="url"><value>${jdbc.url}value>property>
      <property name="username"><value>${jdbc.username}value>property>
      <property name="password"><value>${jdbc.password}value>property>
    bean>

    

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
          <property name="dataSource">
            <ref bean="dataSource"/>
          property>
    bean>


beans>

数据库连接配置信息 config.properties

#jdbc驱动
#jdbc.driver=com.mysql.jdbc.Driver
#URL
#jdbc.url=jdbc:mysql://127.0.0.1:3306/test
#用户名
#jdbc.username=root
#密码
#jdbc.password=176658
#编码
#encoding=GB2312

(2)jdbcTemplate测试类

package util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class demoJdbcTemplate {

    @Test
    public void test(){

        ApplicationContext ac = new ClassPathXmlApplicationContext("conf/applicationContext.xml");  

        JdbcTemplate jdbcTemplate = ac.getBean("jdbcTemplate", JdbcTemplate.class);

        List> list =  jdbcTemplate.queryForList("select * from cp_epon_olt_ip_mapping_tbl t where t.EMS_IP = '10.9.164.2'");
        System.out.println("list:"+list);

    }

}

(3、)实现上面查询时,要注意包的导入,若上面的包没有导入一般会出现创建bean错误的报错信息。

单独加载DataSource,用于测试对数据库的访问。_第1张图片

你可能感兴趣的:(数据库访问)