springBoot使用JdbcTemplate代碼實例
更新時間:2019年09月12日 10:28:01 作者:火鴉哈士奇
這篇文章主要介紹了springBoot使用JdbcTemplate代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
springBoot使用JdbcTemplate
如果是通過spring自動注入的jdbcTemplate,配好application.properties在其他類中就能在其他類中直接使用。
如果通過new JdbcTemplate()出來的就需要自己配置DataSource。
自動注入如下
application.properties文件
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSourceC3P0Adapter
UserDao
package com.example.demo.dao; import com.example.demo.pojo.UserInfo; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.sql.*; import java.util.ArrayList; import java.util.List; @Repository public class UserDao { @Resource private JdbcTemplate jdbcTemplate; public UserInfo createUser(UserInfo u) { String sql = "insert into user(name,address) values(?,?)"; KeyHolder holder=new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps=conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, u.getName()); ps.setString(2, u.getAddress()); return ps; } }, holder); int insertId=holder.getKey().intValue(); u.setId(insertId); return u; } public void createUserList() { String sql="insert into user (name,address) values (?,?)"; List<Object[]> batchArgs=new ArrayList<Object[]>(); batchArgs.add(new Object[]{"caoyc","北京"}); batchArgs.add(new Object[]{"zhh","重慶"}); batchArgs.add(new Object[]{"cjx","天津"}); jdbcTemplate.batchUpdate(sql, batchArgs); } public void deleteUser(int id) { String sql="delete from user where id=?"; jdbcTemplate.update(sql, new Object[] {id},new int[] {java.sql.Types.INTEGER}); } public void updateUser(UserInfo u) { String sql="update user set name=? where id=?"; jdbcTemplate.update(sql, new Object[] {u.getName(),u.getId()}); } public List<UserInfo> queryUser(int id) { String sql="select * from user where id=?"; // RowMapper<UserInfo> rowMapper = new BeanPropertyRowMapper<>(UserInfo.class); return jdbcTemplate.query(sql,new Object[] {id},new UserRowMapper()); } class UserRowMapper implements RowMapper<UserInfo> { public UserInfo mapRow(ResultSet res, int arg1) throws SQLException { UserInfo u=new UserInfo(); u.setId(res.getInt("id")); u.setName(res.getString("name")); u.setAddress(res.getString("address")); return u; } } }
手動配置如下
DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/postgres"); dataSource.setUsername("postgres"); dataSource.setPassword("332578"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
- SpringBoot2使用JTA組件實現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用)
- SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
- SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
- SpringBoot jdbctemplate使用方法解析
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- springboot使用JdbcTemplate完成對數(shù)據(jù)庫的增刪改查功能
- 如何在SpringBoot項目中使用Oracle11g數(shù)據(jù)庫
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
相關(guān)文章
IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決
原來win10電腦上安裝的是jdk8的版本,因某些原因,現(xiàn)在想換成jdk7的版本,修改環(huán)境變量后,在cmd中執(zhí)行 [java -version]命令,顯示的是7的版本,遇到這樣的問題如何解決呢?下面小編給大家分享IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決方案,一起看看吧2023-09-09springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用
WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下2022-10-10