1. 配置文件




    
    

    
    
        
        
        
        

        
        
    

    
    
        
    


2. javabean

jdbc.user=root
jdbc.password=admin
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring4

jdbc.initPoolSize=5
jdbc.maxPoolSize=10
package com.atguigu.spring.jdbc;

public class Grade {
    private int id;
    private int math;
    private int english;
    private int chinese;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public int getEnglish() {
        return english;
    }
    public void setEnglish(int english) {
        this.english = english;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    @Override
    public String toString() {
        return "Grade [id=" + id + ", math=" + math + ", english=" + english + ", chinese=" + chinese + "]";
    }

}
package com.atguigu.spring.jdbc;

public class User {
    private int id;
    private String useName;
    private String email;

    private Grade gradeId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUseName() {
        return useName;
    }

    public void setUseName(String useName) {
        this.useName = useName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Grade getGradeId() {
        return gradeId;
    }

    public void setGradeId(Grade gradeId) {
        this.gradeId = gradeId;
    }

    @Override
    public String toString() {
        return "Test [id=" + id + ", useName=" + useName + ", email=" + email + ", gradeId=" + gradeId + "]";
    }

}

3. 测试类

package com.atguigu.spring.jdbc;
import com.atguigu.spring.jdbc.User;

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

import javax.sql.DataSource;

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

public class JDBCTest {

    private ApplicationContext ctx = null;
    private JdbcTemplate jdbcTemplate = null;

    {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    }

    /**
     * 获取单个列的值,或做统计查询
     * 
     */
    @Test
    public void testQueryForObject2() {
        String sql = "select count(id) from test";
        long count = jdbcTemplate.queryForObject(sql, Long.class);

        System.out.println(count); 
    }

    /**
     * 查到实体类的集合
     * 调用的是 jdbcTemplate.query 方法
     */
    @Test
    public void testQueryForList() {
        String sql = "select id, useName, email from test where id > ?";
        RowMapper rowMapper = new BeanPropertyRowMapper<>(User.class);
        List users = jdbcTemplate.query(sql, rowMapper, 5);

        System.out.println(users);
    }

    /**
     * 从数据库中获取一条记录,实际得到对应的一个对象
     * 调用 queryForObject(sql, rowMapper, 1)方法
     *  1. 其中的 rowMapper 指定如何去映射结果集的行,常用的实现类为 BeanPropertyRowMapper
     *  2. 使用 SQL 中列的别名完成列名和类的属性名的映射 
     */
    @Test
    public void testQueryForObject() {
        String sql = "select id, useName, email from test where id = ?";
        RowMapper rowMapper = new BeanPropertyRowMapper<>(User.class);
        User test = jdbcTemplate.queryForObject(sql, rowMapper, 1);

        System.out.println(test);
    }

    /**
     * 执行批量更新: 批量的 insert, update, delete
     *  最后一个参数是 Objetc[] 的 list 类型:因为修改一条记录需要一个 Object 的数组, 那么多条就需要多个 Object 的数组
     */

    @Test
    public void testBatchUpdate(){
        String sql = "insert into test(useName, email) values(?, ?)";

        List batchArgs = new ArrayList<>();
        batchArgs.add(new Object[]{"AAA", "[email protected]"});
        batchArgs.add(new Object[]{"BBB", "[email protected]"});
        batchArgs.add(new Object[]{"CCC", "[email protected]"});

        jdbcTemplate.batchUpdate(sql, batchArgs);
    }

    /**
     * 执行 insert, update, delete
     */
    @Test
    public void testUpdate() {
        String sql = "update test set useName = ? where id = ?";
        jdbcTemplate.update(sql, "Tom", 2);
    }

    @Test 
    public void testDataSource() throws SQLException {
        DataSource dataSource = ctx.getBean(DataSource.class);
        System.out.println(dataSource.getConnection());
    }
}