JdbcTemplate的使用大全

1、判斷數據庫是否獲得連接

@Test
    public void test() throws SQLException {

        DataSource bean = ioc.getBean(DataSource.class);
        Connection connection = bean.getConnection();
        System.out.println(connection);
    }

2、sql 語句的按位更新、或插入

@Test
    public void test02(){
        //1、從容器中獲取JdbcTemplate進行操作
        String sql = "update employee set salary=? where emp_id=?";
        int update = jdbcTemplate.update(sql, 1300,5);
        System.out.println(update);
    }

3、sql 語句的批量插入

 @Test
    public void test03(){
        String sql = "INSERT  INTO `employee`(`emp_name`,`salary`) VALUES (?,?)";
        List<Object[]> args = new ArrayList<>();

        args.add(new Object[]{"張三",9989.98});
        args.add(new Object[]{"李四",19989.98});
        jdbcTemplate.batchUpdate(sql,args);
    }

4、查詢單個對象

  • 數據字段與數據庫相同

BeanPropertyRowmapper用來將javaBean屬性和查出的數據一一映射

       @Test
        public void test04(){
            String sql = "select * from employee where emp_id=?";
             Employee employee = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Employee.class), 7);
        }
  • 數據字段與數據庫不同 需重寫方法
 @Test
            public void test04(){
                String sql = "select * from employee where emp_id=?";
                 Employee employee = jdbcTemplate.queryForObject(sql,  new RowMapper<Employee >(){
                @Override
                 public Employee mapRow(ResultSet rs, int index)throws SQLException {
			Employee temp = new Employee ();
			temp.setAccountid(rs.getInt("id"));
			temp.setLoginname(rs.getString("name"));
                       return temp;     
                       }
                       		}, 7);
            }

4-2、查詢集合對象

@Test
            public void test04(){
                String sql = "select * from employee where emp_id=?";
                 List<helgolandLog>  employees = jdbcTemplate.query(sql,  new RowMapper<Employee >(){
                @Override
                 public Employee mapRow(ResultSet rs, int index)throws SQLException {
			Employee temp = new Employee ();
			temp.setAccountid(rs.getInt("id"));
			temp.setLoginname(rs.getString("name"));
                       return temp;     
                       }
                       		}, 7);
            }

5、使用帶有具名參數的SQL語句插入一條記錄,並以Map形式傳入參數值

@Test
    public void test07(){
        String sql = "INSERT  INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)";
        Map<String, Object> map = new HashMap<>();
        map.put("empName", "韓總");
        map.put("salary", 40000.0);
        //如果支持具名參數的功能。JdbcTemplate就不能用了。
        /*MapSqlParameterSource source = new MapSqlParameterSource(map);
        namedTemplate.update(sql, source);*/
        namedTemplate.update(sql, map);
    }

6、以SqlParameterSource形式傳入參數值

@Test
    public void test08(){
        String sql = "INSERT  INTO `employee`(`emp_name`,`salary`) VALUES (:empName,:salary)";
        Employee employee = new Employee(null, "韓五", 499998.00);
        //如果支持具名參數的功能。JdbcTemplate就不能用了。
        //SqlParameterSource
        //BeanPropertySqlParameterSource
        //可以直接將Bean的屬性映射參數進行處理
        namedTemplate.update(sql, new BeanPropertySqlParameterSource(employee));

    }

7、自動裝配 JdbcTemplate 對象

 @Test
    public void test09(){
        EmployeeDao bean = ioc.getBean(EmployeeDao.class);
        Employee employee = new Employee(null, "韓五11", 499998.00);
        bean.insert(employee);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章