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

JdbcTemplate方法介紹與增刪改查操作實(shí)現(xiàn)

 更新時(shí)間:2019年11月02日 14:42:02   作者:農(nóng)農(nóng)  
這篇文章主要給大家介紹了關(guān)于JdbcTemplate方法與增刪改查操作實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用JdbcTemplate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

JdbcTemplate介紹

為了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定義了一個(gè)抽象層, 以此建立一個(gè)JDBC存取框架,Spring Boot Spring Data-JPA。

作為 SpringJDBC 框架的核心, JDBC 模板的設(shè)計(jì)目的是為不同類型的JDBC操作提供模板方法. 每個(gè)模板方法都能控制整個(gè)過程,并允許覆蓋過程中的特定任務(wù)。

通過這種方式,可以在盡可能保留靈活性的情況下,將數(shù)據(jù)庫(kù)存取的工作量降到最低。

JdbcTemplate方法介紹

JdbcTemplate主要提供以下五類方法:

1、execute方法:可以用于執(zhí)行任何SQL語(yǔ)句,一般用于執(zhí)行DDL語(yǔ)句;

       Execute、executeQuery、executeUpdate

2、update方法及batchUpdate方法:update方法用于執(zhí)行新增、修改、刪除等語(yǔ)句;batchUpdate方法用于執(zhí)行批處理相關(guān)語(yǔ)句 SQL SERVCER(GO SQL語(yǔ)句 GO) ;

3、query方法及queryForXXX方法:用于執(zhí)行查詢相關(guān)語(yǔ)句;

4、call方法:用于執(zhí)行存儲(chǔ)過程、函數(shù)相關(guān)語(yǔ)句。

JdbcTemplate實(shí)現(xiàn)增刪改查

JdbcTemplate添加數(shù)據(jù)

1. 使用配置實(shí)現(xiàn)

1.1 創(chuàng)建實(shí)體類

public class Account {
 private Integer accountid;
 private String accountname;
 private Double balance;

 public Integer getAccountid() {
 return accountid;
 }

 public void setAccountid(Integer accountid) {
 this.accountid = accountid;
 }

 public String getAccountname() {
 return accountname;
 }

 public void setAccountname(String accountname) {
 this.accountname = accountname;
 }

 public Double getBalance() {
 return balance;
 }

 public void setBalance(Double balance) {
 this.balance = balance;
 }
}

實(shí)體類

1.2 創(chuàng)建Dao層

//查詢所有所有賬戶
public List<Account> getAllAccounts();

.3 創(chuàng)建Dao層實(shí)現(xiàn)類并繼承JdbcDaoSupport接口

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {


 @Override
 public List<Account> getAllAccounts() {
 JdbcTemplate jdbcTemplate = getJdbcTemplate();
 String sql = "select * from accounts";

 //RowMapper:接口 封裝了記錄的行映射關(guān)系
 List<Account> lists = jdbcTemplate.query(sql, new RowMapper<Account>() {


  @Override
  public Account mapRow(ResultSet resultSet, int i) throws SQLException {
  //創(chuàng)建Account對(duì)象
  Account account = new Account();
  //從ResultSet中解數(shù)據(jù)保到Accounts對(duì)象中
  account.setAccountid(resultSet.getInt("accountid"));
  account.setAccountname(resultSet.getString("accountname"));
  account.setBalance(resultSet.getDouble("balance"));

  return account;
  }
 });

return account;

}

實(shí)現(xiàn)查詢方法

1.4創(chuàng)建Service層

//查詢所有所有賬戶
public List<Account> getAllAccounts();

1.5創(chuàng)建Service層實(shí)現(xiàn)類

AccountDao accountDao;
@Override
public List<Account> getAllAccounts() {
 List<Account> allAccounts = accountDao.getAllAccounts();
 return allAccounts;
}

1.6 編寫applicationContext.xml文件

<!--識(shí)別到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置數(shù)據(jù)源-->
<!--spring內(nèi)置的數(shù)據(jù)源:提供連接的,不負(fù)責(zé)管理,使用連接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driver}"></property>
 <property name="url" value="${jdbc.url}"></property>
 <property name="username" value="${jdbc.username}"></property>
 <property name="password" value="${jdbc.password}"></property>
