Spring提供了一种面向对象的方法将sql操作进行了封装。例如可以定义一个特定的SQL查询的对象,然后执行该对象的查询方法使其可以获取业务对象。业务对象的属性对应数据库的每一列。该查询是线程安全的且可以重复使用。
准备工作
https://blog.csdn.net/weixin_42518668/article/details/104276911
封装SQL查询执行
public class AccountByIdQuery extends MappingSqlQuery<Account> {
public AccountByIdQuery(DataSource dataSource){
super(dataSource,"select * from account where id = ?");
declareParameter(new SqlParameter(Types.BIGINT));
compile();
}
@Override
protected Account mapRow(ResultSet rs,int rowNum) throws SQLException {
Account account = new Account();
account.setBalance(rs.getDouble("balance"));
account.setLocked(rs.getBoolean("locked"));
account.setAccessTime(rs.getTimestamp("access_time"));
account.setOwnerName(rs.getString("owner_name"));
account.setId(rs.getLong("id"));
return account;
}
}
该类继承于MappingSqlQuery抽象类。
首先在构造方法中使用超类构造方法输入参数需要查询的sql语句,以及DataSource对象。
然后用declare方法定义输入的查询参数的类型。
然后用compile方法编译,之后可以重复使用。
重写mapRow方法,返回查询后的业务对象。
private MappingSqlQuery<Account> accountDaoAccountByIdQuery;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
}
public Account find(long id){
return accountDaoAccountByIdQuery.findObject(id);
}
在DaoImpl类中添加刚刚定义的MappingSqlQuery类,并设置其set方法。
使用MappingSqlQuery类中的findObject方法查询,输入参数,返回业务对象。
@Bean
public AccountDao accountDao(){
AccountDaoJdbcImpl accountDao = new AccountDaoJdbcImpl();
accountDao.setJdbcTemplate(jdbcTemplate());
accountDao.setAccountDaoAccountByIdQuery(mappingSqlQuery());
return accountDao;
}
@Bean
public MappingSqlQuery<Account> mappingSqlQuery(){
AccountByIdQuery accountByIdQuery = new AccountByIdQuery(dataSource());
return accountByIdQuery;
}
在spring的配置类中添加MappingSqlQuery的Bean
Account account = accountDao.find(101l);
System.out.println(account.getId());
System.out.println(account.getOwnerName());
System.out.println(account.getBalance());
System.out.println(account.getAccessTime());
System.out.println(account.isLocked());
在main方法中测试新添加的find方法。
封装SQL DML操作
可以使用SqlUpdate类将数据库的插入,更新,删除操作封装成一个可以重复使用的java对象。
public class AccountInsert extends SqlUpdate {
public AccountInsert(DataSource dataSource){
super(dataSource,"insert into account(owner_name,balance,access_time,locked) values(?,?,?,?)");
setParameters(new SqlParameter[]{new SqlParameter(Types.VARCHAR),new SqlParameter(Types.DOUBLE),new SqlParameter(Types.TIMESTAMP),new SqlParameter(Types.BOOLEAN)});
setReturnGeneratedKeys(true);
setGeneratedKeysColumnNames(new String[]{"id"});
compile();
}
}
public class AccountUpdate extends SqlUpdate {
public AccountUpdate(DataSource dataSource){
super(dataSource,"update account set(owner_name,balance,access_time,locked) = (?,?,?,?) where id = ?");
setParameters(new SqlParameter[]{
new SqlParameter(Types.VARCHAR),
new SqlParameter(Types.BOOLEAN),
new SqlParameter(Types.TIMESTAMP),
new SqlParameter(Types.BOOLEAN),
new SqlParameter(Types.BIGINT)
});
compile();
}
}
public class AccountDelete extends SqlUpdate {
public AccountDelete(DataSource d){
super(d,"delete account where id = ?");
setParameters(new SqlParameter[]{new SqlParameter(Types.BIGINT)});
compile();
}
}
继承SqlUpdate类分别实现各种操作
需要在其构造函数实现sql语句,dataSource,输入参数的类型,编译等操作