JdbcTemplate对象

  • spring管理jdbc.
    •   这里的例子主要是通过阿里druid连接池为例.
      • 导入对应的druid包和依赖的common包和日志包
      • 因为是导包之后包里的类都是书写好的类.所以可以直接将连接池对象交给spring管理
      • 配置连接池的对应参数.所谓的druid.properties不用在书写了.配置挪到这里了
 <bean id="dds" class="com.alibaba.druid.pool.DruidDataSource">
         <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
         <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
         <property name="username" value="hr"></property>
         <property name="password" value="hr"></property>
         <property name="initialSize" value="10"></property>
         <property name="maxActive" value="20"></property>
         <property name="minIdle" value="5"></property>
         <property name="maxWait" value="3000"></property>
     </bean>
  • JdbcTemplate对象
    •   通过这个对消调用方法对数据库进行增删改查
    • 先将包里得对象创建权利转义给spring工厂
    • <bean id="sf" class="org.springframework.jdbc.core.JdbcTemplate">
                   <property name="dataSource" ref="dds"></property>
           </bean>

      对应如下数据库操作

      •   dml操作增删改
      • @Test
            public void test3(){//更改
                ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/com/baizhi/a/applicationContext.xml");
                JdbcTemplate bean = (JdbcTemplate)ac.getBean("sf");
                int update = bean.update("update t_person set name = ? where id = ?", "樱木工口","231");
                System.out.println(update);
                
                
            }
            @Test
            public void test4(){//删除
                ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/com/baizhi/a/applicationContext.xml");
                JdbcTemplate bean = (JdbcTemplate)ac.getBean("sf");
                int update = bean.update("delete t_person where id = ?","231");
                System.out.println(update);
                
                
            }
            @Test
            public void test5(){//插入
                ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/com/baizhi/a/applicationContext.xml");
                JdbcTemplate bean = (JdbcTemplate)ac.getBean("sf");
                int update = bean.update("insert into t_person values(?,?,?,?,?,?)","338", "酒井麻衣",1,22,"11111111111","东京塔");
                System.out.println(update);
                
                
            }

        查询:单个查询和多个查询结果

      • @Test
            public void test6(){//查询单个
                ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/com/baizhi/a/applicationContext.xml");
                JdbcTemplate bean = (JdbcTemplate)ac.getBean("sf");
                Person person = bean.queryForObject("select * from t_person where id = ?",
                        new RowMapper<Person>(){
        
                            @Override
                            public Person mapRow(ResultSet rs, int arg1)
                                    throws SQLException {
                                Person person = new Person(rs.getString(2), rs.getInt(3)==1?true:false, rs.getInt(4), rs.getString(5), rs.getString(6));    
                                return person;
                            }
                    
                }, "338");
                
                System.out.println(person);
                
                
            }
        @Test
            public void test2(){//多个查询结果
                ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/com/baizhi/a/applicationContext.xml");
                JdbcTemplate bean = (JdbcTemplate)ac.getBean("sf");
                List<Person> list = bean.query("select * from t_person",
                        new RowMapper<Person>(){
        
                            @Override
                            public Person mapRow(ResultSet rs, int arg1)
                                    throws SQLException {
                                Person person = new Person(rs.getString(2), rs.getInt(3)==1?true:false, rs.getInt(4), rs.getString(5), rs.getString(6));
                                return person;
                            }
                    
                }
                        );
                for(Person person:list){
                    System.out.println(person);
                }
                
            }

         

posted @ 2018-12-12 14:35  歌语苳文  阅读(148)  评论(0编辑  收藏  举报