</bean>
<!--構(gòu)建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="accountDao" class="cn.spring.accounttest.dao.impl.AccountDaoImpl">
 <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--Service-->
<bean id="accountService" class="cn.spring.accounttest.service.impl.AccountServiceImpl">
 <property name="accountDao" ref="accountDao"></property>
</bean>

applicationContext.xml

1.7編寫測(cè)試類 

@Test
public void getAllAccount(){
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 //從spring容器中獲取Service對(duì)象
 AccountService accountService = (AccountService)context.getBean("accountService");
 List<Account> allAccounts = accountService.getAllAccounts();
 for (Account account:allAccounts) {
 System.out.println("賬戶名:"+account.getAccountname()+",余額為:"+account.getBalance());
 }
}

查詢測(cè)試類

2. 使用注解方式實(shí)現(xiàn)

2.1 創(chuàng)建實(shí)體類 

實(shí)體類

2.2 創(chuàng)建Dao層

查詢方法

2.3 創(chuàng)建Dao層實(shí)現(xiàn)類

@Repository
public class AccountDaoImpl implements AccountDao {

 @Autowired
 private JdbcTemplate jdbcTemplate;

 Account account = new Account();
 @Override
 public List<Account> getAllAccounts() {

  String sql = "select * from accounts";

  
  //自動(dòng)映射
  RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
  List<Account> query = jdbcTemplate.query(sql, rowMapper);
  for (Account account : query) {
   System.out.println(account);
  }
  return query;
  }
 }

Dao實(shí)現(xiàn)類

2.4創(chuàng)建Service層

查詢方法

2.5創(chuàng)建Service層實(shí)現(xiàn)類

實(shí)現(xiàn)查詢方法

2.6編寫applicationContext.xml文件

<!--掃描注解:包掃描器-->
<context:component-scan base-package="cn.spring"/>

<!--識(shí)別到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置數(shù)據(jù)源-->
<!--spring內(nèi)置的數(shù)據(jù)源:提供連接的,不負(fù)責(zé)管理,使用連接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driver}"></property>
 <property name="url" value="${jdbc.url}"></property>
 <property name="username" value="${jdbc.username}"></property>
 <property name="password" value="${jdbc.password}"></property>
</bean>
<!--構(gòu)建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource"></property>
</bean>

applicationContext.xml

2.7編寫測(cè)試類

查詢測(cè)試類

JdbcTemplate實(shí)現(xiàn)增刪改操作

使用注解方式實(shí)現(xiàn),配置式同添加操作

1.創(chuàng)建Dao層

//刪除賬戶
public int delAccount(int id);

//添加用戶
public int addAccount(Account account);

//修改賬戶
public int updaAccount(Account account);

增刪改方法

2.創(chuàng)建Dao曾實(shí)現(xiàn)類

@Override
 public int delAccount(int id) {

  String sql="delete from accounts where accountid=2";
  int count = jdbcTemplate.update(sql);
  return count;
 }

@Override
 public int addAccount(Account account) {
  String sql="insert into Accounts(accountname,balance) values(?,?)";
  int count = jdbcTemplate.update(sql,account.getAccountname(),account.getBalance());
  return count;
 }

 @Override
 public int updaAccount(Account account) {
  String sql="update accounts set accountname=?,balance=? where accountid=?";
  int count = jdbcTemplate.update(sql, account.getAccountname(),account.getBalance(),account.getAccountid() );
  return count;
 }

增刪改方法實(shí)現(xiàn)類

3. 創(chuàng)建Service層

4. 創(chuàng)建Service層實(shí)現(xiàn)類

5. 編寫applicationContext.xml文件

6. 編寫測(cè)試類

@Test
public void delAccount(){
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 AccountService accountService =(AccountService) context.getBean("accountServiceImpl");
 int i = accountService.delAccount(2);
 if (i>0){
  System.out.println("刪除成功");
 }
}

@Test
public void addAccount(){
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
 Account account=new Account();
 account.setAccountname("劉磊");
 account.setBalance(Double.valueOf(784));
 int count = accountServiceImpl.addAccount(account);
 if (count>0){
  System.out.println("添加成功");
 }
}

