学科分类
目录
SSM框架

update()

update()方法可以完成插入、更新和删除数据的操作。在JdbcTemplate类中,提供了一系列的update()方法,其常用方法如表1所示。

表1 JdbcTemplate类中常用的update()方法

方法 说明
int update(String sql) 该方法是最简单的update方法重载形式,它直接执行传入的SQL语句,并返回受影响的行数。
int update(PreparedStatementCreator psc) 该方法执行从PreparedStatementCreator返回的语句,然后返回受影响的行数。
int update(String sql, PreparedStatementSetter pss) 该方法通过PreparedStatementSetter设置SQL语句中的参数,并返回受影响的行数。
int update(String sql,Object... args) 该方法使用Object…设置SQL语句中的参数,要求参数不能为NULL,并返回受影响的行数。

接下来,通过一个用户账户管理的案例来演示update()方法的使用,具体步骤如下。

(1)在chapter04项目的com.itheima.jdbc包中,创建Account类,在该类中定义id、username和balance属性,以及其对应的getter/setter方法,如文件1所示。

文件1 Account.java

 1    package com.itheima.jdbc;
 2    public class Account {
 3        private Integer id;       // 账户id
 4        private String username; // 用户名
 5        private Double balance;  // 账户余额
 6        public Integer getId() {
 7            return id;
 8        }
 9        public void setId(Integer id) {
 10            this.id = id;
 11        }
 12        public String getUsername() {
 13            return username;
 14        }
 15        public void setUsername(String username) {
 16            this.username = username;
 17        }
 18        public Double getBalance() {
 19            return balance;
 20        }
 21        public void setBalance(Double balance) {
 22            this.balance = balance;
 23        }
 24        public String toString() {
 25            return "Account [id=" + id + ", "
 26                    + "username=" + username + 
 27                    ", balance=" + balance + "]";
 28        }
 29    }

(2)在com.itheima.jdbc包中,创建接口AccountDao,并在接口中定义添加、更新和删除账户的方法,如文件2所示。

文件2 AccountDao.java

 1    package com.itheima.jdbc;
 2    public interface AccountDao {
 3        // 添加
 4        public int addAccount(Account account);
 5        // 更新
 6        public int updateAccount(Account account);
 7        // 删除
 8        public int deleteAccount(int id);
 9    }

(3)在com.itheima.jdbc包中,创建AccountDao接口的实现类AccountDaoImpl,并在类中实现添加、更新和删除账户的方法,编辑后如文件3所示。

文件3 AccountDaoImpl.java

 1    package com.itheima.jdbc;
 2    import org.springframework.jdbc.core.JdbcTemplate;
 3    public class AccountDaoImpl implements AccountDao {
 4        // 声明JdbcTemplate属性及其setter方法
 5        private JdbcTemplate jdbcTemplate;
 6        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 7            this.jdbcTemplate = jdbcTemplate;
 8        }
 9        // 添加账户
 10        public int addAccount(Account account) {
 11            // 定义SQL
 12            String sql = "insert into account(username,balance) value(?,?)";
 13            // 定义数组来存放SQL语句中的参数
 14            Object[] obj = new Object[] { 
 15                               account.getUsername(), 
 16                               account.getBalance() 
 17             };
 18            // 执行添加操作,返回的是受SQL语句影响的记录条数
 19            int num = this.jdbcTemplate.update(sql, obj);
 20            return num;
 21        }
 22        // 更新账户
 23        public int updateAccount(Account account) {
 24            // 定义SQL
 25            String sql = "update account set username=?,balance=? where id = ?";
 26            // 定义数组来存放SQL语句中的参数
 27            Object[] params = new Object[] { 
 28                                   account.getUsername(), 
 29                                   account.getBalance(), 
 30                                   account.getId() 
 31              };
 32            // 执行添加操作,返回的是受SQL语句影响的记录条数
 33            int num = this.jdbcTemplate.update(sql, params);
 34            return num;
 35        }
 36        // 删除账户
 37        public int deleteAccount(int id) {
 38            // 定义SQL
 39            String sql = "delete  from account where id = ? ";
 40            // 执行添加操作,返回的是受SQL语句影响的记录条数
 41            int num = this.jdbcTemplate.update(sql, id);
 42            return num;
 43        }
 44    }

