spring和jdbc的整合

使用spring处理数据访问时的异常

  • spring对数据库的访问过程中的可能错误都进行了封装,与jdbc的错误体系比较较全面详细
  • spring封装的错误都是非检查的错误(运行时异常),故可以不用try。。catch进行捕获。

使用spring提供的数据访问模版

  • 传统的jdbc使用时需要多写很多重复的代码,spring对其进行了封装。
  • 通过jdbc的template可以简化jdbc的访问流程

创建数据源

  • 通过连接池(c3p0)
  • 基于JDBC驱动的数据源,由spring提供(DriverManagerDataSource,SingleConnectionDataSource)
    //通过c3p0连接池建立数据源
    @Bean
    @Primary
    public DataSource dataSourceFromComboPooledDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/user");
        dataSource.setUser("root");
        dataSource.setPassword("xbx");
        return dataSource;
    }
    //通过JDBC驱动的数据源
    //DriverManagerDataSource:对每一个请求都会返回一个新建的链接,但是没有对连接的管理
    //SingleConnectionDataSource:单例每个连接都是用同一个数据库连接
    @Bean
    public DataSource dataSourceFromDriverManagerDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/user");
        dataSource.setUsername("root");
        dataSource.setPassword("xbx");
        return dataSource;
    }

使用模版

  • JdbcTemplate:基本的模版,支持简单的模版以及基于索引的参数的查询
  • NamedParameterJdbcTemplate:可以在查询的时候将参数进行命名
  @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
       return new JdbcTemplate(dataSource);
    }
    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource){
        return new NamedParameterJdbcTemplate(dataSource);
    }
  • 使用模版示例
@Repository
public class MyUserDao {
    @Autowired
    private JdbcTemplate template;
    @Autowired
    private NamedParameterJdbcTemplate npTemplate;
    //通过NamedParameterJdbcTemplate对参数进行命名,将参数封装在Map中
    public void addUserFromNamedParameterJdbcTemplate(){
        DaoUser daoUser = new DaoUser("user","123","123@qq.com");
        String sql="insert into myuser values (:username,:password,:email)";
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", daoUser.getName());
        map.put("password", daoUser.getPassword());
        map.put("email", daoUser.getEmail());
        int update = npTemplate.update(sql, map);
        System.out.println(update);
    }
    //使用基本的JdbcTemplate,参数使用索引进行传递
    public void addUser(){
        System.out.println(template);
        String sql="insert into myuser values (?,?,?)";
        int update = template.update(sql, "test", "pass", "23233");
        System.out.println(update);
    }
    //对查询到的数据进行封装,对参数进行封装的类需要实现RowMapper接口。
    public DaoUser getUser(){
        String sql = "select * from myuser WHERE username = ?";
        String name="xia";
        //参数:sql语句,用来封装数据的对象(需要实现RowMapper接口),要绑定到sql语句中的参数
        Object o = template.queryForObject(sql, new DaoUserMapper(),name);
        return (DaoUser) o;
    }
}
//用来封装查询出来的数据的类
public class DaoUserMapper implements RowMapper {
    @Override
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        DaoUser user = new DaoUser();
        user.setName(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setEmail(rs.getString("email"));
        return user;
    }
}

转载于:https://my.oschina.net/helloXia/blog/1830318

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值