电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

使用JdbcTemplate的步驟

1、設(shè)置spring-jdbc和spring-tx的坐標(biāo)(也就是導(dǎo)入依賴)

<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ù)表和實(shí)體類

創(chuàng)建數(shù)據(jù)表的過程省略 創(chuàng)建實(shí)體類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ù)庫操作

實(shí)現(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進(jìn)行注入

配置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()?'沒有查找結(jié)果':list.get(0));

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

創(chuàng)建template接口和templateDAO接口實(shí)現(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);}

接口實(shí)現(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()); }}

在測試之前,因?yàn)槎嗔艘粋€(gè)接口實(shí)現(xiàn)類,除了數(shù)據(jù)源和jdbcTemplate之外,應(yīng)當(dāng)在xml配置文件中多配置一個(gè)TemplateDAO

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

編寫測試類進(jìn)行測試

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

到此這篇關(guān)于詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫的幾種方式的文章就介紹到這了,更多相關(guān)spring JdbcTemplate操作數(shù)據(jù)庫內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 北京京云律师事务所 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 土壤养分检测仪_肥料养分检测仪_土壤水分检测仪-山东莱恩德仪器 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 洗地机-全自动/手推式洗地机-扫地车厂家_扬子清洁设备 | 茅茅虫AI论文写作助手-免费AIGC论文查重_写毕业论文降重 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 | 方源木业官网-四川木门-全国木门专业品牌 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 高清视频编码器,4K音视频编解码器,直播编码器,流媒体服务器,深圳海威视讯技术有限公司 | 菲希尔X射线测厚仪-菲希尔库伦法测厚仪-无锡骏展仪器有限责任公司 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | 信阳市建筑勘察设计研究院有限公司 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | 派财经_聚焦数字经济内容服务平台| 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 珠宝展柜-玻璃精品展柜-首饰珠宝展示柜定制-鸿钛展柜厂家 | 螺杆真空泵_耐腐蚀螺杆真空泵_水环真空泵_真空机组_烟台真空泵-烟台斯凯威真空 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 耙式干燥机_真空耙式干燥机厂家-无锡鹏茂化工装备有限公司 |