亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式

瀏覽:4日期:2023-08-25 15:03:38

使用JdbcTemplate的步驟

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

<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、創(chuàng)建數(shù)據(jù)表和實體類

創(chuàng)建數(shù)據(jù)表的過程省略 創(chuàng)建實體類Account

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、創(chuàng)建數(shù)據(jù)源、JdbcTemplate對象

4、執(zhí)行數(shù)據(jù)庫操作

實現(xiàn)3、4步的方法提供以下三種

方法一:代碼中直接配置數(shù)據(jù)源和數(shù)據(jù)對象

創(chuàng)建JdbcTemplate對象+執(zhí)行jdbc語句

//創(chuàng)建數(shù)據(jù)源對象 DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName('com.mysql.jdbc.Driver'); ds.setUrl('jdbc:mysql://localhost:3306/think'); ds.setUsername('root'); ds.setPassword(''); //創(chuàng)建jdbcTemplate對象 JdbcTemplate jt = new JdbcTemplate(); //執(zhí)行操作(插入操作) jt.setDataSource(ds); jt.execute('insert into account(name,money)value(’EVA’,50000)');

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

配置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' 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'><!-- //配置數(shù)據(jù)源--> <bean 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 class='org.springframework.jdbc.core.JdbcTemplate'> <property name='dataSource' ref='dataSource'/> </bean>

使用配置操作數(shù)據(jù)庫

編寫test類測試

//二、使用配置操作數(shù)據(jù)庫 //1、獲取容器 ApplicationContext ac = new ClassPathXmlApplicationContext('beans5.xml'); //2、獲取對象 JdbcTemplate jt = ac.getBean('jdbcTemplate',JdbcTemplate.class); //、執(zhí)行操作// 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));

方法三:使用接口實現(xiàn)

創(chuàng)建template接口和templateDAO接口實現(xiàn)類

接口

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);}

接口實現(xiàn)類

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()); }}

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

<!-- 配置賬戶的持久層--> <bean class='com.jdbcTemplate.test.TemplateDAO'> <property name='jdbcTemplate' ref='jdbcTemplate'/> </bean>

編寫測試類進行測試

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操作數(shù)據(jù)庫的幾種方式的文章就介紹到這了,更多相關spring JdbcTemplate操作數(shù)據(jù)庫內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關文章:
主站蜘蛛池模板: 91探花视频在线观看 | 麻豆成人在线 | 国产91精品在线播放 | 成人黄页网站免费观看大全 | 免费又黄又爽又猛大片午夜 | 久久精品道一区二区三区 | 欧美一区二区三区在线可观看 | 激情久久久久久久久久 | 一区二区三区免费在线视频 | 亚洲欧美久久婷婷爱综合一区天堂 | 婷婷色香 | 亚洲精选在线观看 | 欧美成人精品第一区二区三区 | 免费在线观看a级片 | 日本午夜一级特黄毛片 | 可以免费看黄的网站 | 在线免费观看色片 | 日本一道免费一区二区三区 | 欧美一区二区在线播放 | 国产日韩欧美综合一区 | 国产精品99久久久久久人 | 丝袜制服国产 | 欧美成人伊人久久综合网 | 综合玖玖 | 任你操网站 | 国产黄色片在线免费观看 | 欧美三级a做爰在线观看 | 特黄特级a级黄毛片免费观看多人 | 欧美成人亚洲高清在线观看 | 精品国产区一区二区三区在线观看 | 澳门毛片精品一区二区三区 | 在线观看国产一区二区三区99 | 欧美三级第一页 | 一区在线看 | 中文字幕 亚洲 一区二区三区 | 色视频网站人成免费 | 日本精品在线观看视频 | 草草视频在线免费观看 | 高清在线一区二区三区亚洲综合 | 亚洲日韩精品欧美一区二区一 | 亚洲欧美一区二区三区在线播放 |