存储过程:
1.取一个返回的参数,比如是否执行成功:“success”
public class TestDaoImpl extends NamedParameterJdbcDaoSupport{
private SimpleJdbcCall getSingleParameterSjc;
@Override
protected void initDao() throws Exception {
super.initDao();
this.getSingleParameterSjc=
new SimpleJdbcCall(this.getDataSource())
.withProcedureName("sp_getSingleParameter");
}
public Boolean getSjcSingleParameter(String sId) {
Map<String,Object> param = new HashMap<String, Object>();
param.put("sId", sId);
return (Boolean)this.getSingleParameterSjc.execute(param).get("success");
}
}
private final static RowMapper<Perform> PERFORM_MAPPER = BeanPropertyRowMapper.newInstance(Perform.class);
private final static RowMapper<Integer> SINGLE_INTEGER_MAPPER = new SingleColumnRowMapper<Integer>(Integer.class);
2.取查询返回值
private final static String
LIST_RESULT = "ListResult",
ROWCOUNT_RESULT = "totalCount";
private SimpleJdbcCall findListResultSjc;
@Override
protected void initDao() throws Exception {
super.initDao();
this.findRestaurantsSjc = new SimpleJdbcCall(this.getDataSource())
.withProcedureName("sp_function_find_restaurants")
.declareParameters(
new SqlReturnResultSet(ROWCOUNT_RESULT, SINGLE_INTEGER_MAPPER),
new SqlReturnResultSet(RESTAURANT_RESULT, PERFORM_MAPPER));
}
public Map<String,Object> findListResultSjc(Parameter para) {
Map<String, Object> data = this.findListResultSjc.
execute(new BeanPropertySqlParameterSource(para));
List<Perform> list = (List<Perform>) data.get(RESTAURANT_RESULT);
List<Integer> total = ((List<Integer>) data.get(ROWCOUNT_RESULT));
Integer totalCount = null == total
|| total.size() == 0? 0 : total.get(0);
return data;
}
查询:
1.
private final static String
SQL_SELECT_PERFORM_BY_ID = "SELECT id,name from perform where id = ? ";
public Perform findEvalPerformById(String id) {
return this.getJdbcTemplate().
queryForObject(SQL_SELECT_PERFORM_BY_ID, PERFORM_MAPPER,id);
2.
private final static String SQL_FIND_PRIORITIZED_BY_ID =
"SELECT prioritized FROM perform WHERE id = :Id";
public Boolean findPrioritizedByPerformId(String Id) {
Map<String,Object> params = new HashMap<String, Object>();
params.put("Id", Id);
return this.getNamedParameterJdbcTemplate()
.queryForObject(SQL_FIND_PRIORITIZED_BY_PERFORMID,
params, SINGLE_BOOLEAN_MAPPER);
}
3.
private final static String SQL_FIND_PERFORM=
"SELECT * FROM perform WHERE id = :Id and type = :type ";
public List<Perform> findPerform(String Id, String type) {
Map<String,Object> params = new HashMap<String, Object>();
params.put("type", type);
params.put("Id", Id);
return this.getNamedParameterJdbcTemplate().
query(SQL_FIND_PERFORM, params, PERFORM_MAPPER );
}
修改:
private final static String SQL_UPDATE_PERFORM=
"update perform set name = :name ,type = :type WHERE id = :Id";
public int update(Perform perform) {
return this.getNamedParameterJdbcTemplate()
.update(SQL_UPDATE_PERFORM,
new BeanPropertySqlParameterSource(perform));
}
批量:
1.
public void insertBatchNamedParameter2(final List<Customer> customers){
SqlParameterSource[] params =
SqlParameterSourceUtils.createBatch(customers.toArray());
getSimpleJdbcTemplate().batchUpdate(
"INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (:custId, :name, :age)",
params);
}
2.
private final static String SQL_UPDATEPRIORITIZE =
" UPDATE perform"
+" SET priority = :priority WHERE id = :Id "
+" and s_id = :sId ";
public int updatePrioritize(List<Perform> performList) {
int[] updateCounts =
this.getNamedParameterJdbcTemplate().
batchUpdate(SQL_UPDATEPRIORITIZE,SqlParameterSourceUtils
.createBatch(performList.toArray()));
int result = 0;
for (int i = 0; i < updateCounts.length; i++) {
result += updateCounts[i];
}
return result;
}
分享到:
相关推荐
NULL 博文链接:https://1194867672-qq-com.iteye.com/blog/1291025
我的使用Spring的SimpleJdbcTemplate完成DAO操作实例 博文链接:https://zmx.iteye.com/blog/373749
本代码总结了,spring通过JdbcTemplate和SimpleJdbcTemplate来操作数据库的各种方法。 本代码使用的maven project。
SimpleJdbcTemplate Spring Mysql
11.2.3. SimpleJdbcTemplate类 11.2.4. DataSource接口 11.2.5. SQLExceptionTranslator接口 11.2.6. 执行SQL语句 11.2.7. 执行查询 11.2.8. 更新数据库 11.3. 控制数据库连接 11.3.1. DataSourceUtils类 11.3.2. ...
2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...
11.2.3. SimpleJdbcTemplate类 11.2.4. DataSource接口 11.2.5. SQLExceptionTranslator接口 11.2.6. 执行SQL语句 11.2.7. 执行查询 11.2.8. 更新数据库 11.3. 控制数据库连接 11.3.1. DataSourceUtils类 ...
2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...
11.5.2 SimpleJDBCTemplate 11.6 以OO方式访问数据库 11.6.1 使用MappingSqlQuery查询数据 11.6.2 使用SqlUpdate更新数据 11.6.3 使用StoredProcedure执行存储过程 11.6.4 SqlFunction类 11.7 小结 第12章 整合其他...
11.2.3. SimpleJdbcTemplate类 11.2.4. DataSource接口 11.2.5. SQLExceptionTranslator接口 11.2.6. 执行SQL语句 11.2.7. 执行查询 11.2.8. 更新数据库 11.3. 控制数据库连接 11.3.1. DataSourceUtils类 ...
spring4.0的jdbctemplate与springmvc全注解的整合登陆例子.里面已经包括需要的jar包.使用的是j2ee6如果没有请升级你的eclipse或myeclipse
11.5.2 SimpleJDBCTemplate 11.6 以OO方式访问数据库 11.6.1 使用MappingSqlQuery查询数据 11.6.2 使用SqlUpdate更新数据 11.6.3 使用StoredProcedure执行存储过程 11.6.4 SqlFunction类 11.7 小结 第12章 整合其他...
All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....
使用Maven+spring3.0MVC注释方式+SimpleJdbcTemplate开发的Web CRUD应用,没有将jar包上传,可以自己去下载