SSM-Spring-3-JdbcTemplate

1 JdbcTemplate开发步骤

jdbcTemplate是Spring框架中提供的一个对象,是对原始繁琐的jdbcAPI对象的简单封装,Spring框架为我们提供了很多的操作模板类,如操作关系数据的jdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate

jdbcTemplate的开发步骤:

1,导入spring-jdbc和spring-tx相关jar包

2,创建数据库表和对应实体

3,创建jdbcTemplate对象

4,执行数据库操作

1,导入spring-jdbc和spring-tx相关jar包:

      <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,创建数据库表和对应实体:

数据库中的User表:
在这里插入图片描述
对应的实体类:
在这里插入图片描述
3,创建jdbcTemplate对象:

    @Test
    public void test1() throws PropertyVetoException {
        // 准备数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/chatroom");
        dataSource.setUser("root");
        dataSource.setPassword("111");


        // 创建JdbcTemplate对象并设置数据源
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
    }

4,执行数据库操作:
在这里插入图片描述

2 Spring产生JdbcTemplate对象

同样可以将JdbcTemplate对象作为Bean由Spring产生,使用时直接从容器中获取即可,但是jdbcTemplate对象必须注入数据源才能正常使用,因为其底层的操作也都是基于Connection对象进行的

在jdbc.properties文件中配置数据源需要的基本数据:
在这里插入图片描述
在applicationContext.xml对象中进行配置:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 导入jdbc.properties文件   -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!-- 配置dataSource -->
    <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <!-- 配置jdbcTemplate   -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dateSource"></property>
    </bean>

</beans>

测试:

@Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");

        int row = jdbcTemplate.update("INSERT INTO sys_client_userinfo VALUES (?, ?, ?)", 666, "coin", "666");
        System.out.println(row);
    }

3 JdbcTemplate的常用操作

使用的表:
在这里插入图片描述

3.1 更新

更新操作:jdbdTemplate.update(sql, params);

使用JdbcTemplate对象进行插入操作:

jdbcTemplate.update("INSERT INTO sys_client_userinfo VALUES (?, ?, ?)", 555, "rain", "555");

使用JdbcTemplate对象进行删除操作:

jdbcTemplate.update("DELETE FROM sys_client_userinfo WHERE id = ?", 444);

使用JdbcTemplate对象进行修改操作:

jdbcTemplate.update("UPDATE sys_client_userinfo set password = ? WHERE id = ?", "444", 444);

3.2 查询

查询操作:
jdbcTemplate.query(sql, Mapper, params)
jdbcTemplate.queryForObject(sql, Mapper, params)

使用JdbcTemplate对象进行查询操作:

1,全表查询并封装到列表:
List<User> userList = jdbcTemplate.query("SELECT * FROM sys_client_userinfo", new BeanPropertyRowMapper<>(User.class));

2,查询表中的一个对象并封装:
User user = jdbcTemplate.queryForObject("SELECT * FROM sys_client_userinfo WHERE id = ?", new BeanPropertyRowMapper<>(User.class), 111);

3,聚合查询:
Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM sys_client_userinfo", Integer.class);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值