java bean对象 实现接口RowMapper用来处理查询出来的结果集
public class Account implements RowMapper<Account> { private String accountId; private String bankId; private String individualId; private String MBankId; private String EBankId; private String identityNum; private String accountCode; private String password; private String individualName; private String accountType; private String createTime; private String accountStatus; private Double balance; private String creditRating; private Double cashLimit; private Double borrowCash=0.0; private Double consumption=0.0; private List<Account> list; public Account() { } public Account(String accountId) { this.accountId = accountId; } public Account(String accountId, String bankId, String individualId, String MBankId, String EBankId, String identityNum,Double cashLimit, String accountCode, String password, String individualName, String accountType, String createTime, String accountStatus, Double balance, String creditRating,Double borrowCash,Double consumption) { this.accountId = accountId; this.bankId = bankId; this.individualId = individualId; this.MBankId = MBankId; this.EBankId = EBankId; this.identityNum = identityNum; this.accountCode = accountCode; this.password = password; this.individualName = individualName; this.accountType = accountType; this.createTime = createTime; this.accountStatus = accountStatus; this.balance = balance; this.creditRating = creditRating; this.cashLimit=cashLimit; this.borrowCash=borrowCash; this.consumption=consumption; } public Double getBorrowCash() { return borrowCash; } public void setBorrowCash(Double borrowCash) { this.borrowCash = borrowCash; } public Double getConsumption() { return consumption; } public void setConsumption(Double consumption) { this.consumption = consumption; } public String getAccountId() { return this.accountId; } public void setAccountId(String accountId) { this.accountId = accountId; } public String getBankId() { return this.bankId; } public void setBankId(String bankId) { this.bankId = bankId; } public String getIndividualId() { return this.individualId; } public void setIndividualId(String individualId) { this.individualId = individualId; } public String getMBankId() { return this.MBankId; } public void setMBankId(String MBankId) { this.MBankId = MBankId; } public String getEBankId() { return this.EBankId; } public void setEBankId(String EBankId) { this.EBankId = EBankId; } public String getIdentityNum() { return this.identityNum; } public void setIdentityNum(String identityNum) { this.identityNum = identityNum; } public String getAccountCode() { return this.accountCode; } public void setAccountCode(String accountCode) { this.accountCode = accountCode; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getIndividualName() { return this.individualName; } public void setIndividualName(String individualName) { this.individualName = individualName; } public String getAccountType() { return this.accountType; } public void setAccountType(String accountType) { this.accountType = accountType; } public String getCreateTime() { return this.createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getAccountStatus() { return this.accountStatus; } public void setAccountStatus(String accountStatus) { this.accountStatus = accountStatus; } public String getCreditRating() { return this.creditRating; } public void setCreditRating(String creditRating) { this.creditRating = creditRating; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } public List<Account> getList() { return list; } public void setList(List<Account> list) { this.list = list; } public Double getCashLimit() { return cashLimit; } public void setCashLimit(Double cashLimit) { this.cashLimit = cashLimit; } @Override public Account mapRow(ResultSet rs, int arg1) throws SQLException { Account account = new Account(); account.setAccountId(rs.getString("ACCOUNT_ID")); account.setAccountCode(rs.getString("ACCOUNT_CODE")); account.setBalance(rs.getDouble("BALANCE")); account.setCashLimit(rs.getDouble("CASH_LIMIT")); account.setIndividualId(rs.getString("INDIVIDUAL_ID")); account.setBorrowCash(rs.getDouble("BORROW_CASH")); account.setConsumption(rs.getDouble("CONSUMPTION")); account.setAccountStatus(rs.getString("ACCOUNT_STATUS")); account.setAccountType(rs.getString("ACCOUNT_TYPE")); account.setIndividualName(rs.getString("INDIVIDUAL_NAME")); account.setBankId(rs.getString("BANK_ID")); account.setConsumption(rs.getDouble("CONSUMPTION")); account.setCreateTime(rs.getTimestamp("CREATE_TIME").toString()); account.setCreditRating(rs.getString("CREDIT_RATING")); account.setEBankId(rs.getString("E_BANK_ID")); account.setIdentityNum(rs.getString("IDENTITY_NUM")); account.setMBankId(rs.getString("M_BANK_ID")); account.setPassword(rs.getString("PASSWORD")); return account; } }
DAO层的调用
public class AccountDao extends BaseDao { /** * 根据主键查询账户信息 * @param id * @return */ public Account queryAccountById(String id){ String sql ="select * from ACCOUNT where ACCOUNT_ID=?"; Object[] params = new Object[] {id}; List<Account> items = jdbcTemplate.query(sql,params,new Account()); if(items.isEmpty()){ return null; } return (Account)items.get(0); } /** * 更新账户的消费金额 * @param accountId * @param consumption */ public void updataAccount(String accountId,double consumption) { Object[] params =new Object[] {consumption,accountId}; jdbcTemplate.update("update ACCOUNT set CONSUMPTION = ? where ACCOUNT_ID = ?", params); } /** * 根据账户查询交易记录 * @param id * @return */ public List<Transaction> queryTransaction(String id){ String sql ="select * from TRANSACT where Account_Id=? AND TRANS_STATE='ing' FETCH FIRST 5 ROWS ONLY"; Object[] params = new Object[] {id}; List<Transaction> items = jdbcTemplate.query(sql,params,new Transaction()); return items; } /** * 计算交易总和 * @param transactionIds * @return */ public double countTransactionByIds(List<Object> transactionIds) { final List<Double> count =new ArrayList<Double>(); String sql ="select sum(DEPOSIT) as totle from TRANSACT where TRANS_ID in (:transactionIds)"; NamedParameterJdbcTemplate namedParameterJdbcTemplate = null; namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("transactionIds",transactionIds); namedParameterJdbcTemplate.query(sql, paramMap, new RowCallbackHandler(){ @Override public void processRow(ResultSet rs) throws SQLException { count.add(0, rs.getDouble("totle")); } }); return count.get(0); } /** * 变更交易状态 * @param transactionIdList */ public void updateTransact(List<Object> transactionIdList) { String sql ="update TRANSACT set TRANS_STATE = 'save' where TRANS_ID in (:transactionIds)"; NamedParameterJdbcTemplate namedParameterJdbcTemplate = null; namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("transactionIds",transactionIdList); namedParameterJdbcTemplate.update(sql, paramMap); } /** * 当月账单总和 * @param accountId * @return */ public double accountBill(String accountId){ Repayment repayment = new Repayment(); String sql ="select date(rtrim(char(year(min(start_time))))||'-'||rtrim(char(month(min(start_time))))||'-1') as startTime,date(rtrim(char(year(max(start_time))))||'-'||rtrim(char(month(max(start_time)+1 month)))||'-1') as endTime from REPAYMENT"; List<Repayment> items =jdbcTemplate.query(sql,new RowMapper<Repayment>() { @Override public Repayment mapRow(ResultSet rs, int i) throws SQLException { Repayment r = new Repayment(); r.setStartTime(rs.getDate("startTime")); r.setEndTime(rs.getDate("endTime")); return r; } }); if(items.size()>0){ repayment=(Repayment)items.get(0); } String sql2 = "select sum(MONTHLY_REPAYMENTS) as totle from REPAYMENT where ACCOUNT_ID=? AND date('" +repayment.getStartTime()+"') < date(current timestamp ) and date('"+repayment.getEndTime() +"') > date(current timestamp)"; Object[] params = new Object[] {accountId}; Double count = jdbcTemplate.queryForObject(sql2, params,Double.class); if(count==null){ count=0.0; } return count; } /** * * 批量插入数据 * */ public void save(final Individuals individuals) { String sql = "insert into INDIVIDUAL(INDIVIDUAL_ID,FAMILY_ID,INDIVIDUAL_NAME,englishname,MARITAL_STATUS,nationality,folk" + ",BIRTH_DAY,EDUCATION_LEVEL,address,email,sex,telephone,IDENTITY_NUM,CUSTOMER_TYPE,EMPLOYER_NAME" + ",EMPLOYER_ADDRESS,CERTIFICATE_TYPE,CERTIFICATE_NUMBER,POSTAL_CODE,LOCATION_CITY,OFFICE_TELEPHONE,OFFICE_FAXNUMBER" + ",WORK_YEAR,CREDIT_RATING,IMAGE) " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setString(1, IdUtils.uuid()); ps.setString(2, individuals.getFamilyId()); ps.setString(3, individuals.getIndividualName()[i]); ps.setString(4, individuals.getEnglishname()[i]); ps.setString(5, individuals.getMaritalStatus()[i]); ps.setString(6, individuals.getNationality()[i]); ps.setString(7, individuals.getFolk()[i]); ps.setTimestamp(8, DateUtils.getTimestamp(individuals.getBirthDay()[i])); ps.setString(9, individuals.getEducationLevel()[i]); ps.setString(10, individuals.getAddress()[i]); ps.setString(11, individuals.getEmail()[i]); ps.setString(12, individuals.getSex()[i]); ps.setString(13, individuals.getTelephone()[i]); ps.setString(14, individuals.getIdentityNum()[i]); ps.setString(15, individuals.getCustomerType()[i]); ps.setString(16, individuals.getEmployerName()[i]); ps.setString(17, individuals.getEmployerAddress()[i]); ps.setString(18, individuals.getCertificateType()[i]); ps.setString(19, individuals.getCertificateNumber()[i]); ps.setString(20, individuals.getPostalCode()[i]); ps.setString(21, individuals.getLocationCity()[i]); ps.setString(22, individuals.getOfficeTelephone()[i]); ps.setString(23, individuals.getOfficeFaxnumber()[i]); ps.setInt(24, individuals.getWorkYear()[i]); ps.setString(25, individuals.getCreditRating()[i]); ps.setString(26, individuals.getImage()[i]); } public int getBatchSize() { return individuals.getIndividualName().length; } }); } }
未完,碰到常用的还会继续更新的。
发表评论
-
Spring 配置多数据源
2013-03-21 16:07 1362Spring2.0.1以后的版本已经支持配置多数据源,并且 ... -
java POI 导出复杂要求的表格
2013-03-20 12:54 1169见附件 -
java 用iText导出PDF小例子
2013-03-20 12:16 1079iText是著名的开放源 ... -
String isEmpty()和isBank()的区别
2013-01-22 10:51 22381. public static boole ... -
java 两个相同元素的List,合并排序去除重复元素的快速算法
2013-01-07 11:36 12268两个数组: import java.util.Arr ... -
Spring 封装的quartz使用和配置
2012-11-06 10:11 1048quartz-spring.xml的配置: < ... -
quartz时间设置
2012-11-06 10:07 866序号 说明 是否必填 ...
相关推荐
本文将深入探讨Spring JdbcTemplate的常用方法,并结合提供的`JsonBean.java`和`JdbcUtils.java`文件,来理解其在实际应用中的使用。 首先,JdbcTemplate的核心功能在于它提供了一系列的方法来执行SQL语句,包括...
基于注解的Spring JdbcTemplate进一步增强了这种便利性,允许开发者直接在方法上使用注解来指定SQL语句。 ### 2. 注解使用 Spring JdbcTemplate的注解支持主要包含以下几个关键注解: - `@Query`: 用于定义SQL...
在这个实例工程中,我们将深入探讨Spring JdbcTemplate的基本使用、优势以及常见操作。 一、Spring JdbcTemplate简介 Spring JdbcTemplate的出现是为了弥补原生JDBC在编码上的繁琐,它通过模板方法模式,将SQL执行...
SpringJdbcTemplate是一个模板类,它提供了大量的方法来执行SQL查询、更新、存储过程等操作。这些方法会自动处理JDBC相关的资源关闭、异常转换等细节,使得代码更加整洁和健壮。 2. **数据库自适应** Spring...
4. 使用JdbcTemplate方法:调用JdbcTemplate的相应方法,如`update()`, `query()`, `execute()`等,传入SQL和参数。 5. 处理结果:根据操作类型,处理返回的结果集或者受影响的行数。 6. 事务管理:如果需要,可以...
这样,整个应用就可以通过Spring的依赖注入机制来获取并使用JdbcTemplate实例。 例如,一个简单的`applicationContext.xml`配置片段可能如下: ```xml <bean id="dataSource" class="org.springframework.jdbc....
Spring JDBC Template被用来简化数据库操作,它提供了一种模板方法模式,将SQL执行、结果集处理等细节进行了封装,使得开发者只需要关注SQL语句和处理结果即可。例如,登录验证可能涉及查询用户表,查找与输入用户名...
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
Spring JdbcTemplate是Spring框架中用于简化数据库操作的一个重要组件,它是Spring对JDBC的轻量级封装,旨在提供一种结构良好、易于使用的SQL执行机制,同时保持了JDBC的灵活性。在本实例中,我们将深入探讨Spring ...
在Spring框架中,JdbcTemplate是用于简化数据库操作的重要工具,它是Spring JDBC模块的一部分。通过使用JdbcTemplate,开发者可以避免编写大量的重复代码,如手动管理连接、处理结果集等,从而专注于业务逻辑。本文...
在本例中,我们将深入探讨Spring JdbcTemplate的工作原理、使用方式以及它的优点。 首先,JdbcTemplate的核心理念是通过预编译的SQL语句和参数绑定来防止SQL注入攻击,同时提供事务管理的支持。它提供了大量方法来...
值得注意的是,虽然有文档提到`JdbcTemplate`提供了`execute(String sql, Object[] params)`方法,但实际上在Spring 2.x版本中并不存在这样的方法签名。 #### 批量更新 当需要进行批量更新操作时,`JdbcTemplate`...
在Spring框架中,`JdbcTemplate`是用于简化Java数据库连接(JDBC)操作的一个核心组件。这个类提供了很多方法来执行SQL查询、更新语句,包括批处理操作。本篇文章将详细探讨`batchUpdate`方法及其在实际开发中的应用...
本篇将深入探讨Spring JDBCTemplate的使用及其源码解析,帮助你理解其背后的机制。 首先,让我们了解在不使用JDBCTemplate时,传统的JDBC操作通常涉及以下步骤:加载驱动、建立数据库连接、创建Statement或...
JdbcTemplate是Spring JDBC模块的核心,它提供了一组模板方法,用于执行SQL查询、更新、插入和删除操作。通过使用这个模板类,开发者可以避免直接与JDBC API交互,从而减少错误并提高代码的可读性。它还支持事务管理...
在这个例子中,我们注入了JdbcTemplate实例,并使用query方法执行SQL查询。传入的RowCallbackHandler将在遍历结果集时被调用,处理每一行数据。 **5. 总结** Spring JdbcTemplate简化了数据库操作,通过模板模式...
本篇文章将深入探讨Spring JdbcTemplate的使用方法和核心概念,并结合一个实际的图书馆系统Demo进行阐述。 一、Spring JdbcTemplate简介 Spring JdbcTemplate是Spring JDBC模块的核心组件,它提供了一套模板方法,...
在SSHnote_Spring_14_JDBCTemplate这个压缩包中,可能包含了关于这些知识点的详细教程、示例代码和练习,帮助读者深入理解和掌握Spring JdbcTemplate的使用。学习这些内容,开发者能够更加高效地在Spring环境中进行...
如果你使用的是Spring Boot,可以在`@SpringBootApplication`类的`main`方法中使用`SpringApplication.run()`启动应用,`JdbcTemplate`会自动注入。 4. **使用JdbcTemplate执行SQL**:现在,Servlet已经具备了执行...
综上所述,Spring JdbcTemplate结合MySQL的分页查询封装涉及到的主要知识点有:Spring的JdbcTemplate用法、MySQL的分页查询(LIMIT和OFFSET)、结果集映射、分页结果对象的构建、以及代码封装与复用。通过这些技术,...