详解在spring中使用JdbcTemplate操作数据库的几种方式
作者:永远喜欢由比滨结衣 发布时间:2024-01-29 09:29:50
标签:spring,JdbcTemplate,数据库
使用JdbcTemplate的步骤
1、设置spring-jdbc和spring-tx的坐标(也就是导入依赖)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
2、创建数据表和实体类
创建数据表的过程省略
创建实体类Account
package com.jdbcTemplate.bean;
public class Account {
private String name;
private Double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
3、创建数据源、JdbcTemplate对象
4、执行数据库操作
实现3、4步的方法提供以下三种
方法一:代码中直接配置数据源和数据对象
创建JdbcTemplate对象+执行jdbc语句
//创建数据源对象
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/think");
ds.setUsername("root");
ds.setPassword("");
//创建jdbcTemplate对象
JdbcTemplate jt = new JdbcTemplate();
//执行操作(插入操作)
jt.setDataSource(ds);
jt.execute("insert into account(name,money)value('EVA',50000)");
方法二:在resources目录下配置xx.xml文件,对数据源、JdbcTemplate进行注入
配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- //配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/think"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- //配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
使用配置操作数据库
编写test类测试
//二、使用配置操作数据库
//1、获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans5.xml");
//2、获取对象
JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
//、执行操作
// jt.execute("insert into account(name,money)value ('Alice',2000)");
//保存
//jt.update("insert into account(name,money)value (?,?)","Eden",100);
//更新
// jt.update("update account set money=?,name=? where name=?",1000,"Kiroto","Eden");
//删除
//jt.update("delete from account where name =? and money =?","Kiroto",1000);
//查找
List<Account> list = jt.query("select * from account where name =?",new BeanPropertyRowMapper<Account>(Account.class),"Eden");
System.out.println(list.isEmpty()?"没有查找结果":list.get(0));
方法三:使用接口实现
创建template接口和templateDAO接口实现类
接口
package com.jdbcTemplate.test;
import com.jdbcTemplate.bean.Account;
public interface Template {
Account find(String name);
int update(Account account);
int delete(Account account);
int add(Account account);
}
接口实现类
package com.jdbcTemplate.test;
import com.jdbcTemplate.bean.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class TemplateDAO implements Template {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Account find(String name) {//查找
List<Account> list = jdbcTemplate.query("select * from account where name=?",
new BeanPropertyRowMapper<Account>(Account.class),name);
return list.isEmpty()?null:list.get(0);
}
public int update(Account account) {//更新
return jdbcTemplate.update("update account set money=? where name=?",
account.getMoney(),account.getName());
}
public int delete(Account account) {//删除
return jdbcTemplate.update("delete from account where name =?",account.getName());
}
public int add(Account account) {//添加
return jdbcTemplate.update("insert into account(name ,money)value (?,?)",account.getName(),account.getMoney());
}
}
在测试之前,因为多了一个接口实现类,除了数据源和jdbcTemplate之外,应当在xml配置文件中多配置一个TemplateDAO
<!-- 配置账户的持久层-->
<bean id="templateDAO" class="com.jdbcTemplate.test.TemplateDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
编写测试类进行测试
import com.jdbcTemplate.bean.Account;
import com.jdbcTemplate.test.Template;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class mytest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans6.xml");
Template tp = ac.getBean("templateDAO",Template.class);//注意对比方法二的不同
Account account = tp.find("Lily");
System.out.println(account.toString());
}
}
来源:https://blog.csdn.net/L_GRAND_ORDER/article/details/107449883
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 怀旧滤镜实现原理不管是荣耀华为手机还是其他的手机,我们都可以找到相机中的怀旧效果,这是手机中常用的一种滤镜效果。怀旧风格的设计主要是在图像的
- 目录项目引入flask-sqlalchemyORM简介及模型定义表关系类型及编码实现一对多关系(多对一关系)一对一关系多对多关系数据库基本操
- 容器与可迭代对象在正式开始前先补充一些基本概念在 Python 中存在容器 与 可迭代对象容器:用来存储多个元素的数据结构,例如 列表,元组
- TensorFlow版本更新太快 了,所以导致一些以前接口函数不一致,会报错。这里总结了一下自己犯的错,以防以后再碰到,也可以给别人参考。首
- 本文实例讲述了Python HTML解析器BeautifulSoup用法。分享给大家供大家参考,具体如下:BeautifulSoup简介我们
- 添加表字段alter table table1 add transactor varchar(10) not Null;alter tabl
- 前言python中常用的写爬虫的库常有urllib2、requests,对于大多数比较简单的场景或者以学习为目的,可以用这两个库实现。这里有
- 问题:pycharm无法调用pip安装的包原因:pycharm没有设置解析器解决方法:打开pycharm->File->Sett
- 在用jupyter notebook写python代码的过程中会产生很多变量,而关闭后或者restart jupyter kernel后所有
- 先看几个数据。。一大堆文字滴,不管人家是不是故意的,字数还是这样:news.163.cn:14px,39个中文字符 news.sina.co
- 简介 SQL Server 2014提供了众多激动人心的新功能,但其中我想最让人期待的特性之一就要算内存数据库了。去
- 最近在学习MySQL优化方面的知识。本文就数据类型和schema方面的优化进行介绍。1. 选择优化的数据类型MySQL支持的数据类型有很多,
- 指针的操作在Go语言中,指针是一种非常重要的类型,可以用来传递变量的地址而不是变量本身。定义指针在Go语言中,使用*运算符来定义指针。例如,
- 俄罗斯方块是俄罗斯人发明的一款休闲类的小游戏,这款小游戏可以说是很多人童年的主打电子游戏了,本文我们使用 Python 来实现这款小游戏。游
- 其实各大深度学习框架背后的原理都可以理解为拟合一个参数数量特别庞大的函数,所以各框架都能用来拟合任意函数,Pytorch也能。在这篇博客中,
- 在xadmin中是不能像原生admin那样使用formfield_for_manytomany方法来过滤多对多字段进入xadmin源码,找到
- 今天搭了个“发短信”的页面,找朋友测试,没想到一位大侠直接弄了本长篇小说发我手机上……为了我的宝贝手机能继续健康澎湃,给文本区域(texta
- MySQL 内连接、左连接、右连接、外连接、多表查询构建环境:create table t_emp(id int primary key,
- python保存图片时和原图大小一致之前遇到过一次这个问题,当时解决了但是忘了记录,这里再记录一次好了。1. matplotlib系列的整体
- 在SQL SERVER下跟踪sql采用事件探查器,而在mysql下如何跟踪sql呢? 其实方法很简单,开启mysql的日志log功能,通过查