一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解在spring中使用JdbcTemplate操作數據庫的幾種方式

詳解在spring中使用JdbcTemplate操作數據庫的幾種方式

2020-07-22 11:52永遠喜歡由比濱結衣 Java教程

這篇文章主要介紹了詳解在spring中使用JdbcTemplate操作數據庫的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

使用JdbcTemplate的步驟

1、設置spring-jdbc和spring-tx的坐標(也就是導入依賴)

?
1
2
3
4
5
6
7
8
9
10
11
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.7.RELEASE</version>
  </dependency>
 
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.7.RELEASE</version>
  </dependency>

2、創建數據表和實體類

  • 創建數據表的過程省略
  • 創建實體類Account
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.jdbcTemplate.bean;
 
public class Account {
  private String name;
  private Double money;
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public Double getMoney() {
    return money;
  }
 
  public void setMoney(Double money) {
    this.money = money;
  }
 
  @Override
  public String toString() {
    return "Account{" +
        "name='" + name + '\'' +
        ", money=" + money +
        '}';
  }
}

3、創建數據源、JdbcTemplate對象

4、執行數據庫操作

實現3、4步的方法提供以下三種

方法一:代碼中直接配置數據源和數據對象

創建JdbcTemplate對象+執行jdbc語句

?
1
2
3
4
5
6
7
8
9
10
11
//創建數據源對象
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/think");
ds.setUsername("root");
ds.setPassword("");
//創建jdbcTemplate對象
JdbcTemplate jt = new JdbcTemplate();
//執行操作(插入操作)
jt.setDataSource(ds);
jt.execute("insert into account(name,money)value('EVA',50000)");

方法二:在resources目錄下配置xx.xml文件,對數據源、JdbcTemplate進行注入

配置xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    //配置數據源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/think"/>
      <property name="username" value="root"/>
      <property name="password" value=""/>
    </bean>
<!--  //配置jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"/>
    </bean>

使用配置操作數據庫

編寫test類測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//二、使用配置操作數據庫
    //1、獲取容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans5.xml");
    //2、獲取對象
    JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
    //、執行操作
//    jt.execute("insert into account(name,money)value ('Alice',2000)");
    //保存
    //jt.update("insert into account(name,money)value (?,?)","Eden",100);
    //更新
    // jt.update("update account set money=?,name=? where name=?",1000,"Kiroto","Eden");
    //刪除
    //jt.update("delete from account where name =? and money =?","Kiroto",1000);
    //查找
    List<Account> list = jt.query("select * from account where name =?",new BeanPropertyRowMapper<Account>(Account.class),"Eden");
    System.out.println(list.isEmpty()?"沒有查找結果":list.get(0));

方法三:使用接口實現

創建template接口和templateDAO接口實現類

接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.jdbcTemplate.test;
 
import com.jdbcTemplate.bean.Account;
 
public interface Template {
 
  Account find(String name);
 
  int update(Account account);
 
  int delete(Account account);
 
  int add(Account account);
}

接口實現類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.jdbcTemplate.test;
 
import com.jdbcTemplate.bean.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.util.List;
 
public class TemplateDAO implements Template {
  private JdbcTemplate jdbcTemplate;
 
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }
 
  public Account find(String name) {//查找
    List<Account> list = jdbcTemplate.query("select * from account where name=?",
        new BeanPropertyRowMapper<Account>(Account.class),name);
    return list.isEmpty()?null:list.get(0);
  }
 
  public int update(Account account) {//更新
 
    return jdbcTemplate.update("update account set money=? where name=?",
        account.getMoney(),account.getName());
  }
 
  public int delete(Account account) {//刪除
 
    return jdbcTemplate.update("delete from account where name =?",account.getName());
  }
 
  public int add(Account account) {//添加
    return jdbcTemplate.update("insert into account(name ,money)value (?,?)",account.getName(),account.getMoney());
  }
}

在測試之前,因為多了一個接口實現類,除了數據源和jdbcTemplate之外,應當在xml配置文件中多配置一個TemplateDAO

?
1
2
3
4
<!--  配置賬戶的持久層-->
  <bean id="templateDAO" class="com.jdbcTemplate.test.TemplateDAO">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
  </bean>

編寫測試類進行測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.jdbcTemplate.bean.Account;
import com.jdbcTemplate.test.Template;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class mytest {
  public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans6.xml");
    Template tp = ac.getBean("templateDAO",Template.class);//注意對比方法二的不同
    Account account = tp.find("Lily");
    System.out.println(account.toString());
 
  }
}

到此這篇關于詳解在spring中使用JdbcTemplate操作數據庫的幾種方式的文章就介紹到這了,更多相關spring JdbcTemplate操作數據庫內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家! 

原文鏈接:https://blog.csdn.net/L_GRAND_ORDER/article/details/107449883

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本特黄一级大片 | 国产精品视频第一区二区 | 国产欧美一区二区精品性色99 | 拍拍叫痛的无挡视频免费 | 九九99热久久999精品 | 午夜一级影院 | 人人爱天天做夜夜爽88 | av中文字幕网免费观看 | 贵妇的私人性俱乐部 | 暖暖日本在线观看免费 | 国产成人小视频 | 校草让我脱了内裤给全班看 | 91探花在线观看 | 天天快乐高清在线观看 | 欧美大奶艳星 | 欧美特黄aaaaaa | 肉文np高h| 日韩精品特黄毛片免费看 | sihu国产午夜精品一区二区三区 | 色淫阁小说 | 韩国美女豪爽一级毛片 | 美女草b | 美女把小内内脱个精光打屁屁 | 五月天婷婷精品免费视频 | 男人插女人软件 | 男人天堂网av | 91桃色视频在线观看 | 国产二区视频 | 日本xxxx69hd | porno中国xxxxx | 日韩在线中文字幕 | 欧美性色老妇人 | 毛片免 | 奇米影视中文字幕 | 性生大片免费看 | 国产在线99| 色菇凉天天综合网 | 天天综合亚洲 | 国产国语videosex另类 | 日本精工厂网址 | 欧美国产日韩在线 |