• 中文
    • English
  • 注册
  • 查看作者
    • Spring:JdbcTemplate简单使用

      一.  添加依赖

      分别添加以下依赖:

      commons-logging-1.2.jar
      druid-1.1.6.jar
      mysql-connector-java-5.1.47.jar
      spring-beans-5.1.8.RELEASE.jar
      spring-context-5.1.8.RELEASE.jar
      spring-core-5.1.8.RELEASE.jar
      spring-expression-5.1.8.RELEASE.jar
      spring-jdbc-5.1.8.RELEASE.jar
      spring-tx-5.1.8.RELEASE.jar

      二.  配置数据库连接池

      将applicationContext.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"
             xmlns:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
      
      <!--    读取properties文件-->
          <content:property-placeholder location="jdbc.properties" />
      
          <bean id = "dataSource" scope="prototype" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driver}" />
              <property name="url" value="${jdbc.url}"/>
              <property name="username" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
      
      
      </beans>

      三.  配置JdbcTemplate

      Spring针对各种数据库都提供了模板工具来完成数据的操作,即Template:

      在将applicationContext.xml文件的Beans标签中添加以下内容:

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

      其中dataSource即第二节中我们配置的数据库连接池。

      四.  测试类

      package io.zhangjia.test;
      
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import org.springframework.jdbc.core.JdbcTemplate;
      
      
      import javax.sql.DataSource;
      import java.sql.Connection;
      import java.sql.SQLException;
      import java.util.List;
      import java.util.Map;
      
      public class Test {
          public static void main(String[] args) throws SQLException {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              //获取JdbcTemplate
              JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
              String sql = "SELECT * FROM BOOK";
               //使用JdbcTemplate获取数据
              List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
              System.out.println("maps = " + maps);
              context.close();
          }
      }
      输出:
      maps = [{BOOK_ID=1, NAME=西游记, AUTHOR=吴承恩, PRICE=10.1}, {BOOK_ID=2, NAME=水浒站, AUTHOR=施耐庵, PRICE=19.9}]

      在上面的例子中,我们获取的结果是一个Map,我们还可以将结果集转换为实体对象,即Book:

      public class Test {
          public static void main(String[] args) throws SQLException {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
              String sql = "SELECT * FROM BOOK";
      
              List<Book> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
              System.out.println("query = " + query);
              context.close();
          }
      }

      如果你的实体类属性名和数据库的列名没有遵守相应的规范,则上述方法将会无法获取正确的数据,我们可以通过手动使用mapRow方法,完成结果集转换为实体对象:

      public class Test {
          public static void main(String[] args) throws SQLException {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
              String sql = "SELECT * FROM BOOK";
              List<Book> query = jdbcTemplate.query(sql, new RowMapper<Book>() {
                  @Override
                  public Book mapRow(ResultSet resultSet, int i) throws SQLException {
                      Book book = new Book();
                      book.setBookId(resultSet.getInt("book_id"));
                      book.setName(resultSet.getString("name"));
                      book.setAuthor(resultSet.getString("author"));
                      book.setPrice(resultSet.getDouble("price"));
                      return book;
                  }
              });
              System.out.println("query = " + query);
              context.close();
          }
      }


    • 0
    • 0
    • 0
    • 1.8k
    • 请登录之后再进行评论

      登录

      赞助本站

      • 支付宝
      • 微信
      • QQ

      感谢一直支持本站的所有人!

      单栏布局 侧栏位置: