JdbcTemplate

181 阅读1分钟

有了模板就不用关心那些共性的东西了

图片.png

JdbcTemplate提供了一个标注的API

JDBCConfig

1.注册JdbcTemplate模板对象Bean
@Bean("jdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource){
    return new JdbcTemplate(dataSource);
}

AccountDaoImpl 实现类

@Repository("accountDao")
public class AcountDaoImpl implements AccountDao{
    //2.注入模板对象
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    //实际上只需要书写sql语句和里面的参数就可以了
    第一个方法
    public void save(Account account){
        String sql 
        = "insert into account(name,money)values(?,?)";
              jdbcTemplate.update(sql,account.getName(),account.getMoney());
      
      第二个方法 
      public void delete(Integer id){
          String sql = "delete from account where id =?"
          jdbcTemplate.update(sql,id)
      }
      
      第三个方法 
      public void update(Account account){
          String sql = "update account set name = ?,money=? where id = ?"
          jdbcTemplate.update(sql,account.getName(),account.getMoney(),account.getId());
      }
      
      第四个方法        
      public String findNameById(Integer id){
      String sql = "select name from account where id =?"
      
      //单字段查询可以使用专门的查询方法,必须指定查询出的数据类型,例如name为String类型;String.class=查询出来的类型
      return
      jdbcTemplate.queryForObject(sql,String.calss,id)        
      //多个字段查询的方式
      第五个方法
      public Account findById(Integer id){
          String sql = "select * from account where id = ?"
          //支持自定义行 映射 解析器
          RowMapper<Account> rm = (rs,rowNum){
          public Account mapRow(ResultSet rs,int rowNum) throws SQLException{
              Account account = new Account();
              account.setId(rs.getInt("id"));
              account.setName(rs.getString("name"));
              account.setMoney(rs.getDouble("money"));
              return account;
          }
        };
          return jdbcTemplate.queryForObject(sql,rm,id)
      }
      
      第六个方法<Account>类是哪一个,后面是 类型
      public List<Account> findAll(){
          String sql = "select * from account";
          使用spring自带的行映射解析器,要求必须是标准封装
          return jbdcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class));
      }
      
    }
}

AccountServiceTest测试实现类

设定spring专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
设定加载的spring上下文对应的配置
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest{
    @Autowired
    private AccountService accountService;
    
    @Test
    public void testSave(){
        Account account = new Account();
        account.setName("名字");
        account.setMoney(1000);
        accountService.save(account);
    }
    
    @Test
    public void testDelete(){
        accountService.delete(6); //id=6的 
    }
    
    @Test
    public void testUpdate(){
        Account account = new Account();
        account.setId(7);
        account.setName("新名字");
        account.setMoney(1000);
        accountService.update(account);
    }
   
    @Test
    public void testFindNameById(){
        String name = accountService.findNameById(2);
        System.out.println(name);
    }
    
    @Test
    public void testFindById(){
        Account account = accountService.findById(2);
        System.out.println(account);
    }
  
    @Test
    public void testFindAll(){
        List<Account> list = accountService.findAll();
        System.out.println(list);
    }
    
    }
}