update()方法可以完成插入、更新、删除数据的操作。
在上一条博文中,我们已经了解了execute()方法的使用,在上个实例的基础上来学习update()的使用方法。
Spring JdbcTemplate的常用方法--execute()
一、在com.itheima.jdbc包下创建Account类,在该类中定义id,username和balanca属性,以及对应的setter、getter和tostring方法
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;
}
@Override
public String toString() {
return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
}
}
快速添加getter/setter方法,在空白处右键-->Source-->Generate Getters and Setter
二、在com.itheima.jdbc包下创建接口AccountDao,并在接口中定义添加、更新和删除账户的方法
package com.itheima.jdbc;
public interface AccountDao {
//添加 int返回的是添加的记录条数
public int addAccount(Account account);
//更新
public int updateAccount(Account account);
//删除
public int deleteAccount(int id);
}
三、在com.itheima.jdbc包下创建接口的实现类AccountDaoImpl,并在类中实现添加、更新、删除账户的方法
package com.itheima.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
public class AccountDaoImpl implements AccountDao{
//声明JdbcTemplate属性及其setter方法
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//添加账户 ?为占位符
public int addAccount(Account account) {
//定义SQL
String sql = "insert into account(username,balance)value(?,?)";
//定义数组来存储SQL语句中的参数
Object[] objects = new Object[] {
account.getUsername(),
account.getBalance()
};
//执行添加操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql,objects);
return num;
}
@Override
public int updateAccount(Account account) {
//定义SQL
String sql = "update account set username=?,balance=? where id = ?";
//定义数组来存储SQL语句中的参数
Object[] objects = new Object[] {
account.getUsername(),
account.getBalance(),
account.getId()
};
//执行更新操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql,objects);
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;
}
}
四、在配置文件中,定义一个id为accountDaode 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.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:3306/spring"/>
<!-- 连接数据库的用户名 -->
<property name="username" value="root"/>
<!-- 连接数据库的密码 -->
<property name="password" value="123456"/>
</bean>
<!-- 2配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
五、在测试类中,添加测试方法
package com.itheima.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
/**
* 使用execute()方法建表
*/
@Test
public void createTableTest() {
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取JdbcTemplate实例
JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//使用execute()方法执行SQL语句,创建用户账户管理表account
String sql = "create table account("
+ "id int primary key auto_increment,"
+ "username varchar(50),"
+ "balance double)";
jdTemplate.execute(sql);
System.out.println("账户表account创建完成!");
}
@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("李四");
account.setBalance(50000.0);
//执行addAccount()方法,并获取返回结果
int num = accountDao.addAccount(account);
if (num>0) {
System.out.println("成功插入了"+num+"条数据!");
}else {
System.out.println("插入失败");
}
}
@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("王五");
account.setBalance(60000.0);
//执行updateAccount()方法,并获取返回结果
int num = accountDao.updateAccount(account);
if (num>0) {
System.out.println("成功修改了"+num+"条数据!");
}else {
System.out.println("修改失败");
}
}
@Test
public void deleteAccountTest() {
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
//执行deleteAccount()方法,并获取返回结果
int num = accountDao.deleteAccount(2);
if (num>0) {
System.out.println("成功删除了"+num+"条数据!");
}else {
System.out.println("删除失败");
}
}
}
六、运行结果
添加账户:
修改账户:
删除账户: