Spring知识四(Spring JdbcTemplate)

1、Spring jdbcTemlpate 基本使用

1.1 jdbcTemplate概述

它是spring框架中提供的一个对象,是对原始繁琐的jdbcAPI对象的简单封装。spring框架为我们提供了很多的操作模板类。

1.2 JdbcTemplate开发步骤

(1)导入spring-jdbc和spring-tx坐标

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>

(2)创建数据库表和实体

(3)创建JdbcTemplate对象

(4)执行数据库操作

 //测试JdbcTemplate开发步骤
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("");

        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        //设置数据源对象,知道数据在哪
        jdbcTemplate.setDataSource(dataSource);
        //执行操作
        int row = jdbcTemplate.update("insert into account values (?,?)", "tom", 5000);
        System.out.println(row);
    }

1.3 抽取文件

抽取jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=
<!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

1.4 JdbcTemplate的常用操作

 @Autowired  //进行注入
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQueryAll(){   //查询多个操作
        List<Account> accountList = jdbcTemplate.query("select * form account ", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

    @Test
    public void testQueryOne(){   //查询单个操作
        Account account = jdbcTemplate.queryForObject("select * form account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }

    @Test
    public void testQueryCount(){   //查询聚合操作
        Long count = jdbcTemplate.queryForObject("select count(*) form account", Long.class);
        System.out.println(count);
    }

    @Test
    public void testUpdate(){   //修改操作
        jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
    }

    @Test
    public void testDelete(){   //删除操作
        jdbcTemplate.update("delete form account where name=?","tom");
    }

1.5  知识要点

        (1)导入Spring-jdbc和Spring-tx坐标

        (2)创建数据库表和实体

        (3)创建JdbcTemplate对象

 JdbcTemplate jdbcTemplate=new JdbcTemplate();
        //设置数据源对象,知道数据在哪
        jdbcTemplate.setDataSource(dataSource);

        (4)执行数据库操作

                更新操作:

jdbcTemplate.update(sql,params)

                查询操作:

jdbcTemplate.query(sql,Mapper,params)
jdbcTemplate.queryForObject(sql,Mapper,params)

总结:坚持很难,放弃容易,再接再厉,一起进步!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值