Spring JDBCTemplate原理及使用實(shí)例
一:JDBCTemplate簡介
Spring為各種持久化技術(shù)提供了簡單操作的模板和回調(diào)API:
| ORM持久化技術(shù) | 模板類 |
| 原生JDBC | org.springframework.jdbc.core.JdbcTemplate |
| Hibernate5.0 | org.springframework.orm.hibernate5.HibernateTemplate |
| IBatis(MyBatis) | org.springframework.orm.ibatis.SqlMapClientTemplate |
| JPA | org.springfrmaework.orm.jpa.JpaTemplate |
其中,對于原生的JDBC編程,Spring提供了JDBCTemplate,對jdbc操作進(jìn)行了一系列封裝,使得jdbc編程更加簡單。
二:引入jar包或添加依賴

數(shù)據(jù)庫驅(qū)動(dòng)包則按需引入。
三:創(chuàng)建數(shù)據(jù)庫連接配置文件
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc\:......
四:Spring托管
在Spring配置文件中,注入dataSource,從配置文件中獲取內(nèi)容進(jìn)行bean的初始化。
<context:property-placeholder location="classpath:db.properties"/> //指定數(shù)據(jù)庫連接配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> //托管dataSource bean
<property name="user" value="${jdbc.user}"></property> //獲取配置文件內(nèi)容對dataSource進(jìn)行初始化
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> //托管jdbcTemplate,引用dataSource
<property name="dataSource" ref="dataSource"></property>
</bean>
五:在代碼中使用
1:通過Spring配置文件啟動(dòng)IOC容器
//啟動(dòng)IoC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2:在IOC容器獲取jdbcTemplate實(shí)例
//獲取IoC容器中JdbcTemplate實(shí)例
JdbcTemplate jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");
3:使用jdbcTemplate調(diào)用方法執(zhí)行數(shù)據(jù)庫操作
JdbcTemplate主要提供以下五大方法:
execute方法:可以用于執(zhí)行任何SQL語句。
update方法及batchUpdate方法:update方法用于執(zhí)行新增、修改、刪除等語句;batchUpdate方法用于執(zhí)行批處理相關(guān)語句;
query方法及queryForXXX方法:用于執(zhí)行查詢相關(guān)語句;
call方法:用于執(zhí)行存儲(chǔ)過程、函數(shù)相關(guān)語句。
3.1)通過 update 進(jìn)行插入
String sql="insert into user (name,deptid) values (?,?)";
int count= jdbcTemplate.update(sql, new Object[]{"caoyc",3});
3.2)通過 update 進(jìn)行修改
String sql="update user set name=?,deptid=? where id=?";
jdbcTemplate.update(sql,new Object[]{"zhh",5,51});
3.3)通過 update 進(jìn)行刪除
String sql="delete from user where id=?";
jdbcTemplate.update(sql,51);
3.4)批量插入、修改、刪除
String sql="insert into user (name,deptid) values (?,?)";
List<Object[]> batchArgs=new ArrayList<Object[]>();
batchArgs.add(new Object[]{"caoyc",6});
batchArgs.add(new Object[]{"zhh",8});
batchArgs.add(new Object[]{"cjx",8});
jdbcTemplate.batchUpdate(sql, batchArgs);
六:在DAO層使用
數(shù)據(jù)庫操作一般結(jié)合ORM進(jìn)行使用。
1:首先定義實(shí)體類
package com.proc;
public class User {
private Integer id;
private String name;
private Integer deptid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDeptid() {
return deptid;
}
public void setDeptid(Integer deptid) {
this.deptid = deptid;
}
public String toString() {
return "User [id=" + id + ", name=" + name + ", deptid=" + deptid + "]";
}
}
2:然后定義該實(shí)體類的DAO類
package com.proc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public User get(int id){
String sql="select id,name,deptid from user where id=?";
RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
return jdbcTemplate.queryForObject(sql, rowMapper,id);
}
}
在DAO類中,定義對應(yīng)實(shí)體實(shí)例的增刪查改操作方法,在方法體中,使用jdbcTemplate。
3:在service層,通過dao實(shí)例調(diào)用方法,執(zhí)行數(shù)據(jù)操作。
UserDao userDao=(UserDao) ctx.getBean("userDao");
System.out.println(userDao.get(53));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- Spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫
- SpringBoot JdbcTemplate批量操作的示例代碼
- springboot使用JdbcTemplate完成對數(shù)據(jù)庫的增刪改查功能
- 基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(詳解)
- SpringBoot用JdbcTemplates訪問Mysql實(shí)例代碼
- 詳解spring boot中使用JdbcTemplate
- Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫實(shí)例
相關(guān)文章
SpringBoot 使用hibernate validator校驗(yàn)
這篇文章主要介紹了SpringBoot 使用hibernate validator校驗(yàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Java Web使用POI導(dǎo)出Excel的方法詳解
這篇文章主要介紹了Java Web使用POI導(dǎo)出Excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java Web使用POI導(dǎo)出Excel的具體操作步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
spring學(xué)習(xí)教程之@ModelAttribute注解運(yùn)用詳解
這篇文章主要給大家介紹了關(guān)于spring學(xué)習(xí)教程之@ModelAttribute注釋運(yùn)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟
這篇文章主要給大家介紹了在IDEA中創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)步驟,文中有詳細(xì)的圖文介紹和代碼示例,對大家的學(xué)習(xí)和工作有一定的幫助,需要的朋友可以參考下2023-09-09

