JDBC 4.Spring Jdbc

Spring 框架对JDBC的简单封装 提供一个JDBC Template对象简化JDBC的开发

步骤:
1 导入Jar包
2 创建JdbcTemplate对象依赖于数据源DataSourse
JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUnits.getDataSource());
方法:调用JdbcTempalte方法来完成操作
update();执行增删改语句
queryForMap();查询结果将结果集封装为map集合 只能操作一行数据的
queryForList();查询结果将结果集封装为list集合 操作多行数据
query查询结果 将结果封装为JavaBean对象 将数据库数据封装成对象
传入参数为RowMapper对象
一般使用new BeanPropertyRowMapper<类型>(类型.class)
queryForObject查询结果 将结果封装成对象想· 应用于聚合函数上

例子:

public class JdbcTemplateDemo {
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUnits.getDataSource());
        String sql="update change_money set money=? where id =?";
        int count = jdbcTemplate.update(sql, 1500, 1);
        System.out.println(count);
    }
}

各方法演示

public class JdbcTemplateDemo2 {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUnits.getDataSource());

    //queryForMap  只能查询一行结果
    @Test
    public void test1() {
        String sql = "select * from galary where id= ?";
        Map map = jdbcTemplate.queryForMap(sql, 1);
        System.out.println(map);
    }
    //    queryForList 可以查询多行结果
    @Test
    public void test2(){
        String sql = "select * from galary ";
        List> list = jdbcTemplate.queryForList(sql);
        System.out.println(list);
    }

    @Test
    public void test3(){
        String sql = "select * from galary ";
        List people = jdbcTemplate.query(sql, new RowMapper() {
            @Override
            public Person mapRow(ResultSet resultSet, int i) throws SQLException {
                Person person = new Person();
                int id = resultSet.getInt("id");
                person.setId(id);
                String name = resultSet.getString("name");
                person.setName(name);
                int galary = resultSet.getInt("galary");
                person.setGalary(galary);
                return person;
            }
        });
        for (Person person : people) {
            System.out.println(person);
        }
    }

    @Test
    public void test4(){
        String sql = "select * from galary ";
        List people = jdbcTemplate.query(
                sql,new BeanPropertyRowMapper(Person.class));
        for (Person person : people) {
            System.out.println(person);
        }
    }

}
//为了防止无法将赋值的null写入 设置为类型为包装器类
public class Person {
    private Integer id;
    private String name;
    private Integer galary;

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", galary=" + galary +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getGalary() {
        return galary;
    }

    public void setGalary(Integer galary) {
        this.galary = galary;
    }
}

你可能感兴趣的:(JDBC 4.Spring Jdbc)