Java Spring JdbcTemplate基本使用詳解
JdbcTemplate概述
它是spring框架中提供的一個對象,是對原始繁瑣的Jdbc API對象的簡單封裝。spring框架為我們提供了很多的操作模板類。例如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的RedisTemplate,操作消息隊列的JmsTemplate等等。
JdbcTemplate開發(fā)步驟
①導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
②創(chuàng)建數(shù)據(jù)庫表和實體
③創(chuàng)建JdbcTemplate對象
④執(zhí)行數(shù)據(jù)庫操作
JdbcTemplate快速入門
①導(dǎo)入spring-jdbc和spring-tx坐標(biāo)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
②創(chuàng)建accout表和Accout實體

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 +
'}';
}
}
③創(chuàng)建JdbcTemplate對象
④執(zhí)行數(shù)據(jù)庫操作
//測試JdbcTemplate開發(fā)步驟
public void test1() throws PropertyVetoException {
//創(chuàng)建數(shù)據(jù)源對象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//設(shè)置數(shù)據(jù)源對象 知道數(shù)據(jù)庫在哪
jdbcTemplate.setDataSource(dataSource);
//執(zhí)行操作
int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
System.out.println(row);
}
Spring產(chǎn)生JdbcTemplate對象
我們可以將JdbcTemplate的創(chuàng)建權(quán)交給Spring,將數(shù)據(jù)源DataSource的創(chuàng)建權(quán)也交給Spring,在Spring容器內(nèi)部將數(shù)據(jù)源DataSource注入到JdbcTemplate模版對象中,配置如下:
<!--數(shù)據(jù)源對象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--jdbc模板對象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
從容器中獲得JdbcTemplate進(jìn)行添加操作
//測試Spring產(chǎn)生jdbcTemplate對象
public void test2() throws PropertyVetoException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
jdbcTemplate.update("insert into account values(?,?)", "lisi", 5000);
System.out.println(row);
}
JdbcTemplate的常用操作
修改操作、刪除和查詢?nèi)坎僮?/p>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testQueryCount(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
@Test
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
System.out.println(account);
}
@Test
public void testQueryAll(){
List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
@Test
public void testUpdate(){
jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
}
@Test
public void testDelete(){
jdbcTemplate.update("delete from account where name=?","tom");
}
}
到此這篇關(guān)于Java Spring JdbcTemplate基本使用詳解的文章就介紹到這了,更多相關(guān)Java Spring JdbcTemplate內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2.0集成MQTT消息推送功能實現(xiàn)
這篇文章主要介紹了SpringBoot2.0集成MQTT消息推送功能實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
spring boot下mybatis配置雙數(shù)據(jù)源的實例
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring內(nèi)置任務(wù)調(diào)度如何實現(xiàn)添加、取消與重置詳解
任務(wù)調(diào)度是我們?nèi)粘i_發(fā)中經(jīng)常會碰到的,下面這篇文章主要給大家介紹了關(guān)于Spring內(nèi)置任務(wù)調(diào)度如何實現(xiàn)添加、取消與重置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
解決java-jar報錯:xxx.jar 中沒有主清單屬性的方法
在使用 java -jar xxx.jar 命令運行 Java 應(yīng)用程序時,遇到了以下錯誤:xxx.jar 中沒有主清單屬性,這個錯誤表示 JAR 文件缺少必要的啟動信息,本文將介紹該錯誤的原因以及如何通過修改 pom.xml 文件來解決,需要的朋友可以參考下2024-11-11
Java socket通信模擬QQ實現(xiàn)多人聊天室
Socket在Java實戰(zhàn)網(wǎng)絡(luò)通信編程應(yīng)用中有非常重要的作用,你想要跟別人聯(lián)系都得通過socket占據(jù)端口來實現(xiàn),掌握Socket技術(shù)不僅在聊天應(yīng)用程序中需要用到(比如QQ什么的都都是用socket來寫的),而且對于學(xué)習(xí) Asp.net 也非常有幫助2022-07-07
springmvc處理模型數(shù)據(jù)Map過程解析
這篇文章主要介紹了springmvc處理模型數(shù)據(jù)Map過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案
這篇文章主要介紹了SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

