Spring中使用JdbcTemplate操作数据库(增删改查以及批量操作)附带源码解释【超详细

本文介绍了如何在Spring中使用JdbcTemplate进行数据库的增删改查操作,包括配置数据库连接池、配置JdbcTemplate、注入DataSource,以及详细展示了使用JdbcTemplate进行单条和批量操作的代码示例。
摘要由CSDN通过智能技术生成

1.0-SNAPSHOT

org.apache.maven.plugins

maven-compiler-plugin

7

7

jar

org.springframework

spring-context

5.2.6.RELEASE

org.springframework

spring-jdbc

5.2.7.RELEASE

org.springframework

spring-tx

5.2.7.RELEASE

mysql

mysql-connector-java

5.1.46

com.alibaba

druid

1.1.10

org.aspectj

aspectjweaver

1.8.9

junit

junit

4.12

compile

maven项目的代码结构:

在这里插入图片描述

配置xml配置文件


在 spring 配置文件配置数据库连接池,配置 JdbcTemplate 对象,注入 DataSource。配置时用的是引用外部配置文件,所以还需要引入外部的属性文件,同时创建对象时是基于注解的所以还要开启组件扫描。

bean1.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=“http://www.springframework.org/schema/p”

xmlns:util=“http://www.springframework.org/schema/util”

xmlns:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package=“com.Keafmd”></context:component-scan>

<context:property-placeholder location=“classpath:jdbc.properties”/>

jdbc.properties:

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/user_db?characterEncoding=utf8&useUnicode=true&useSSL=false

jdbc.username=root

jdbc.password=root

JdbcTemplate 操作数据库

============================================================================

相关的操作及注释都在代码中,请看代码。

  • 21
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值