Spring将JDBC操作模块化为java对象

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,输入参数的类型,编译等操作

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值