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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/61587.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Leetcode328 奇偶链表

思路&#xff1a;分别处理奇偶&#xff0c;保存奇偶的第一个和最后一个节点&#xff0c;注意最后链接的时候需要把偶数的next去掉再拼接不然就成环了 class Solution:def oddEvenList(self, head: ListNode) -> ListNode:if not head or not head.next or not head.next.ne…

使用openssl rand随机生成MAC地址的方法

介绍 当我们使用虚拟网卡的时候&#xff0c;有时候需要为虚拟网卡配置随机的MAC地址。我们知道&#xff0c;网卡的MAC地址实际上是一个6字节的整型数&#xff0c;通常表现为用英文冒号&#xff08;:&#xff09;隔开的十六进制字符串&#xff08;全部大写或者全部小写&#xf…

设计模式--模板方法模式(Template Method Pattern)

一、什么是模板方法模式&#xff08;Template Method Pattern&#xff09; 模板方法模式&#xff08;Template Method Pattern&#xff09;是一种行为型设计模式&#xff0c;它定义了一个算法的骨架&#xff0c;将一些步骤的实现延迟到子类中。模板方法模式允许在不改变算法的…

OpenCV c++ 使用imshow显示灰色窗口

OpenCV使用imshow显示灰色窗口 原因是使用了system(‘pause’);函数&#xff0c;只需要将该函数去掉&#xff0c;使用opencv中的对应函数 waitKey(0) 即可实现同样效果。 system(“pause”); 改为&#xff1a; cv::waitKey(0); 显示效果&#xff1a;

Decoupling Knowledge from Memorization: Retrieval-augmented Prompt Learning

本文是LLM系列的文章&#xff0c;针对《Decoupling Knowledge from Memorization: Retrieval 知识与记忆的解耦&#xff1a;检索增强的提示学习 摘要1 引言2 提示学习的前言3 RETROPROMPT&#xff1a;检索增强的提示学习4 实验5 相关实验6 结论与未来工作 摘要 提示学习方法在…

Unity贝塞尔曲线的落地应用-驱动飞行特效

前言 本文教你怎么用贝塞尔曲线驱动一个飞行特效 中间点的准备 开放一些可以给策划配置的变量 startPos flyEffect.transform.position; var right (GetAimPoistion(targetActor) - flyEffect.transform.position).x > 0?1:-1; midPos startPos new Vector3(righ…

适配ADRC自抗扰控制算法的MFP450-ADRC 套件焕新而来

关注 FMT 开源自驾仪的开发者可能知道&#xff0c;早在 2018 年 7 月 FMT开源自驾仪的早期版本就已经实现了 ADRC 算法。 经过几年的发展&#xff0c;FMT 在自抗扰控制算法的适配上做了进一步的优化&#xff0c;为了方便科研工作者和开发者快速上手&#xff0c;我们针对搭载 F…

并发编程的故事——共享模型之内存

共享模型之内存 文章目录 共享模型之内存一、JVM内存抽象模型二、可见性三、指令重排序 一、JVM内存抽象模型 主要就是把cpu下面的缓存、内存、磁盘等抽象成主存和工作内存 体现在 可见性 原子性 有序性 二、可见性 出现的问题 t线程如果频繁读取一个静态变量&#xff0c;那…

解决Spring Data JPA中的NullPointerException问题

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

苹果为 Vision Pro 头显申请游戏手柄专利

苹果Vision Pro 推出后&#xff0c;美国专利局公布了两项苹果公司申请的游戏手柄专利&#xff0c;其中一项的专利图如下图所示。据 PatentlyApple 报道&#xff0c;虽然申请专利并不能保证苹果公司会推出游戏手柄&#xff0c;但是苹果公司同时也为游戏手柄申请了商标&#xff0…

性能优化维度

CPU 首先检查 cpu&#xff0c;cpu 使用率要提升而不是降低。其次CPU 空闲并不一定是没事做&#xff0c;也有可能是锁或者外部资源瓶颈。常用top、vmstat命令查看信息。 vmstat 命令: top: 命令 IO iostat 命令&#xff1a; Memory free 命令&#xff1a; 温馨提示&#xff1a…

postgresql-窗口函数

postgresql-窗口函数 简介窗口函数的定义分区选项&#xff08;PARTITION BY&#xff09;排序选项&#xff08;ORDER BY&#xff09;窗口选项&#xff08;frame_clause&#xff09; 聚合窗口函数排名窗口函数演示了 CUME_DIST 和 NTILE 函数 取值窗口函数 简介 常见的聚合函数&…

因果推断(六)基于微软框架dowhy的因果推断

因果推断&#xff08;六&#xff09;基于微软框架dowhy的因果推断 DoWhy 基于因果推断的两大框架构建&#xff1a;「图模型」与「潜在结果模型」。具体来说&#xff0c;其使用基于图的准则与 do-积分来对假设进行建模并识别出非参数化的因果效应&#xff1b;而在估计阶段则主要…

雅思写作 三小时浓缩学习顾家北 笔记总结(二)

目录 饥饿网一百句翻译 Using government funds for pollution cleanup work can create a comfortable environment. "Allocating government funds to pollution cleanup work can contribute to the creation of a comfortable environment." Some advertise…

ChatGPT的局限性及商业化应用限制讨论

首先&#xff0c;ChatGPT仅使用公开可用的信息&#xff0c;这是其第一个局限。如果基础信息缺失、过时、模糊或过于泛化&#xff0c;AI生成的内容就将不会准确。 只有在使用企业内部专有信息和知识创建特定的GPT时&#xff0c;才会出现真正的商业化解决方案。但对企业而言&…

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

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

【MySQL】3、MySQL的索引、事务、存储引擎

create table class (id int not null,name char(10),score decimal(5,2)); insert into class values (1,zhangsan,80.5); update class set namewangwu,passwd123 where id2; select * from class where id2; drop 索引的概念 是一种帮助系统&#xff0c;能够更快速的查询信…

es6·await/async案例笔记

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>await/async案例笔记</title> </head> …

【2023研电赛】安谋科技企业命题三等奖作品: 短临天气预报AI云图分析系统

本文为2023年第十八届中国研究生电子设计竞赛安谋科技企业命题三等奖分享&#xff0c;参加极术社区的【有奖活动】分享2023研电赛作品扩大影响力&#xff0c;更有丰富电子礼品等你来领&#xff01;&#xff0c;分享2023研电赛作品扩大影响力&#xff0c;更有丰富电子礼品等你来…

python爬虫14:总结

python爬虫14&#xff1a;总结 前言 ​ python实现网络爬虫非常简单&#xff0c;只需要掌握一定的基础知识和一定的库使用技巧即可。本系列目标旨在梳理相关知识点&#xff0c;方便以后复习。 申明 ​ 本系列所涉及的代码仅用于个人研究与讨论&#xff0c;并不会对网站产生不好…