• jdbatemplate使用



     
    public static void main(String[] args) throws SQLException {
            DataSource ds = new ComboPooledDataSource();
            JdbcTemplate jt = new JdbcTemplate(ds);
            String sql = "select * from tb_emp1 where id = ?";
            //jdbctemplate返回map集合
            Map<String, Object> map = jt.queryForMap(sql, 1);
            System.out.println(map);
        }
    queryForMap方法使用
    {id=1, name=xiaohei, deptId=1, salary=2000.0}

    这个方法返回一个Map集合,但是只能返回一个数据集,否则报错

    要想返回多个数据集要使用   queryForList  方法。

    public static void main(String[] args) throws SQLException {
            DataSource ds = new ComboPooledDataSource();
            JdbcTemplate jt = new JdbcTemplate(ds);
            String sql = "select * from tb_emp1 where id = ? or id=?";
            //jdbctemplate返回map集合
    //        Map<String, Object> map = jt.queryForMap(sql, 1);
            List<Map<String, Object>> maps = jt.queryForList(sql, 1,2);
            System.out.println(maps);
        }
    [{id=1, name=xiaohei, deptId=1, salary=2000.0}, {id=2, name=haha, deptId=3, salary=500.0}]

    但是在真实开发中使用最多的是吧数据封装成javabean对象,在封装到List中,所以就用到了query方法

    @Test
        public void test(){
            // 真实开发中数据封装成Map或者List的情况较少,比较多的是封装成javabean对象,在封装成list,所以使用query方法 。
            //List<T> query(String var1, RowMapper<T> var2),参数中需要RowMapper接口
            DataSource ds = new ComboPooledDataSource();//获取连接池
            JdbcTemplate jt = new JdbcTemplate(ds);//获取jdbctemplate对象
            String sql = "select * from tb_emp1";
            List<Emp> list = jt.query(sql, new RowMapper<Emp>() {
                @Override
                public Emp mapRow(ResultSet rs, int i) throws SQLException {
                    //获取值
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    double salary = rs.getDouble("salary");
                    //封装到对象中
                    Emp emp = new Emp();
                    emp.setId(id);
                    emp.setSalary(salary);
                    emp.setName(name);
                    return emp;
                }
            });
            System.out.println(list);
    
        }
    [Emp{id=1, name='xiaohei', salary=2000.0}, Emp{id=2, name='haha', salary=500.0}]

    但是从代码中可以看到,还是 太麻烦了,因为要自己实现RowMapper接口,实现这个接口时,里面的代码太麻烦了,

    所以spring公司给我们封装好了实现类,可以供你使用

    //public BeanPropertyRowMapper(Class<T> mappedClass),这个方法需要传参数,需要指定  类名.class文件
    这个方法是spring公司提供的RowMapper的实现类,极大地简化了代码。
    @Test
        public void test2(){
            DataSource ds = new ComboPooledDataSource();//获取连接池
            JdbcTemplate jt = new JdbcTemplate(ds);//获取jdbctemplate对象
            String sql = "select * from tb_emp1";
            //public BeanPropertyRowMapper(Class<T> mappedClass),这个方法需要传参数,需要指定  类名.class文件
            List<Emp> list = jt.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
            for (Emp emp : list) {
                System.out.println(emp);
            }
        }
    Emp{id=1, name='xiaohei', salary=2000.0}
    Emp{id=2, name='haha', salary=500.0}
  • 相关阅读:
    AJAX的使用
    django.template.exceptions.TemplateDoesNotExist: login.html报错
    cookie、session
    关于zipfile解压出现的字符编码问题
    使用jquery清空input 文本框中的内容
    DVWA SQL-injection 附python脚本
    关于itchat用法的一篇博文
    记录两个python itchat的用法博客网址
    pyinstaller 打包.exe文件记录遇到的问题
    用python编写的excel拆分小工具
  • 原文地址:https://www.cnblogs.com/bozhengheng/p/12215849.html
Copyright © 2020-2023  润新知