int update(String Sql) : 直接执行传入的sql语句,返回受影响的sql语句的行数;
int update(String sql ,Object … args): args设置sql中设置的参数,返回受影响的行数;
int update(String sql ,PreparedStatementSetter pss): 通过PreparedStatementSetter设置SQL语句中的参数,并返回受影响的行数;
int update(PreparedStatementCreator psc):用于执行从PreparedStatementCreator返回的语句,放回受影响的行数。
Account
package com.yzb.chapter04.example2;
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 +
'}';
}
}
AccountDao
package com.yzb.chapter04.example2;
public interface AccountDao {
//添加账户
public int addAccount(Account account);
// 更改用户
public int updateAccount(Account account);
//删除用户
public int deleteAccount(int id );
}
AccountDaoImpl
package com.yzb.chapter04.example2;
import org.springframework.jdbc.core.JdbcTemplate;
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 = {account.getUsername(),account.getBalance()};
//执行添加操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, obj);
return update;
}
//更改用户
@Override
public int updateAccount(Account account) {
//定义SQL
String sql = "update account set username=?,balance=? where id=?";
//定义一个数组存储sql中的参数
Object[] obj = {account.getUsername(),account.getBalance(),account.getId()};
//执行更改操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, obj);
return update;
}
//删除用户
@Override
public int deleteAccount(int id) {
//定义sql
String sql = "delete from account where id=?";
//执行更改操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, id);
return update;
}
}
package com.yzb.chapter04.example2;
import java.util.List;
public interface AccountDao {
//添加账户
public int addAccount(Account account);
// 更改用户
public int updateAccount(Account account);
//删除用户
public int deleteAccount(int id );
//通过id查询
public Account findAccountById(int id);
//查询所有
public List<Account> findAllAccount();
}
UserDaoImpl
package com.yzb.chapter04.example2;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
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 = {account.getUsername(),account.getBalance()};
//执行添加操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, obj);
return update;
}
//更改用户
@Override
public int updateAccount(Account account) {
//定义SQL
String sql = "update account set username=?,balance=? where id=?";
//定义一个数组存储sql中的参数
Object[] obj = {account.getUsername(),account.getBalance(),account.getId()};
//执行更改操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, obj);
return update;
}
//删除用户
@Override
public int deleteAccount(int id) {
//定义sql
String sql = "delete from account where id=?";
//执行更改操作,返回的是受sql语句影响的记录条数
int update = jdbcTemplate.update(sql, id);
return update;
}
//通过ID查询账户
@Override
public Account findAccountById(int id) {
//定义sql语句
String sql ="select * from account where id=?";
//创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
//将id保定到sql语句中,通过RowMapper返回一个Object类型的单行记录
Account account = this.jdbcTemplate.queryForObject(sql, rowMapper, id);
return account;
}
//查询所有的账户信息
@Override
public List<Account> findAllAccount() {
//定义sql语句
String sql ="select * from account";
//创建一个新的BeanPropertyRowMapper对象
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
//执行静态的sql语句查询,通过RowMapper返回结果
List<Account> query = this.jdbcTemplate.query(sql, rowMapper);
return query;
}
}
UpdateTest3
package com.yzb.chapter04.example2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UpdateTest3 {
public static void main(String[] args) {
//加载配置文件
String path = "com/yzb/chapter04/example2/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
//获取AccountDao的实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
Account accountById = accountDao.findAccountById(2);
System.out.println(accountById);
}
}
UpdatTest4
package com.yzb.chapter04.example2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class UpdateTest4 {
public static void main(String[] args) {
//加载配置文件
String path = "com/yzb/chapter04/example2/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
//获取AccountDao的实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
List<Account> allAccount = accountDao.findAllAccount();
for (Account account : allAccount) {
System.out.println(account);
}
}
}