当前位置: 首页 > news >正文

javaee spring jdbcTemplate的使用

依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>testspring02</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>testspring02 Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 导入spring的核心jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!-- 配置的 spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.18.RELEASE</version></dependency></dependencies><build><finalName>testspring02</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

测试类

package com.test.springJdbc;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;import java.beans.PropertyVetoException;/*** @description:* @projectName:testspring2* @see:com.test.springJdbc* @author:123* @createTime:2023/8/28 19:33*/
public class TestJdbcTemplate {@Testpublic void test(){//1.创建一个连接池ComboPooledDataSource pooledDataSource=new ComboPooledDataSource();try {pooledDataSource.setDriverClass("com.mysql.jdbc.Driver");pooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/itstar");pooledDataSource.setUser("itstar");pooledDataSource.setPassword("yyy123456");//2.创建JdbcTemplate对象JdbcTemplate jdbcTemplate=new JdbcTemplate(pooledDataSource);//3.执行sqlString sql="insert into user values(null,?,?)";jdbcTemplate.update(sql,"daimenglaoshi","123");} catch (PropertyVetoException e) {e.printStackTrace();}}
}

用spring管理jdbc相关类

spring配置文件

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 开启注解--><context:component-scan base-package="com.test" /><!-- 导入db.properties --><context:property-placeholder location="db.properties" /><!-- 创建数据源--><bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${jdbcUrl}" /><property name="user" value="${user}" /><property name="password" value="${password}" /></bean><!--创建JdbcTemplate对象 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="comboPooledDataSource" /></bean></beans>

测试类

package com.test.dao.impl;import com.test.dao.IUsersDao;
import com.test.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;/*** @description:* @projectName:testspring2* @see:com.test.dao.impl* @author:杨钧博* @createTime:2023/8/28 21:56*/
@Component
public class UsersDao implements IUsersDao {//注入jdbcTemplate对象@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void add(Users user) {String sql = "insert into users values(null,?,?)";jdbcTemplate.update(sql, user.getName(), user.getPassword());}
}
package com.test.springJdbc;import com.test.dao.IUsersDao;
import com.test.pojo.Users;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class TestUsersDao {@Testpublic void test(){ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");IUsersDao usersDao=  applicationContext.getBean("usersDao",IUsersDao.class);Users user=new Users("xiaowanglaoshi","123");usersDao.add(user);}}

相关文章:

javaee spring jdbcTemplate的使用

依赖 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 htt…...

qt day

#include "widget.h" #include "ui_widget.h" void Widget::my_slot() {} Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);this->setWindowIcon(QIcon(":/wodepeizhenshi.png"));//設置窗口的…...

Configurable Weather Parameters

1. Weather Parameters 2. Classification ClearNoon, CloudyNoon, WetNoon, WetCloudyNoon, SoftRainNoon, MidRainyNoon, HardRainNoon, ClearSunset, CloudySunset, WetSunset, WetCloudySunset, SoftRainSunset, MidRainSunset, HardRainSunset to do list: 可以关注一下…...

MySQL 8 数据清洗总结

MySQL 8 数据清洗三要素&#xff1a; 库表拷贝和数据备份数据清洗SQL数据清洗必杀技-存储过程 前提&#xff1a;数据库关联库表初始化和基础数据初始化&#xff1a; -- usc.t_project definitionCREATE TABLE t_project (id varchar(64) NOT NULL COMMENT 主键,tid varchar(…...

设计模式第九讲:常见重构技巧 - 去除不必要的!=

设计模式第九讲&#xff1a;常见重构技巧 - 去除不必要的! 项目中会存在大量判空代码&#xff0c;多么丑陋繁冗&#xff01;如何避免这种情况&#xff1f;我们是否滥用了判空呢&#xff1f;本文是设计模式第九讲&#xff0c;讲解常见重构技巧&#xff1a;去除不必要的! 文章目录…...

自动化安装系统问题记录

Cobbler 版本更新 https://github.com/cobbler/cobbler/releases Centos7/8 Cobbler 问题&#xff1a; 部署cobbler时&#xff0c;使用cobbler get-loaders从网络下载引导加载程序时提示命令未找到 解决&#xff1a; yum -y install syslinux Rockylinux9.2 Cobbler3.…...

centos7 docker安装记录

以下所有命令都在root用户下进行&#xff0c;若为普通用户&#xff0c;需要在所有命令前加上 sudo。 1、更新yum包 将yum包更新到最新 yum update2、安装需要的软件包 yum-util 提供yum-config-manager功能&#xff0c;另外两个是devicemapper驱动依赖的&#xff1b; yum …...

全网实时热点热榜事件API

全网热榜API 一、全网热榜二、使用步骤1、接口2、请求参数 三、 案例和demo 一、全网热榜 1个免费的帮助你获取全网热点事件的接口API 二、使用步骤 1、接口 重要提示:建议使用https协议,当https协议无法使用时再尝试使用http协议 请求方式: GET https://luckycola.com.cn…...

淘宝API接口:提高电商运营效率与用户体验的利器(淘宝API接口使用指南)

淘宝API接口&#xff1a;提高电商运营效率与用户体验的利器 随着电商行业的快速发展&#xff0c;淘宝作为国内最大的电商平台之一&#xff0c;不断探索和创新&#xff0c;以满足不断变化的用户需求和商家需求。其中&#xff0c;淘宝API接口便是其创新的一个重要方面。本文将深…...

智己 LS6 用实力和你卷,最强 800v ?

2023 成都车展期间&#xff0c;智己 LS6 正式公布预售价格&#xff0c;新车预售价为 23-30 万元。新车会在 10 月份进行上市&#xff0c;11 月正式交付。 此前我们对智己 LS6 做过非常详细的静态体验&#xff0c;感兴趣的可点击此链接了解。 造型方面&#xff0c;新车前脸相比…...

深入探索C语言自定义类型:打造你的编程世界

一、什么是自定义类型 C语言提供了丰富的内置类型&#xff0c;常见的有int, char, float, double, 以及各种指针。 除此之外&#xff0c;我们还能自己创建一些类型&#xff0c;这些类型称为自定义类型&#xff0c;如数组&#xff0c;结构体&#xff0c;枚举类型和联合体类型。 …...

Opencv基于文字检测去图片水印

做了一个简单的去水印功能&#xff0c;基于文字检测去图片水印。效果如下&#xff1a; 插件功能代码参考如下&#xff1a; using namespace cv::dnn; TextDetectionModel_DB *textDetector0; void getTextDetector() {if(textDetector)return;String modelPath "text_de…...

jdbc235

概念&#xff1a;java database connectivity java数据库连接 java语言操作数据库 定义了一套操作所有关系型数据库的规则&#xff08;接口&#xff09; 本质&#xff1a;其实是官方公司定义了一套操作所有关系型数据库的规则&#xff0c;即接口。各个数据库厂商去实现这套接…...

库仑定律和场强

1、库伦定律 两个电荷相互作用的力。 力是矢量&#xff0c;有大小和方向。 1.1、力的大小 1.2、力的方向 在两个电荷的连线上&#xff0c;同种电荷相互排斥&#xff0c;异种电荷相互吸引。 真空&#xff0c;不是必要条件&#xff0c;修改公式中介电常数的值仍然满足库伦定律。…...

Android安卓实战项目(13)---记账APP详细记录每天的收入和支出并且分类统计【生活助手类APP】强烈推荐自己也在用!!!(源码在文末)

Android安卓实战项目&#xff08;13&#xff09;—记账APP详细记录每天的收入和支出并且分类统计【生活助手类APP】强烈推荐自己也在用&#xff01;&#xff01;&#xff01;&#xff08;源码在文末&#x1f415;&#x1f415;&#x1f415;&#xff09; 一.项目运行介绍 B站…...

嵌入式开发之syslog和rsyslog构建日志记录

1.syslogd作客户端 BusyBox v1.20.2 (2022-04-06 16:19:14 CST) multi-call binary.Usage: syslogd [OPTIONS]System logging utility-n Run in foreground-O FILE Log to FILE (default:/var/log/messages)-l N Log only messages more urge…...

Jaeger的经典BUG原创

前端&#xff0c;笔者在使用Jaeger进行Trace监控的时候&#xff0c;当数据量增大到一定数量级时&#xff0c;出现了一次CPU暴增导致节点服务器挂了的经典案例&#xff0c;这里对案例进行一个简单的抽象&#xff0c;供大家参考&#xff1a; 首先通过pprof对耗时的函数进行定位&…...

四款简洁好看 自适应的APP下载单页源码

分享四款简洁好看 自适应的APP下载单页源码&#xff0c;采用了底部自动获取ICP备案号&#xff0c;还有蓝奏云文件直链解析。不光可以做APP下载引导页&#xff0c;也可以随便改下按钮做网站引导页&#xff0c;自由发挥即可&#xff01; 蓝奏云直链解析的好处&#xff1a;APP放在…...

【服务器】交换机带外管理和带内管理

一、交换机的带外管理是什么&#xff1f; 在带外管理模式中&#xff0c;网络的管理控制信息与用户网络的承载业务信息在不同的逻辑信道传送。 带外管理最大的优势在于&#xff0c;当网络出现故障中断时数据传输和管理都可以正常进行——不同的物理通道传送管理控制信息和数据…...

Kotlin的内置函数 apply、let、run、with、also

let 1.let函数返回类型&#xff0c;是根据匿名函数的最后一行变化而变化 2.let函数中的匿名函数里面持有的是it 集合自身 fun main() {var num1 1var num2 1var result:Intresult num1 num2var str result?.let {//传入本身&#xff0c;it指代本身即result,result不为空…...

渲染学进阶内容——模型

最近在写模组的时候发现渲染器里面离不开模型的定义,在渲染的第二篇文章中简单的讲解了一下关于模型部分的内容,其实不管是方块还是方块实体,都离不开模型的内容 🧱 一、CubeListBuilder 功能解析 CubeListBuilder 是 Minecraft Java 版模型系统的核心构建器,用于动态创…...

vue3+vite项目中使用.env文件环境变量方法

vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量&#xff0c;这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

Go 语言并发编程基础:无缓冲与有缓冲通道

在上一章节中&#xff0c;我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道&#xff0c;它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好&#xff0…...

在 Spring Boot 项目里,MYSQL中json类型字段使用

前言&#xff1a; 因为程序特殊需求导致&#xff0c;需要mysql数据库存储json类型数据&#xff0c;因此记录一下使用流程 1.java实体中新增字段 private List<User> users 2.增加mybatis-plus注解 TableField(typeHandler FastjsonTypeHandler.class) private Lis…...

HybridVLA——让单一LLM同时具备扩散和自回归动作预测能力:训练时既扩散也回归,但推理时则扩散

前言 如上一篇文章《dexcap升级版之DexWild》中的前言部分所说&#xff0c;在叠衣服的过程中&#xff0c;我会带着团队对比各种模型、方法、策略&#xff0c;毕竟针对各个场景始终寻找更优的解决方案&#xff0c;是我个人和我司「七月在线」的职责之一 且个人认为&#xff0c…...

渗透实战PortSwigger Labs指南:自定义标签XSS和SVG XSS利用

阻止除自定义标签之外的所有标签 先输入一些标签测试&#xff0c;说是全部标签都被禁了 除了自定义的 自定义<my-tag onmouseoveralert(xss)> <my-tag idx onfocusalert(document.cookie) tabindex1> onfocus 当元素获得焦点时&#xff08;如通过点击或键盘导航&…...

ubuntu中安装conda的后遗症

缘由: 在编译rk3588的sdk时&#xff0c;遇到编译buildroot失败&#xff0c;提示如下&#xff1a; 提示缺失expect&#xff0c;但是实测相关工具是在的&#xff0c;如下显示&#xff1a; 然后查找借助各个ai工具&#xff0c;重新安装相关的工具&#xff0c;依然无解。 解决&am…...

Python第七周作业

Python第七周作业 文章目录 Python第七周作业 1.使用open以只读模式打开文件data.txt&#xff0c;并逐行打印内容 2.使用pathlib模块获取当前脚本的绝对路径&#xff0c;并创建logs目录&#xff08;若不存在&#xff09; 3.递归遍历目录data&#xff0c;输出所有.csv文件的路径…...