update()方法可以完成插入,更新和删除数据的操作,在JdbcTemplate类中,提供了一系列的update()方法,
接下来通过一个用户管理的案例来演示update()方法的使用,具体步骤如下:
1、在chapter04项目下的com.itheima.jdk包中创建Account类,在该类中定义id,username,balance属性,以及对应的getter/setter方法,代码如下所示:
package com.itheima.jdbc;
public class Account {
private Integer id;//账户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;
}
public String toString() {
return "Account [id=" + id + "," + "username=" + username + ",balance=" + balance + "]";
}
}
2、在com.itheima.jdbc包中,创建接口AccoutDao,并在接口中定义添加,更新,删除账户的方法;
package com.itheima.jdbc;
public interface AccountDao {
//添加
public int addAccount(Account account) ;
//更新
public int upAccount(Account account) ;
//删除
public int deleteAccount(int id) ;
}
3、在com.itheima.jdbc包中,创建AccountDao接口的实现类AccountDaoImpl,并在类中实现添加,删除,更新等操作
package com.itheima.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class AccountDaoImpl implements AccountDao {
//声明JdbcTemplate属性及其setter方法
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//添加账户
@Override
public int addAccount(Account account) {
//定义SQL数据库操作语句
String sql= "insert into account(username,balance) value(?,?)";
//定义数组来储存SQL语句中的参数
Object obj =new Object[] {
account.getUsername(),
account.getBalance(),
};
//执行添加操作,返回的是受SQL语句影响的记录条数
int num=this.jdbcTemplate.update(sql, obj);
return num;
}
//更新账户
@Override
public int upAccount(Account account) {
//定义SQL数据库操作语句
String sql= "update account set username=?,balance=? where id=?";
//定义数组来储存SQL语句中的参数
Object obj =new Object[] {
account.getUsername(),
account.getBalance(),
account.getId(),
};
//执行更新操作,返回的是受SQL语句影响的记录条数
int num=this.jdbcTemplate.update(sql, obj);
return num;
}
//删除操作
@Override
public int deleteAccount(int id) {
//定义SQL数据库操作语句
String sql= "delete from account where id=?";
//执行删除操作,返回的是受SQL语句影响的记录条数
int num=this.jdbcTemplate.update(sql, id);
return num;
}
}
4、在之前好的applicationContext.xml中(创建这个文件是在上一篇博客中点这里)定义一个id为accountDao的Bean,该Bean用于将jdbcTemplate注入到accountDao实例中,其代码如下所示:
<?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">
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 连接数据库的url -->
<property name="url" value="jdbc:mysql://localhost/spring" />
<!-- 连接数据库的用户名 -->
<property name="username" value="root" />
<!-- 连接数据库的用户密码 -->
<property name="password" value="itcast" />
</bean>
<!-- 配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认使用数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
重点是
<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
5、在测试类JdbcTemplate中添加一个测试方法addAccountTest(),该方法主要用于添加用户账户信息,具体代码如下:
@Test
public void addAccountTest() {
ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
//创建Account对象,并向Account对象中添加数据
Account account = new Account();
account.setBalance(1000.00);
account.setUsername("tom");
//执行update方法,并获取返回结果
int num=accountDao.addAccount(account);
if(num!=0)System.out.println("创建成功了,插入了"+num+"条数据");
else {
System.out.println("创建失败了");
}
}
进行Junit4测试运行,运行结果如下所示:
从结果来看是创建成功了,此时我们查询一下accunt表,
可以看到account表中确实添加了一条信息;
6、执行完插入操作后接下来使用JdbcTemplate类的update()方法执行更新操作,在测试类JdbcTemplateTest中,添加一个测试方法upAccountTest(),其代码如下所示:
@Test
public void upzAccountTest() {
ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
//创建Account对象,并向Account对象中添加数据
Account account = new Account();
account.setId(1);
account.setBalance(1000.00);
account.setUsername("tom");
//执行upAccount方法,并获取返回结果
int num=accountDao.upAccount(account);
if(num!=0)System.out.println("成功修改了,插入了"+num+"条数据");
else {
System.out.println("修改失败了");
}
}
运行结果如下所示:
现在再来看一看account表中信息
从表中来看确实是修改了表中tom的账户余额
7、现在来演示删除操作,在测试类JdbcTemplateTest中,添加一个测试方法deleteAccountTest(),具体代码如下:
@Test
public void deleteAccountTest() {
ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
//执行upAccount方法,删除id为1的账号,并获取返回结果
int num=accountDao.deleteAccount(1);
if(num!=0)System.out.println("成功删除了"+num+"条数据");
else {
System.out.println("删除失败了");
}
}
测试结果如图所示:
此时再次看看account表中的数据
发现为空了,说明删除操作成功了,
至此update()添加,修改,删除的操作全部演示完成了!