详解在spring中使用JdbcTemplate操作数据库的几种方式

网友投稿 217 2023-03-31

详解在spring中使用JdbcTemplate操作数据库的几种方式

使用JdbcTemplate的步骤

1、设置spring-jdbc和spring-tx的坐标(也就是导入依赖)

org.springframework

spring-jdbc

5.2.7.RELEASE

org.springframework

spring-tx

5.2.7.RELEASE

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文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

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

使用配置操作数据库

编写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 list = jt.query("select * from account where name =?",new BeanPropertyRowMapper(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 list = jdbcTemplate.query("select * from account where name=?",

new BeanPropertyRowMapper(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

编写测试类进行测试

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

}

}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

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

使用配置操作数据库

编写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 list = jt.query("select * from account where name =?",new BeanPropertyRowMapper(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 list = jdbcTemplate.query("select * from account where name=?",

new BeanPropertyRowMapper(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

编写测试类进行测试

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

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:银行卡三要素(3分钟之前已更新)
下一篇:极速api(4分钟之前已更新)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~