学习笔记--JdbcTemplate

1.JdbcTemplate开发步骤

1)添加依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
      <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>5.3.13</version>
   </dependency>
 
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-tx</artifactId>
       <version>5.3.13</version>
   </dependency>

 

2)创建数据源对象

1
2
3
4
5
6
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456")   

  

3)设置数据源对象 执行操作

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

  

代码改进:

显然上面的方法用Spring注入的方法具有更好的结构。

在配置文件ApplicationContext.xml中,首先注入数据库类,再注入jdbc模板对象。

 

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 配置数据源对象  -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="${jdbc.driver}"/>
      <property name="jdbcUrl" value="${jdbc.url}"/>
      <property name="user" value="${jdbc.uername}"/>
      <property name="password" value="${jdbc.password}"/>
  </bean>
 
  <!--  jdbc模板对象  -->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"/>
  </bean>

  

再进行测试:

1
2
3
4
5
6
7
8
@Test
  //测试Spring产生jdbcTemplate对象
  public void test2(){
      ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
      JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");
      int row = jdbcTemplate.update("insert into account values(?,?)","zhangsan",4000);
      System.out.println(row);
  }

  

需要注意的是,我们不希望以后修改数据库再去更改xml配置,所以我们将xml中的set属性的变量剥离到properties文件中。

此配置需要在context命名空间下。

1
2
3
4
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.uername = root
jdbc.password = 123456

  

在xml中配置

1
<context:property-placeholder location="jdbc.properties"/>

  

2.JdbcTemplate常用操作

 

@ContextConfiguration 这个注解通常与 @RunWith(SpringJUnit4ClassRunner.class)联合使用用来测试

1
2
3
4
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
}

  

添加在测试类之上

2.1 更新操作

1
2
3
4
@Test
  public void testUpdate(){
      jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
  }

  

2.2 删除操作

1
2
3
4
@Test
  public void testDelete(){
      jdbcTemplate.update("delete from account where name=?","tom");
  }

  

2.3 查询全部操作

1
2
3
4
5
@Test
  public void testQueryAll(){
      List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
      System.out.println(accountList);
  }

  

jdbcTemplate.query 第一个参数为sql语句,第二个参数是new一个Bean属性映射类,负责将查询到的数据注入<>中的实体类,并强转成xxx.class类型。

返回的是一个集合。

 

2.4 查询单个操作

1
2
3
4
5
@Test
  public void testQueryOne(){
      Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "zhangsan");
      System.out.println(account);
  }

  

jdbcTemplate.queryForObject第一个参数为sql语句,第二参数同2.3,第三个参数是sql中占位符的内容。因为方法是queryForObject 返回的是注入的实体类对象。

 

2.5 查询数据数量

1
2
3
4
5
@Test
  public void testQueryCount(){
      Long count = jdbcTemplate.queryForObject("select count(*) from account", long.class);
      System.out.println(count);
  }

  

因为结果是一个数字,所以不需要使用映射器,第二个参数直接输入强转类型即可。

 

posted @   小超和你  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示