@Test
public void updaAcccount(){
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
 Account account=new Account();
 account.setAccountid(10);
 account.setAccountname("劉磊");
 account.setBalance(Double.valueOf(784));
 int count = accountServiceImpl.updaAccount(account);
 if (count>0){
  System.out.println("修改成功");
 }
}

增刪改測(cè)試類

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr)

    IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr)

    這篇文章主要介紹了IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • springboot新建項(xiàng)目jdk只有17/21,無法選中1.8解決辦法

    springboot新建項(xiàng)目jdk只有17/21,無法選中1.8解決辦法

    最近博主也有創(chuàng)建springboot項(xiàng)目,發(fā)現(xiàn)了IntelliJ IDEA在通過Spring Initilizer初始化項(xiàng)目的時(shí)候已經(jīng)沒有java8版本的選項(xiàng)了,這里給大家總結(jié)下,這篇文章主要給大家介紹了springboot新建項(xiàng)目jdk只有17/21,無法選中1.8的解決辦法,需要的朋友可以參考下
    2023-12-12
  • 簡(jiǎn)單聊一聊Spring中Bean別名的處理原理

    簡(jiǎn)單聊一聊Spring中Bean別名的處理原理

    今天來和小伙伴們聊一聊 Spring 中關(guān)于 Bean 別名的處理邏輯,別名,顧名思義就是給一個(gè) Bean 去兩個(gè)甚至多個(gè)名字,整體上來說,在 Spring 中,有兩種不同的別名定義方式,感興趣的小伙伴跟著小編一起來看看吧
    2023-09-09
  • Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例

    Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例

    這篇文章主要介紹了Spring實(shí)現(xiàn)擁有者權(quán)限驗(yàn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    SpringBoot接口中如何直接返回圖片數(shù)據(jù)

    這篇文章主要介紹了SpringBoot接口中如何直接返回圖片數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java后端訪問https證書的問題及解決

    java后端訪問https證書的問題及解決

    這篇文章主要介紹了java后端訪問https證書的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Installij IDEA install或clean項(xiàng)目的使用

    Installij IDEA install或clean項(xiàng)目的使用

    這篇文章主要介紹了Installij IDEA install或clean項(xiàng)目的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解如何有效地處理Java中的多線程

    詳解如何有效地處理Java中的多線程

    在現(xiàn)代軟件開發(fā)中,多線程編程已成為提高程序性能和響應(yīng)速度的重要手段,Java提供了豐富的多線程支持,使得在Java中實(shí)現(xiàn)并發(fā)操作變得相對(duì)簡(jiǎn)單,本文將深入探討Java多線程編程的基本概念、常見問題和最佳實(shí)踐,需要的朋友可以參考下
    2024-06-06
  • SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn)

    SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn)

    本文主要介紹了SpringSecurity+jwt+redis基于數(shù)據(jù)庫(kù)登錄認(rèn)證的實(shí)現(xiàn),其中也涉及到自定義的過濾器和處理器,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Java?Unsafe創(chuàng)建對(duì)象的方法實(shí)現(xiàn)

    Java?Unsafe創(chuàng)建對(duì)象的方法實(shí)現(xiàn)

    Java中使用Unsafe實(shí)例化對(duì)象是一項(xiàng)十分有趣而且強(qiáng)大的功能,本文主要介紹了Java?Unsafe創(chuàng)建對(duì)象的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論

怀仁县| 乾安县| 肇东市| 托克逊县| 通河县| 广宗县| 吴忠市| 鲁甸县| 东兰县| 马尔康县| 淮南市| 綦江县| 慈溪市| 西充县| 宜兰市| 江西省| 理塘县| 葵青区| 新沂市| 嫩江县| 宝丰县| 武冈市| 三门县| 丰县| 土默特右旗| 施秉县| 大厂| 朔州市| 梓潼县| 商河县| 巴林右旗| 二连浩特市| 江口县| 安岳县| 通山县| 鄂伦春自治旗| 嘉义市| 三明市| 龙里县| 会同县| 尼木县|