从上述三种操作的代码可以看出,添加、更新和删除操作的实现步骤类似,只是定义的SQL语句有所不同。

(4)在applicationContext.xml中,定义一个id为accountDao的Bean,该Bean用于将jdbcTemplate注入到accountDao实例中,其代码如下所示。

<!--定义id为accountDao的Bean-->
<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
    <!-- 将jdbcTemplate注入到accountDao实例中 -->
    <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

(5)在测试类JdbcTemplateTest中,添加一个测试方法addAccountTest(),该方法主要用于添加用户账户信息,其代码如下所示。

@Test
public void addAccountTest() {
    // 加载配置文件
    ApplicationContext applicationContext = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
    // 获取AccountDao实例
    AccountDao accountDao = 
            (AccountDao) applicationContext.getBean("accountDao");
    // 创建Account对象,并向Account对象中添加数据
    Account account = new Account();
    account.setUsername("tom");
    account.setBalance(1000.00);
    // 执行addAccount()方法,并获取返回结果
    int num = accountDao.addAccount(account);
    if (num > 0) {
        System.out.println("成功插入了" + num + "条数据!");
    } else {
        System.out.println("插入操作执行失败!");
    }
}

在上述代码中,获取了AccountDao的实例后,又创建了Account对象,并向Account对象中添加了属性值。然后调用了AccountDao对象的addAccount()方法向数据表中添加一条数据。最后,通过返回的受影响的行数来判断数据是否插入成功。

使用Junit4测试运行后,控制台的输出结果如图1所示。

图1 运行结果

此时再次查询数据库中的account表,其结果如图2所示。

图2 account表

从图2可以看出,使用JdbcTemplate的update()方法已成功向数据表中插入了一条数据。

(6)执行完插入操作后,接下来使用JdbcTemplate类的update()方法执行更新操作。在测试类JdbcTemplateTest中,添加一个测试方法updateAccountTest(),其代码如下所示。

@Test
public void updateAccountTest() {
    // 加载配置文件
    ApplicationContext applicationContext = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
    // 获取AccountDao实例
    AccountDao accountDao = 
            (AccountDao) applicationContext.getBean("accountDao");
    // 创建Account对象,并向Account对象中添加数据
    Account account = new Account();
    account.setId(1);
    account.setUsername("tom");
    account.setBalance(2000.00);
    // 执行updateAccount()方法,并获取返回结果
    int num = accountDao.updateAccount(account);
    if (num > 0) {
        System.out.println("成功修改了" + num + "条数据!");
    } else {
        System.out.println("修改操作执行失败!");
    }
}

与addAccountTest()方法相比,更新操作的代码增加了id属性值的设置,并将余额修改为2000后,调用了AccountDao对象中的updateAccount()方法执行对数据表的更新操作。

使用Junit4运行方法后,再次查询数据库中的account表,其结果如图3所示。

图3 account表

从图3可以看出,使用update()方法已成功更新了account表中id为1的账户余额信息。

(7)在测试类JdbcTemplateTest中,添加一个测试方法deleteAccountTest(),来执行删除操作,其代码如下所示。

@Test
public void deleteAccountTest() {
    // 加载配置文件
    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("删除操作执行失败!");
    }
}

在上述代码中,获取了AccountDao的实例后,执行了实例中的deleteAccount()方法来删除id为1的数据。

使用Junit4测试运行方法后,查询account表中数据,其结果如图4所示。

图4 account表

从图4可以看出,已成功通过update()方法删除了id为1的数据。由于account表中只有一条数据,所以删除后表中数据为空。

点击此处
隐藏目录