詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫(kù)的幾種方式
使用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對(duì)象
4、執(zhí)行數(shù)據(jù)庫(kù)操作
實(shí)現(xiàn)3、4步的方法提供以下三種
方法一:代碼中直接配置數(shù)據(jù)源和數(shù)據(jù)對(duì)象
創(chuàng)建JdbcTemplate對(duì)象+執(zhí)行jdbc語(yǔ)句
//創(chuàng)建數(shù)據(jù)源對(duì)象 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對(duì)象 JdbcTemplate jt = new JdbcTemplate(); //執(zhí)行操作(插入操作) jt.setDataSource(ds); jt.execute("insert into account(name,money)value('EVA',50000)");
方法二:在resources目錄下配置xx.xml文件,對(duì)數(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 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>
使用配置操作數(shù)據(jù)庫(kù)
編寫test類測(cè)試
//二、使用配置操作數(shù)據(jù)庫(kù) //1、獲取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("beans5.xml"); //2、獲取對(duì)象 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()); } }
在測(cè)試之前,因?yàn)槎嗔艘粋€(gè)接口實(shí)現(xiàn)類,除了數(shù)據(jù)源和jdbcTemplate之外,應(yīng)當(dāng)在xml配置文件中多配置一個(gè)TemplateDAO
<!-- 配置賬戶的持久層--> <bean id="templateDAO" class="com.jdbcTemplate.test.TemplateDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean>
編寫測(cè)試類進(jìn)行測(cè)試
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);//注意對(duì)比方法二的不同 Account account = tp.find("Lily"); System.out.println(account.toString()); } }
到此這篇關(guān)于詳解在spring中使用JdbcTemplate操作數(shù)據(jù)庫(kù)的幾種方式的文章就介紹到這了,更多相關(guān)spring JdbcTemplate操作數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
maven?scope?provided和runtime的例子說明
這篇文章主要介紹了maven?scope?provided和runtime的例子說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12idea在用Mybatis時(shí)xml文件sql不提示解決辦法(提示后背景顏色去除)
mybatis的xml文件配置的時(shí)候,有時(shí)候會(huì)沒有提示,這讓我們很頭疼,下面這篇文章主要給大家介紹了關(guān)于idea在用Mybatis時(shí)xml文件sql不提示的解決辦法,提示后背景顏色去除的相關(guān)資料,需要的朋友可以參考下2023-03-03SpringBoot之logback-spring.xml不生效的解決方法
這篇文章主要介紹了SpringBoot之logback-spring.xml不生效的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01對(duì)dbunit進(jìn)行mybatis DAO層Excel單元測(cè)試(必看篇)
下面小編就為大家?guī)硪黄獙?duì)dbunit進(jìn)行mybatis DAO層Excel單元測(cè)試(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Java8新特性lambda表達(dá)式有什么用(用法實(shí)例)
這篇文章主要介紹了Java8新特性lambda表達(dá)式有什么用,著重以實(shí)例講解lambda表達(dá)式,需要的朋友可以參考下2014-06-06解決Spring Boot 正常啟動(dòng)后訪問Controller提示404問題
今天小編再次搭建Spring Boot項(xiàng)目的時(shí)候遇到訪問Controller報(bào)404錯(cuò)誤,之前都很順利。到底怎么回事呢?下面小編給大家?guī)砹私鉀QSpring Boot 正常啟動(dòng)后訪問Controller提示404問題,感興趣的朋友一起看看吧2018-08-08spring boot攔截器的使用場(chǎng)景示例詳解
這篇文章主要給大家介紹了關(guān)于spring boot攔截器的使用場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05