找婚慶公司去什么網(wǎng)站東莞網(wǎng)絡(luò)公司代理
文章目錄
- Spring JdbcTemplate詳解
- 一、引言
- 二、配置JdbcTemplate
- 1、引入依賴
- 2、配置數(shù)據(jù)庫連接池
- 3、配置JdbcTemplate
- 三、使用JdbcTemplate操作數(shù)據(jù)庫
- 1、添加數(shù)據(jù)
- 2、查詢數(shù)據(jù)
- 查詢某個(gè)值
- 根據(jù)條件查詢返回某個(gè)對(duì)象
- 查詢對(duì)象集合
- 四、總結(jié)
Spring JdbcTemplate詳解
一、引言
在Java開發(fā)中,數(shù)據(jù)庫操作是常見的需求。JDBC
作為Java連接數(shù)據(jù)庫的標(biāo)準(zhǔn)接口,雖然功能強(qiáng)大,但代碼繁瑣且容易出錯(cuò)。Spring
框架通過JdbcTemplate
對(duì)JDBC
進(jìn)行了封裝,簡化了數(shù)據(jù)庫操作,提高了開發(fā)效率。本文將詳細(xì)介紹Spring JdbcTemplate
的使用,包括配置、基本操作和示例代碼。
二、配置JdbcTemplate
1、引入依賴
在Spring
項(xiàng)目中使用JdbcTemplate
,首先需要引入相關(guān)依賴。以下是Maven
依賴配置:
<dependencies><!-- Spring Context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.16</version></dependency><!-- MySQL Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency><!-- Spring JDBC --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.16</version></dependency><!-- Druid Connection Pool --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.9</version></dependency>
</dependencies>
2、配置數(shù)據(jù)庫連接池
使用Druid
作為數(shù)據(jù)庫連接池,配置jdbc.properties
文件:
url=jdbc:mysql://localhost:3306/your_database?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username=root
password=your_password
driver-class-name=com.mysql.cj.jdbc.Driver
在Spring
配置文件中引入jdbc.properties
并配置Druid
數(shù)據(jù)源:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="driverClassName" value="${driver-class-name}"/>
</bean>
3、配置JdbcTemplate
在Spring
配置文件中配置JdbcTemplate
對(duì)象,并注入DataSource
:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/>
</bean>
三、使用JdbcTemplate操作數(shù)據(jù)庫
1、添加數(shù)據(jù)
創(chuàng)建Man
實(shí)體類,并在Dao
接口中定義添加方法:
public interface ManDao {int addEntity(Man man);
}
實(shí)現(xiàn)Dao
接口,并使用JdbcTemplate
添加數(shù)據(jù):
@Repository
public class ManDaoImpl implements ManDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic int addEntity(Man man) {String sql = "insert into t_man(user_name, sex) values(?,?)";return jdbcTemplate.update(sql, man.getUserName(), man.getSex());}
}
2、查詢數(shù)據(jù)
查詢某個(gè)值
在Dao
接口中定義查詢方法,并在實(shí)現(xiàn)類中使用JdbcTemplate
查詢:
public interface ManDao {String getNameByUserId(int id);
}@Override
public String getNameByUserId(int id) {String sql = "select user_name from t_man where uid = ?";return jdbcTemplate.queryForObject(sql, String.class, id);
}
根據(jù)條件查詢返回某個(gè)對(duì)象
在Dao
接口中定義查詢方法,并使用BeanPropertyRowMapper
返回對(duì)象:
public interface ManDao {Man getEntityById(int id);
}@Override
public Man getEntityById(int id) {String sql = "select uid, user_name userName, sex from t_man where uid = ?";return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Man>(Man.class), id);
}
查詢對(duì)象集合
定義查詢所有數(shù)據(jù)的方法:
public interface ManDao {List<Man> findAll();
}@Override
public List<Man> findAll() {String sql = "select uid, user_name userName, sex from t_man";return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Man>(Man.class));
}
四、總結(jié)
Spring JdbcTemplate
提供了一個(gè)簡便的方法來執(zhí)行數(shù)據(jù)庫操作,通過封裝JDBC
,減少了模板化的代碼,提高了開發(fā)效率。本文介紹了JdbcTemplate
的基本配置和使用方法,包括添加、查詢等基本操作。通過實(shí)際代碼示例,可以幫助開發(fā)者快速上手JdbcTemplate
。
版權(quán)聲明:本博客內(nèi)容為原創(chuàng),轉(zhuǎn)載請(qǐng)保留原文鏈接及作者信息。
參考文章:
- Spring JdbcTemplate詳解
- Spring學(xué)習(xí)之JdbcTemplate