欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JDBC Template基本使用方法詳解

 更新時(shí)間:2020年06月11日 14:32:19   作者:shouyaya  
這篇文章主要介紹了JDBC Template基本使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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.增刪改:

對(duì)應(yīng)的使用例子

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('關(guān)羽','女')",
        "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;
    }
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決Springboot項(xiàng)目啟動(dòng)后自動(dòng)創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案

    解決Springboot項(xiàng)目啟動(dòng)后自動(dòng)創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案

    這篇文章主要介紹了解決Springboot項(xiàng)目啟動(dòng)后自動(dòng)創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Java手寫Redis服務(wù)端的實(shí)現(xiàn)

    Java手寫Redis服務(wù)端的實(shí)現(xiàn)

    本文主要介紹了Java手寫Redis服務(wù)端的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 如何在SpringBoot中使用Spring-AOP實(shí)現(xiàn)接口鑒權(quán)

    如何在SpringBoot中使用Spring-AOP實(shí)現(xiàn)接口鑒權(quán)

    這篇文章主要介紹了如何在SpringBoot中使用Spring-AOP實(shí)現(xiàn)接口鑒權(quán),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-09-09
  • Java SpringMVC攔截器與異常處理機(jī)制詳解分析

    Java SpringMVC攔截器與異常處理機(jī)制詳解分析

    SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡?qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡(jiǎn)化開發(fā),SpringMVC也是要簡(jiǎn)化我們?nèi)粘eb開發(fā)
    2021-10-10
  • java中子類繼承父類,程序運(yùn)行順序的深入分析

    java中子類繼承父類,程序運(yùn)行順序的深入分析

    本篇文章是對(duì)java中子類繼承父類,程序運(yùn)行順序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題

    Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題

    當(dāng)進(jìn)行業(yè)務(wù)操作時(shí),訂單發(fā)生異常 ,進(jìn)行了回滾操作,因?yàn)樵诓煌臄?shù)據(jù)庫實(shí)例中,余額卻扣除成功,此時(shí)發(fā)現(xiàn)數(shù)據(jù)不一致問題,本文給大家介紹Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題,感興趣的朋友一起看看吧
    2023-11-11
  • 在Java中實(shí)現(xiàn)二叉搜索樹的全過程記錄

    在Java中實(shí)現(xiàn)二叉搜索樹的全過程記錄

    二叉樹包含了根節(jié)點(diǎn),孩子節(jié)點(diǎn),葉節(jié)點(diǎn),每一個(gè)二叉樹只有一個(gè)根節(jié)點(diǎn),每一個(gè)結(jié)點(diǎn)最多只有兩個(gè)節(jié)點(diǎn),左子樹的鍵值小于根的鍵值,右子樹的鍵值大于根的鍵值,下面這篇文章主要給大家介紹了關(guān)于如何在Java中實(shí)現(xiàn)二叉搜索樹的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Java集合功能與用法實(shí)例詳解

    Java集合功能與用法實(shí)例詳解

    這篇文章主要介紹了Java集合功能與用法,結(jié)合實(shí)例形式詳細(xì)分析了java集合的基本概念、功能、原理、操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • mybatis-plus實(shí)體類主鍵策略有3種(小結(jié))

    mybatis-plus實(shí)體類主鍵策略有3種(小結(jié))

    這篇文章主要介紹了mybatis-plus實(shí)體類主鍵策略有3種(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • spring整合shiro框架的實(shí)現(xiàn)步驟記錄

    spring整合shiro框架的實(shí)現(xiàn)步驟記錄

    Shiro是一個(gè)強(qiáng)大易用的Java安全框架,提供了認(rèn)證、授權(quán)、加密和會(huì)話管理等功能。下面這篇文章主要給大家介紹了關(guān)于spring整合shiro框架的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05

最新評(píng)論