springBoot使用JdbcTemplate代码实例

所属分类: 软件编程 / java 阅读数: 44
收藏 0 赞 0 分享

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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多