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

Spring中如何操作JDBC的實(shí)現(xiàn)

 更新時(shí)間:2019年10月18日 11:26:51   作者:~wangweijun  
這篇文章主要介紹了Spring中如何操作JDBC的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本篇文章介紹一下在Spring中如何使用JDBC,事實(shí)上,在Spring中使用JDBC和傳統(tǒng)的JDBC或者一些JDBC框架,如:DBUtils的使用沒有什么區(qū)別,所以Spring中使用JDBC是非常簡(jiǎn)單的。

獲取數(shù)據(jù)庫(kù)連接

在這之前,我們首先通過Spring獲得對(duì)數(shù)據(jù)庫(kù)的連接,創(chuàng)建一個(gè)Java項(xiàng)目,導(dǎo)入Spring、c3p0、數(shù)據(jù)庫(kù)驅(qū)動(dòng)的jar包即可,然后創(chuàng)建一個(gè)數(shù)據(jù)表做測(cè)試:

 create table user(
 id integer primary key auto_increment,
 name varchar(20),
 password varchar(20)
 );
 

接下來創(chuàng)建Spring的配置文件并作如下配置:

<?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:context="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 http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 <!-- 導(dǎo)入資源文件 -->
 <context:property-placeholder
 location="classpath:db.properties" />

 <!-- 配置C3P0數(shù)據(jù)源 -->
 <bean id="dataSource"
 class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="user" value="${jdbc.user}"></property>
 <property name="password" value="${jdbc.password}"></property>
 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 <property name="driverClass" value="${jdbc.driverClass}"></property>
 </bean>

</beans>

測(cè)試數(shù)據(jù)庫(kù)連接是否能夠成功獲取:

public class SpringJDBCTest {

 private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

 @Test
 public void testConnection() throws SQLException {
 DataSource dataSource = ctx.getBean(DataSource.class);
 System.out.println(dataSource.getConnection());
 }
}

運(yùn)行結(jié)果:

com.mchange.v2.c3p0.impl.NewProxyConnection@5d47c63f

能夠成功獲取到數(shù)據(jù)庫(kù)連接。

對(duì)數(shù)據(jù)表進(jìn)行更新操作

傳統(tǒng)的JDBC用法相信大家都很熟悉,在Spring中并沒有什么特別的,無非是將Bean的生命周期交給了IOC容器管理,而Spring框架獨(dú)立出了一套API用于數(shù)據(jù)庫(kù)操作(JDBCTemplate)。接下來分別測(cè)試一下常見的數(shù)據(jù)庫(kù)操作,先將JDBCTemplate類放入容器:

 <!-- 將JDBCTemplate放入容器 -->
 <bean id="jdbcTemplate"
 class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource"></property>
 </bean>

該類需要一個(gè)參數(shù),參數(shù)值指向剛才配置的dataSource即可,這樣我們就能夠使用JDBCTemplate類了,先測(cè)試一下插入操作:

 @Test
 public void testInsert() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "insert into user(name,password) values(?,?)";
 Object[] args = {"zhangsan","12345"};
 jdbcTemplate.update(sql,args);
 }
 

執(zhí)行代碼,查詢表結(jié)果,插入成功。

mysql> select * from user;
+----+----------+----------+
| id | name  | password |
+----+----------+----------+
| 1 | zhangsan | 12345 |
+----+----------+----------+
1 row in set (0.00 sec)

接著測(cè)試一下數(shù)據(jù)表的修改操作:

 @Test
 public void testUpdate() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "update user set password = ? where id = ?";
 Object[] args = {"admin","1"};
 jdbcTemplate.update(sql,args);
 }

執(zhí)行代碼,查詢表結(jié)果,修改成功。

mysql> select * from user;
+----+----------+----------+
| id | name  | password |
+----+----------+----------+
| 1 | zhangsan | admin |
+----+----------+----------+
1 row in set (0.00 sec)

數(shù)據(jù)表的刪除操作與其類似,不做重復(fù)測(cè)試,接下來我們測(cè)試一下批量操作:

 @Test
 public void testBatchInsert() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "insert into user(name,password) values(?,?)";
 List<Object[]> batchArgs = new ArrayList();
 batchArgs.add(new Object[]{"lisi","123"});
 batchArgs.add(new Object[]{"wangwu","1234"});
 batchArgs.add(new Object[]{"zhaoliu","12345"});
 jdbcTemplate.batchUpdate(sql, batchArgs);
 }
 

此時(shí)的一個(gè)對(duì)象數(shù)組即為一條記錄,批量操作則需要泛型為Object數(shù)組的集合。

執(zhí)行代碼,查詢表結(jié)果,批量插入成功。

