jdbcTemplate使用及xml配置

1.导入相关包  页底链接有我的百度云资源共享 需要导入的所有包

jdbcTemplate使用及xml配置

导入完成是这个样子

2.配置beans1.xml  

<?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:context="http://www.springframework.org/schema/context"
       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">

    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        <!-- ${key} 可以读取properties文件中配置 key对应value -->
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- setDataSource 方法里面传一个dataSource对象 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 另外一种写法
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        </bean>
        -->
</beans>

3.创建pojo类   在这里用的是Dept类 里面只有两个属性

jdbcTemplate使用及xml配置

创建测试类Test

package com.baidu.beans1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;


public class Test1 {

    public static void main(String[] args) {
      //加载beans1.xml,创建bean工厂 

    ApplicationContext applicationContext=    new ClassPathXmlApplicationContext("beans1.xml");

    //applicationContext.getBean(“jdbcTemplate”),里面是beans1.xml里面配置的信息,获取bean,强转为 JdbcTemplate对象

    JdbcTemplate jdbcTemplate=(JdbcTemplate)applicationContext.getBean("jdbcTemplate");
    //调用jdbcTemplate.query(sql,rowMapper)方法,执行查找语句,里面两个参数,第一个sql语句,第二个是RowMapper的对象,RowMapper是一个接口,要对里面方法进行实现,实现mapRow方法,返回一个Dept对象,存放到集合中,在这写的是一个匿名类,<Dept>是泛型,参数rs 结果集,colNum 数据库列号   did dname分别是数据库列的名称
    List<Dept> list=jdbcTemplate.query("SELECT * from dept",new RowMapper<Dept>(){

        @Override
        public Dept mapRow(ResultSet rs, int colNum) throws SQLException {
            // TODO Auto-generated method stub
            Dept dept = new Dept(rs.getInt("did"),rs.getString("dname"));
            return dept;
        }
        
    }
);
    //遍历集合 打印
    for (Dept dept : list) {
        System.out.println(dept);
    }
    
    }
    
}

jdbcTemplate使用及xml配置

jdbc.properties在这里面更改数据库名称

jdbcTemplate使用及xml配置


相关包和jdbc.properties和log4j.properties,beans1.xml等文件资源:

链接: https://pan.baidu.com/s/1yJmdGf-J-iMTy5isS90U-A 密码: ek48