Spring JDBC的使用方法詳解
1、為什么使用Spring提供的JDBC的封裝?
因為Spring提供了完整的模板類以及基類可以簡化開發(fā),我們只需寫少量的代碼即可。
2、實例講解
第一步:導入依賴 mysql-connector spring-jdbc spring-tx spring-core spring-beans spring-context 等等
第二步:數據庫的建立,配置xml
第三步:獲取jdbcTemplate對象
可以這樣獲得
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource){
this.jdbcTemplate=new JdbcTemplate(dataSource);
}
<bean id="userDao" class="com.test.jdbc.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
也可以繼承JdbcDaoSupport,使用super.getJdbcTemplate()獲得
第四步:使用jdbcTemplate對象進行增刪改查
增刪改使用update(),這個方法的參數,第一個為sql語句,之后為可變參數,傳入sql語句中的占位符
查詢使用queryForObject()方法的第一個參數為sql中需要的參數,第三個參數為實現RowMapper接口的對象,
用于處理結果集,該方法要求返回單一的對象
query()該方法返回對象集合
public class UserDaoImpl extends JdbcDaoSupport implements IUserDao {
public void save(User user) {
super.getJdbcTemplate().update("insert into user(id,username,password,date,salary) values(null,?,?,?,?)",
user.getUsername(),user.getPassword(),user.getDate(),user.getSalary());
}
public void update(User user) {
super.getJdbcTemplate().update("update user set username=?,password=?,date=?,salary=? where id=?",
user.getUsername(), user.getPassword(), user.getDate(), user.getSalary(), user.getId());
}
public void delete(Integer id) {
super.getJdbcTemplate().update("delete from user where id=?",id);
}
public User findById(Integer id) {
/* List<User> user = super.getJdbcTemplate().query("select * from user where id=?", new Object[]{id},
new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setDate(resultSet.getDate("date"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setSalary(resultSet.getBigDecimal("salary"));
return user;
}
//方法的返回值會直接封裝到集合中
});
return user.size()>0?user.get(0):null;*/
User user = super.getJdbcTemplate().queryForObject("select * from user where id=?", new Object[]{id},
new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setDate(resultSet.getDate("date"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setSalary(resultSet.getBigDecimal("salary"));
return user;
}
});
return user;
}
public List<User> findAll() {
List<User> users = super.getJdbcTemplate().query("select * from user",
new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setDate(resultSet.getDate("date"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setSalary(resultSet.getBigDecimal("salary"));
return user;
}
//方法的返回值會直接封裝到集合中
});
return users;
}
}
3、JdbcTemplate是JDBC核心包的中心類
知識點查詢地址:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring BPP中如何優(yōu)雅的創(chuàng)建動態(tài)代理Bean詳解
這篇文章主要給大家介紹了關于Spring BPP中如何優(yōu)雅的創(chuàng)建動態(tài)代理Bean的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03
windows java.exe內存暴漲解決、idea跑java\ tomcat內存無限增長
這篇文章主要介紹了windows java.exe內存暴漲解決、idea跑 java\ tomcat內存無限增長,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
spring security登錄成功后通過Principal獲取名返回空問題
這篇文章主要介紹了spring security登錄成功后通過Principal獲取名返回空問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java countDownLatch如何實現多線程任務阻塞等待
這篇文章主要介紹了Java countDownLatch如何實現多線程任務阻塞等待,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
SpringBoot動態(tài)修改yml配置文件的方法詳解
這篇文章主要為大家詳細介紹了SpringBoot動態(tài)修改yml配置文件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