mysql> select * from user;
+----+----------+----------+
| id | name  | password |
+----+----------+----------+
| 1 | zhangsan | admin |
| 2 | lisi  | 123  |
| 3 | wangwu | 1234  |
| 4 | zhaoliu | 12345 |
+----+----------+----------+
4 rows in set (0.00 sec)

最后是數(shù)據(jù)表的查詢操作,首先創(chuàng)建數(shù)據(jù)表對(duì)應(yīng)的User類:

package com.wwj.spring.jdbc;

public class User {

 private Integer id;
 private String name;
 private String password;

 public Integer getId() {
 return id;
 }

 public void setId(Integer id) {
 this.id = id;
 }

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getPassword() {
 return password;
 }

 public void setPassword(String password) {
 this.password = password;
 }

 @Override
 public String toString() {
 return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
 }
}

編寫測(cè)試代碼:

 @Test
 public void testGet() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "select id,name,password from user where id = ?";
 RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
 User user = jdbcTemplate.queryForObject(sql, rowMapper,1);
 System.out.println(user);
 }

運(yùn)行結(jié)果如下:

User [id=1, name=zhangsan, password=admin]

在Spring中,查詢的結(jié)果是交給了RowMapper來處理的,RowMapper用來指定映射結(jié)果集行數(shù)據(jù)的方式,相對(duì)于傳統(tǒng)的ResultSet來說,RowMapper省去了遍歷結(jié)果集的重復(fù)操作,從而使查詢變得更簡(jiǎn)單。

而查詢多條數(shù)據(jù)的方式如下:

 @Test
 public void testGetMore() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "select id,name,password from user where id > ?";
 RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
 List<User> userList = jdbcTemplate.query(sql, rowMapper,2);
 System.out.println(userList);
 }

運(yùn)行結(jié)果:

[User [id=3, name=wangwu, password=1234], User [id=4, name=zhaoliu, password=12345]]

有些同學(xué)可能會(huì)想當(dāng)然地認(rèn)為,查詢多條數(shù)據(jù)要調(diào)用queryForList()方法,事實(shí)上,調(diào)用的是query()方法。需要注意的是,JDBCTemplate提供的API不支持級(jí)聯(lián)屬性。

獲取數(shù)據(jù)表單列的值:

@Test
 public void testSingleValue() {
 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
 String sql = "select name from user where id = ?";
 String name = jdbcTemplate.queryForObject(sql, String.class,1);
 System.out.println(name);
 }
 

運(yùn)行結(jié)果如下:

zhangsan

整個(gè)增刪改查的過程其實(shí)都沒有任何難度,這和之前學(xué)的數(shù)據(jù)庫(kù)操作API區(qū)別并不大,所以也沒有特別需要講解的地方,通過一些測(cè)試代碼應(yīng)該就能夠理解了。

使用具名參數(shù)

在經(jīng)典的JDBC用法中,sql參數(shù)是用占位符"?"表示,并且受到位置的限制,定位參數(shù)的問題在于,一旦改變參數(shù)的順序,就必須改變參數(shù)的綁定方式。

為了解決這個(gè)問題,Spring為我們提供了另一種選擇:使用具名參數(shù)。

使用具名參數(shù)后,sql將按名稱而不是位置進(jìn)行指定,具名參數(shù)更易于后期維護(hù),也提升了代碼的可讀性,具名參數(shù)只在NamedParameterJdbcTemplate類中得到支持。

要想使用具名參數(shù),首先將NamedParameterJdbcTemplate放入容器:

 <!-- 將namedParameterJdbcTemplate放入容器 -->
 <bean id="namedParameterJdbcTemplate"
 class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
 <constructor-arg ref="dataSource"></constructor-arg>
 </bean>

該類沒有無參的構(gòu)造器,通常使用參數(shù)為DataSource類型的構(gòu)造器。

這里以插入數(shù)據(jù)舉例具名參數(shù)的使用:

 @Test
 public void testNamedParameterJdbcTemplate() {
 NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate");
 String sql = "insert into user(name,password) values(:name,:psd)";
 Map<String, Object> map = new HashMap<String, Object>();
 map.put("name", "wangweijun");
 map.put("psd", "112233");
 namedParameterJdbcTemplate.update(sql,map);
 }

執(zhí)行代碼,查詢表結(jié)果,插入成功。

mysql> select * from user;
+----+------------+----------+
| id | name  | password |
+----+------------+----------+
| 1 | zhangsan | admin |
| 2 | lisi  | 123  |
| 3 | wangwu  | 1234  |
| 4 | zhaoliu | 12345 |
| 5 | wangweijun | 112233 |
+----+------------+----------+
5 rows in set (0.00 sec)

