JDBC Template基本使用方法详解

1.使用maven引用依赖

<dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>

  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>5.2.5.RELEASE</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.19</version>
  </dependency>
 </dependencies>

2.编写spring的xml文件

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


  <!--配置dataSource-->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
  </bean>
  
<!--使用jdbcTemplate工具类-->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
</beans>

3.ddl操作(建表之类)

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class test {
@Test
  public void test(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("myApplication.xml");
    JdbcTemplate springTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
    springTemplate.execute("create table test(id int,username varchar(10))");
  }
}

4.增删改:

对应的使用例子

public void testUpdate(){
    String sql = "insert into student(name,sex) values(?,?)";
    jdbcTemplate.update(sql,new Object[]{"张飞","男"});
  }

  public void testUpdate2(){
    String sql = "update student set sex=? where id=?";
    jdbcTemplate.update(sql,"女",1003);
  }

  public void testBatchUpdate(){
    String[] sqls={
        "insert into student(name,sex) values('关羽','女')",
        "insert into student(name,sex) values('刘备','男')",
        "update student set sex='女' where id=2001"
    };
    jdbcTemplate.batchUpdate(sqls);
  }

  public void testBatchUpdate2(){
    String sql = "insert into selection(student,course) values(?,?)";
    List<Object[]> list = new ArrayList<Object[]>();
    list.add(new Object[]{1005,1001});
    list.add(new Object[]{1005,1003});
    jdbcTemplate.batchUpdate(sql,list);
  }

5.查询

public void testQuerySimple1(){
    String sql = "select count(*) from student";
    int count = jdbcTemplate.queryForObject(sql,Integer.class);
    System.out.println(count);
  }

  public void testQuerySimple2(){
    String sql = "select name from student where sex=?";
    List<String> names = jdbcTemplate.queryForList(sql,String.class,"女");
    System.out.println(names);
  }

  public void testQueryMap1(){
    String sql = "select * from student where id = ?";
    Map<String,Object> stu = jdbcTemplate.queryForMap(sql,1003);
    System.out.println(stu);
  }

  public void testQueryMap2(){
    String sql = "select * from student";
    List<Map<String,Object>> stus = jdbcTemplate.queryForList(sql);
    System.out.println(stus);
  }

  public void testQueryEntity1(){
    String sql = "select * from student where id = ?";
    Student stu = jdbcTemplate.queryForObject(sql, new StudentRowMapper(), 1004);
    System.out.println(stu);
  }
  @org.junit.Test
  public void testQueryEntity2(){
    String sql = "select * from student";
    List<Student> stus = jdbcTemplate.query(sql,new StudentRowMapper());
    System.out.println(stus);
  }

  private class StudentRowMapper implements RowMapper<Student>{
    public Student mapRow(ResultSet resultSet, int i) throws SQLException {
      Student stu = new Student();
      stu.setId(resultSet.getInt("id"));
      stu.setName(resultSet.getString("name"));
      stu.setSex(resultSet.getString("sex"));
      stu.setBorn(resultSet.getDate("born"));
      return stu;
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程(cainiaojc.com)。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。