spring jdbctemplate的用法小結(jié)
概述
原始繁瑣的JDBC API對象的封裝;操作模板如下:JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫RedisTemplate,消息隊列JmsTemplate
JDBCTemplate開發(fā)步驟
1)導(dǎo)入spring-jdbc和spring-tx坐標
2)創(chuàng)建數(shù)據(jù)庫和實體
3)創(chuàng)建jdbcTemplate對象
4)執(zhí)行數(shù)據(jù)庫操作
介紹
Spring JDBCTemplate對jdbc進行了簡單的封裝,提供了一個JDBCTemplate對象簡化JDBC的開發(fā)
使用
1、導(dǎo)入jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency>
2、創(chuàng)建JdbcTemplate對象
org.springframework.jdbc.core.JdbcTemplate類有三個構(gòu)造方法
public JdbcTemplate() {
}
public JdbcTemplate(DataSource dataSource) {
setDataSource(dataSource);
afterPropertiesSet();
}
public JdbcTemplate(DataSource dataSource, boolean lazyInit) {
setDataSource(dataSource);
setLazyInit(lazyInit);
afterPropertiesSet();
}- 一般直接使用第二個構(gòu)造方法
- 如果使用默認構(gòu)造方法,可以調(diào)用setDataSource傳入DataSource對象賦值
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
3、關(guān)于ds
(1)spring內(nèi)置數(shù)據(jù)源
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--加載屬性文件-->
<context:property-placeholder location="classpath:properties/jdbc.properties"/>(2)第三方數(shù)據(jù)庫連接池
<context:property-placeholder location="classpath:props/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>db.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mysite?useSSL=false&serverTimezone=UTC&characterEncoding=utf8 jdbc.username=root jdbc.password=3145tj
4、API
<!--增刪改--> int update(String sql) int update(String sql,Object... args) int update(String sql,Object[] args) <!--query--> public <T> List<T> query(String sql, RowMapper<T> rowMapper) queryForMap queryForList queryForObject
到此這篇關(guān)于spring jdbctemplate的用法小結(jié)的文章就介紹到這了,更多相關(guān)spring jdbctemplate使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HttpMessageConverter報文信息轉(zhuǎn)換器的深入講解
在Spring中內(nèi)置了大量的HttpMessageConverter,通過請求頭信息中的MIME類型,選擇相應(yīng)的HttpMessageConverter,這篇文章主要給大家介紹了關(guān)于HttpMessageConverter報文信息轉(zhuǎn)換器的相關(guān)資料,需要的朋友可以參考下2022-01-01
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
分享我的第一次java Selenium自動化測試框架開發(fā)過程
這篇文章主要介紹了分享我的第一次java Selenium自動化測試框架開發(fā)過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
springboot+springsecurity如何實現(xiàn)動態(tài)url細粒度權(quán)限認證
這篇文章主要介紹了springboot+springsecurity如何實現(xiàn)動態(tài)url細粒度權(quán)限認證的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