雖然具名參數(shù)有很多好處,但無形中也增加了很多代碼,可以說比之前的方式更加復(fù)雜。

當(dāng)然,它還有更簡(jiǎn)單的實(shí)現(xiàn)方式:

 @Test
 public void testSimpleNamedParameterJdbcTemplate() {
 NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate");
 String sql = "insert into user(name,password) values(:name,:password)";
 User user = new User();
 user.setName("zhaoliu");
 user.setPassword("223344");
 SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
 namedParameterJdbcTemplate.update(sql, paramSource);
 }

執(zhí)行代碼,查詢表結(jié)果,插入成功。

mysql> select * from user;
+----+------------+----------+
| id | name  | password |
+----+------------+----------+
| 1 | zhangsan | admin |
| 2 | lisi  | 123  |
| 3 | wangwu  | 1234  |
| 4 | zhaoliu | 12345 |
| 5 | wangweijun | 112233 |
| 6 | zhaoliu | 223344 |
+----+------------+----------+
6 rows in set (0.00 sec)

這種方式實(shí)現(xiàn)了通過對(duì)象自動(dòng)匹配具名參數(shù),前提是具名參數(shù)的參數(shù)名要和類中的屬性名相同,該方式相對(duì)于第一種方式要更合理一些,因?yàn)閺那芭_(tái)傳遞過來的就是對(duì)象,這樣就可以直接通過對(duì)象進(jìn)行處理而無需其它操作。

項(xiàng)目源碼
https://github.com/blizzawang/spring_jdbc

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

相關(guān)文章

  • Feign Client 超時(shí)時(shí)間配置不生效的解決

    Feign Client 超時(shí)時(shí)間配置不生效的解決

    這篇文章主要介紹了Feign Client 超時(shí)時(shí)間配置不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java語言自行實(shí)現(xiàn)ULID過程底層原理詳解

    java語言自行實(shí)現(xiàn)ULID過程底層原理詳解

    這篇文章主要為大家介紹了java語言自行實(shí)現(xiàn)ULID過程底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Spring Security基于json登錄實(shí)現(xiàn)過程詳解

    Spring Security基于json登錄實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Spring Security基于json登錄實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot集成quartz實(shí)現(xiàn)定時(shí)任務(wù)

    SpringBoot集成quartz實(shí)現(xiàn)定時(shí)任務(wù)

    這篇文章主要介紹了如何使用SpringBoot整合Quartz,并將定時(shí)任務(wù)寫入庫(kù)中(持久化存儲(chǔ)),還可以任意對(duì)定時(shí)任務(wù)進(jìn)行如刪除、暫停、恢復(fù)等操作,需要的可以了解下
    2023-09-09
  • CyclicBarrier線程同步共享變量底層原理示例解析

    CyclicBarrier線程同步共享變量底層原理示例解析

    這篇文章主要為大家介紹了CyclicBarrier線程同步共享變量底層原理示例解析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 詳解RestTemplate的三種使用方式

    詳解RestTemplate的三種使用方式

    這篇文章主要介紹了詳解RestTemplate的三種使用方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • IDEA啟動(dòng)Springboot報(bào)錯(cuò):無效的目標(biāo)發(fā)行版:17 的解決辦法

    IDEA啟動(dòng)Springboot報(bào)錯(cuò):無效的目標(biāo)發(fā)行版:17 的解決辦法

    這篇文章主要給大家介紹了IDEA啟動(dòng)Springboot報(bào)錯(cuò):無效的目標(biāo)發(fā)行版:17 的解決辦法,文中通過代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • 解析Mybatis延遲加載問題

    解析Mybatis延遲加載問題

    這篇文章主要介紹了Mybatis的延遲加載問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • SpringBoot 增量部署發(fā)布的實(shí)現(xiàn)步驟

    SpringBoot 增量部署發(fā)布的實(shí)現(xiàn)步驟

    本文介紹了通過拆分項(xiàng)目jar包和使用類加載器實(shí)現(xiàn)Spring Boot的增量部署,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)XML與JSON的互相轉(zhuǎn)換,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03

最新評(píng)論

新河县| 丹棱县| 阿克陶县| 洪雅县| 瑞丽市| 常德市| 三江| 高州市| 旅游| 定陶县| 科尔| 聂荣县| 西城区| 三台县| 鹰潭市| 镇远县| 菏泽市| 永福县| 荔波县| 武胜县| 张掖市| 阳曲县| 嘉义市| 师宗县| 孟州市| 佳木斯市| 洪泽县| 三穗县| 福鼎市| 泸州市| 宜兰市| 北流市| 松溪县| 阆中市| 鹿邑县| 囊谦县| 陆河县| 司法| 万荣县| 察雅县| 石棉县|