JDBC Template

jdbcTemplateDemo1
package cn.jdbcTemplate;

import cn.utils.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

public class jdbcTemplateDemo1 {
    public static void main(String[] args) {
        //1.导入jar包
        //2.创建JDBCTemplate对象
        JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
        //3.调用方法
        String sql ="update bank set acount=500 where id=?";
        int count=template.update(sql,5);
        System.out.println(count);

    }
}

导入的包网上都有,注意版本问题就ok
Emp
package cn.domain;

public class Emp {
    private int id;
    private String username;
    private int acount;

    public Emp() {
    }

    public Emp(int id, String username, int acount) {
        this.id = id;
        this.username = username;
        this.acount = acount;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAcount() {
        return acount;
    }

    public void setAcount(int acount) {
        this.acount = acount;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", acount=" + acount +
                '}';
    }
}

jabcTemplateDemo2Test
package cn.jdbcTemplate;

import cn.domain.Emp;
import cn.utils.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

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

public class jabcTemplateDemo2Test {
    //Junit单元测试,可以让方法独立执行
    //1.获取JDBCTemplate对象
    private  JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * Junit单元测试
     */
    /**
     * 1.修改数据 id=1  acount=10
     */
    @Test
    public void test1(){
        //1.获取JDBCTemplate对象
        JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
        //2.定义sql
        String sql="update bank set acount =10 where id =1";
        //3.执行sql
        int count =template.update(sql);
        System.out.println(count);
    }
    /**
     * 添加一条数据
     */
    @Test
    public void test2(){
        String sql="insert into bank values(null,?,?)";
       int count= template.update(sql,"Demo100",10000000);
        System.out.println(count);

    }
    /**
     * 删除刚刚的添加纪录
     */
    @Test
    public void test3(){
        String sql="delete from bank where id=?";
        int count=template.update(sql,7);
        System.out.println(count);
    }
    /**
     * 4.查询id为1的记录,将其封装为Map集合
     */
    @Test
    public void test4(){
        String sql="select * from bank where id=?";
        Map<String, Object> map = template.queryForMap(sql, 1);
        System.out.println(map);//{id=1, username=Demo1, acount=10}
    }
    /**
     * 5.查询所有记录,将其封装为List
     */
    @Test
    public void test5(){
        String sql="select * from bank";
        List<Map<String, Object>> list = template.queryForList(sql);

        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }
  /*  *//**
     * 查询所有记录,将其封装为Emp对象的List集合
     *
     *//*
    @Test
    public void test6(){
        String sql ="select * from bank";
        List<Emp> list = template.query(sql, new RowMapper<Emp>() {
            @Override
            public Emp mapRow(ResultSet rs, int i) throws SQLException {
                Emp emp = new Emp();
                int id = rs.getInt("id");
                String username = rs.getString("username");
                int acount = rs.getInt("acount");

                emp.setAcount(acount);
                emp.setId(id);
                emp.setUsername(username);

                return emp;
            }
        });
        for (Emp emp : list) {
            System.out.println(emp);

        }


    }*/
    /**
     * 查询所有记录,将其封装为Emp对象的List集合
     * 简化上面的操作
     */
    @Test
    public void test6(){
        String sql="select * from bank";
        List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
        for (Emp emp : list) {
            System.out.println(emp);

        }



    }
    /**
     * 查询总记录数
     */
    @Test
    public void test7(){
        String sql="select count(id) from bank";
        Long al = template.queryForObject(sql, Long.class);
        System.out.println(al);
    }


}

猜你喜欢

转载自blog.csdn.net/DemoD_/article/details/89041429