Spring中JdbcTemplate即Jdbc封装工具类的使用



Spring中封装好的工具类叫作模板(Template)
在这里插入图片描述
在这里插入图片描述
导入的坐标为:

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
快速入门:

在这里插入图片描述
变为:
在这里插入图片描述

public class JdbcDemo {
    @Test
   public void fun(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        int i = jdbcTemplate.update("insert into jdbc values (?,?)", 123, "张佳成");
        System.out.println(i);
    }
}

然后可以抽取Properties工具类,前面都讲过

在这里插入图片描述

插入/修改/删除操作

在JdbcTemplate中插入操作/修改操作/删除操作。都归结为update操作。
在这里插入图片描述

查询操作
  1. 查询所有
    在这里插入图片描述
  2. 查询一个
    在这里插入图片描述
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