Spring中如何使用JdbcTemplate中update()更新数据呢?
下文笔者讲述Spring中使用JdbcTemplate中update方法更新数据的方法及示例分享,如下所示
applicationContext.xml中
update()方法简介
update()方法 可完成插入、更新和删除数据的操作
JdbcTemplate类中常用update()方法
方法 | 备注 |
int update(String sql) | 直接传入SQL语句,并返回受影响的行数 |
int update(PreparedStatementCreator psc) | 执行从PreparedStatementCreator返回的语句,然后返回受影响的行数 |
int update(String sql,PreparedStatementSetter pss) | 通过PreparedStatementSetter设置SQL语句中的参数,并返回受影响的行数 |
int update(String sql,Object...args) | 使用Object...设置SQL语句中的参数,参数不能为NULL,并返回受影响的行数 |
实体类的定义
package com.java265.jdbc; public class Account { private Integer id; private String username; private Double balance; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } @Override public String toString() { return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]"; } }
Dao接口定义
package com.java265.jdbc; public interface AccountDao { //添加 public int addAccount(Account account); //更新 public int updateAccount(Account account); //删除 public int deleteAccount(int id); }
接口实现类
package com.java265.jdbc; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDaoImpl implements AccountDao { //声明JdbcTemplate属性及其setter方法 /* * 想要使用JdbcTemplcat类的方法,需要先声明再使用 */ private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } //添加账户 @Override //account负责接收测试类set传过来的值 public int addAccount(Account account) { //1、定义SQL insert into 表名(字段名1,字段名2...) values(值1,值2...) String sql="insert into user01(username,balance) values(?,?)"; //2、定义数组来存储SQL语句中的参数 Object[] obj= new Object[] { //get获取测试类Test,set传过来的值 account.getUsername(), account.getBalance(), }; //3、执行添加操作,返回的是受SQL语句影响的记录条数 /* * JdbcTemplate类中的所有方法调用后,都会返回受影响的行数, * 所以这里定义了一个整型变量num来作为返回值 */ int num=this.jdbcTemplate.update(sql,obj); return num; } //更新账户 @Override public int updateAccount(Account account) { //1、定义SQL /** * 定义SQL语句时,数组中参数与定义SQL语句参数顺序必须一致 * 如:"update user01 set username=?,balance=? where id=?" * 则:Object[] params = new Object[] {account.getUsername(),account.getBalance(),account.getId()}; */ String sql = "update user01 set username=?,balance=? where id=?"; //2、定义数组来存储SQL语句中的参数 Object[] params = new Object[] { account.getUsername(), account.getBalance(), account.getId() }; /** * JdbcTemplate类中的所有方法调用后,都会返回受影响的行数, * 所以这里定义了一个整型变量num来作为返回值 * * int num :定义num为整型变量 * * this.jdbcTemplate.update(sql,params): * 使用JdbcTemplate类的update()方法执行更新操作,第一个值为sql语句,第二个值为参数,且多个参数值按sql语句对应顺序中间逗号隔开 * * 3、调用JdbcTemplate中的update()方法 * update()写法一: * int num=this.jdbcTemplate.update("update user01 set username=?,balance=? where id=?", * account.getUsername(), account.getBalance(), account.getId()); * */ //3、调用JdbcTemplate中的update()方法 //update()写法二: int num=this.jdbcTemplate.update(sql,params); //给调用者返回受影响的行数 return num; } //删除账户 @Override public int deleteAccount(int id) { //1、定义SQL String sql = "delete from user01 where id=?"; //2、执行删除操作,返回的是受SQL语句影响的记录条数 /* * JdbcTemplate类中的所有方法调用后,都会返回受影响的行数, * 所以这里定义了一个整型变量num来作为返回值 */ int num = this.jdbcTemplate.update(sql,id); //3、返回受影响行数 return num; } }
applicationContext.xml中
配置id为dataSource数据源Bean
和id为jdbcTemplate的JDBC模板Bean
并将数据源注入到JDBC模板中,
最后定义一个id为accountDao的Bean,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 1、配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<!-- drverClassName:所使用的驱动名称,对应驱动JAR包中的Driver类 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 连接数据库的url -->
<!-- url:数据源所在地址 -->
<property name="url" value="jdbc:mysql://localhost/spring"></property>
<!-- 连接数据库的用户名 -->
<!-- username:访问数据库的用户名 -->
<property name="username" value="root"></property>
<!-- 连接数据库的密码 -->
<!-- password:访问数据库的密码 -->
<property name="password" value="123456"></property>
</bean>
<!-- 2、配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3、配置注入类 -->
<bean id="accountDao" class="com.java265.jdbc.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
测试类
package com.java265.jdbc;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
/**
* 使用execute()方法创建表
*/
@Test
public static void main01Test(String[] args) {
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取JdbcTemplat实例
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//使用该实例的execute(String sql)方法执行创建数据表的SQL语句
jdbcTemplate.execute("create table user03("
+ "id int primary key auto_increment,"
+ "username varchar(10),"
+ "balance double)");
System.out.println("创建成功");
}
@Test
public static void main02Test(String[] args){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
//创建AccountDao对象,并向Account对象中添加数据
Account account = new Account();
account.setId(3);
account.setUsername("jjj");
account.setBalance(3000.00);
//执行addAccount()方法,并获取返回结果
int num = accountDao.addAccount(account); if(num>0) {
System.out.println("成功插入"+num); }else { System.out.println("插入操作执行失败"); }
}
@Test
public static void main03Test(String[] args){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
//创建AccountDao对象,并向Account对象中添加数据
Account account = new Account();
account.setId(4);
account.setUsername("ggg");
account.setBalance(4000.00);
/**
* int num 定义一个整型变量
* accountDao.updateAccount(account)调用updateAccount(Account account)方法
*/
/*
* 调用流程:首先这里带着account数组参数进入到AccountDao接口,
* AccountDao接口带着数组,去找AccountDaoImpl实现类,
* 在AccountDaoImpl实现类中通过get方法获取数组参数,并利用JdbcTemplate类中的方法,进行相应操作,并给予返回值,
* AccountDaoImpl实现类将返回值传给调用它的测试类Test,
* 然后这里定义的整型num就获取到了AccountDaoImpl返回的参数
*/
//执行updateAccount()方法,并获取返回结果
int num = accountDao.updateAccount(account);
//接收AccountDaoImpl的返回值
if(num>0) {
System.out.println("成功更新"+num+"行数据");
}else {
System.out.println("更新操作执行失败");
}
}
public static void main(String[] args){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
/*这里的删除操作,只做了指定id删除的动作,所以不需要数组了
* 创建AccountDao对象,并向Account对象中添加数据
* Account account = new Account();
* account.setId(5);
*/
/**
* int num 定义一个整型变量
* accountDao.updateAccount(account)调用updateAccount(Account account)方法
*/
/*
* 调用流程:首先这里带着account数组参数进入到AccountDao接口,
* AccountDao接口带着数组,去找AccountDaoImpl实现类,
* 在AccountDaoImpl实现类中通过get方法获取数组参数,并利用JdbcTemplate类中的方法,进行相应操作,并给予返回值,
* AccountDaoImpl实现类将返回值传给调用它的测试类Test,
* 然后这里定义的整型num就获取到了AccountDaoImpl返回的参数
*/
//执行updateAccount()方法,并获取返回结果
int num = accountDao.deleteAccount(5);
//接收AccountDaoImpl的返回值
if(num>0) {
System.out.println("成功删除"+num+"行数据");
}else {
System.out.println("删除操作执行失败");
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。