最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JDBC Template基本使用方法詳解

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

1.使用maven引用依賴(lài)

<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.編寫(xiě)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工具類(lèi)-->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
</beans>

3.ddl操作(建表之類(lèi))

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.查詢(xún)

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)文章

  • Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例

    Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例

    本篇文章主要介紹了Java使用HttpClient實(shí)現(xiàn)Post請(qǐng)求實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • SpringBoot集成ENC對(duì)配置文件進(jìn)行加密的流程步驟

    SpringBoot集成ENC對(duì)配置文件進(jìn)行加密的流程步驟

    Spring Boot Encoder,即Spring Boot加密模塊,它提供了一種簡(jiǎn)單的方式來(lái)集成安全編碼功能到Spring Boot應(yīng)用程序中,它是Spring Security框架的一部分,旨在幫助開(kāi)發(fā)者輕松地處理數(shù)據(jù)加密,本文給大家介紹了SpringBoot集成ENC對(duì)配置文件進(jìn)行加密的流程步驟
    2024-12-12
  • Java導(dǎo)出oracle表結(jié)構(gòu)實(shí)例詳解

    Java導(dǎo)出oracle表結(jié)構(gòu)實(shí)例詳解

    這篇文章主要介紹了 Java導(dǎo)出oracle表結(jié)構(gòu)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • java使用pdfbox操作pdf文件示例

    java使用pdfbox操作pdf文件示例

    有時(shí)候PDF中的文字無(wú)法復(fù)制,這可能是因?yàn)镻DF文件加密了,不過(guò)使用PDFBox開(kāi)源軟件就可以把它讀出來(lái),下面是使用示例
    2014-03-03
  • SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出

    SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出

    這篇文章主要介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出的方法,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下
    2021-05-05
  • Java內(nèi)存泄漏問(wèn)題排查與解決

    Java內(nèi)存泄漏問(wèn)題排查與解決

    大家好,本篇文章主要講的是Java內(nèi)存泄漏問(wèn)題排查與解決,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-01-01
  • Spring Cloud 中@FeignClient注解中的contextId屬性詳解

    Spring Cloud 中@FeignClient注解中的contextId屬性詳解

    這篇文章主要介紹了Spring Cloud 中@FeignClient注解中的contextId屬性詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot熱部署配置過(guò)程

    SpringBoot熱部署配置過(guò)程

    這篇文章主要介紹了SpringBoot熱部署配置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • spring6+JDK17實(shí)現(xiàn)SSM起步配置文件

    spring6+JDK17實(shí)現(xiàn)SSM起步配置文件

    本文介紹了使用Spring6和JDK17配置SSM(Spring + Spring MVC + MyBatis)框架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • 一段代碼搞懂關(guān)于Java中List、Set集合及Map的使用

    一段代碼搞懂關(guān)于Java中List、Set集合及Map的使用

    這篇文章主要介紹了關(guān)于Java中List、Set集合及Map的使用及l(fā)ist,set和map三者的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

吉木萨尔县| 凌云县| 苍南县| 琼结县| 恭城| 汨罗市| 祥云县| 娱乐| 万安县| 阆中市| 祁门县| 平南县| 吴堡县| 河北省| 博兴县| 长春市| 潍坊市| 东阿县| 兴化市| 姜堰市| 太原市| 西城区| 邢台县| 顺义区| 清涧县| 雷山县| 巫溪县| 信阳市| 都昌县| 建湖县| 梁河县| 永春县| 金湖县| 资阳市| 安仁县| 合肥市| 九台市| 万盛区| 岱山县| 永仁县| 莱芜市|