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

(1、)配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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="demoJdbcTemplate" class="util.demo">
        <property name="jdbcTemplate">
            <ref bean="jdbcTemplate"/>
        </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<Map<String,Object>> 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错误的报错信息。

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值