二、update()方法
JdbcTemplate类中常用的update()方法
方法 | 说明 |
int update(String sql) | 该方法是最简单的update()方法重载形式,它直接执行传入的SQL语句,并返回受影响的行数。 |
int update(PreparedStatementCreator psc) | 该方法执行参数psc返回的语句,然后返回受影响的行数。 |
int update(String sql, PreparedStatementSetter pss) | 该方法通过参数pss设置SQL语句中的参数,并返回受影响的行数。 |
int update(String sql,Object… args) | 该方法可以为SQL语句设置多个参数,这些参数保存在参数args中,使用Object…设置SQL语句中的参数,要求参数不能为NULL,并返回受影响的行数。 |
下面通过一个案例演示update()方法的使用,该案例要求添加、更新、删除用户账户。案例具体实现步骤如下所示。
1、编写实体类
创建Account类,在该类中定义属性,以及其对应的getter/setter方法。
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、编写Dao层接口
创建接口AccountDao,并在接口中定义添加、更新和删除账户的方法。
public interface AccountDao {
// 添加
public int addAccount(Account account);
// 更新
public int updateAccount(Account account);
// 删除
public int deleteAccount(int id);
}
3、实现Dao层接口
创建AccountDao接口的实现类AccountDaoImpl,并在类中实现添加、更新和删除账户的方法。
public class AccountDaoImpl implements AccountDao {
// 定义JdbcTemplate属性,此处省略setter方法
private JdbcTemplate jdbcTemplate;
// 这里只展示(添加账户)的操作
public int addAccount(Account account) {
String sql = "insert into account(username,balance) value(?,?)";
Object[] obj = new Object[] {// 定义数组来存放SQL语句中的参数
account.getUsername(), account.getBalance() };
// 执行添加操作,返回的是受SQL语句影响的记录条数
return this.jdbcTemplate.update(sql, obj);}
}
4、编写配置文件
在applicationContext.xml中,定义一个id为accountDao的Bean,用于将jdbcTemplate注入到accountDao实例中。
<!--定义id为accountDao的Bean-->
<bean id="accountDao" class="com.itheima.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
5、测试添加功能
创建测试类TestAddAccount,该类主要用于添加用户账户信息。
public class TestAddAcount {
public static void main(String[] args) {
ApplicationContext applicationContext =new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao =
(AccountDao) applicationContext.getBean("accountDao");
Account account = new Account();
account.setUsername("tom"); account.setBalance(1000.00);
int num = accountDao.addAccount(account);
if (num > 0) { System.out.println("成功插入了" + num + "条数据!");
} else { System.out.println("插入操作执行失败!"); }
}}
6、查看执行第5步后的运行结果
在IDEA中启动TestAddAccount类,控制台会输出结果。此时再次查询spring数据库中的account表。
mysql> select * from account;
+----+----------+---------+
| id | username | balance |
+----+----------+---------+
| 1 | tom | 1000 |
+----+----------+---------+
1 rows in set (0.00 sec)
7、测试更新功能
执行完插入操作后,接下来调用JdbcTemplate类的update()方法执行更新操作。
public class TestUpdateAccount {
public static void main(String[] args) {
ApplicationContext applicationContext =new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountDao accountDao =
(AccountDao) applicationContext.getBean("accountDao");
Account account = new Account();
account.setId(1); account.setUsername("tom");
account.setBalance(2000.00);
int num = accountDao.updateAccount(account);
if (num > 0) {System.out.println("成功修改了" + num + "条数据!");
} else {System.out.println("修改操作执行失败!");} }}
8、查看执行第7步后的运行结果
在IDEA中启动TestUpdateAccount类,控制台会输出结果。此时再次查询spring数据库中的account表。
mysql> select * from account;
+----+----------+---------+
| id | username | balance |
+----+----------+---------+
| 1 | tom | 2000 |
+----+----------+---------+
1 rows in set (0.00 sec)
9、测试删除功能
创建测试类TestDeleteAccount,该类主要用于测试删除用户账户信息。
public class TestDeleteAccount {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext applicationContext =new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获取AccountDao实例
AccountDao accountDao =
(AccountDao) applicationContext.getBean("accountDao");
// 执行deleteAccount()方法,并获取返回结果
int num = accountDao.deleteAccount(1);
if (num > 0) {
System.out.println("成功删除了" + num + "条数据!");
} else {
System.out.println("删除操作执行失败!");
}
}
}
10、查看执行第9步后的运行结果
在IDEA中启动TestDeleteAccount类,控制台会输出结果。此时再次查询spring数据库中的account表。
mysql> select * from account;
Empty set (0.00 sec)