`

Spring----杂记

阅读更多

1、spring得到文件

File configFile = new ClassPathResource("paymentConfig.xml").getFile();

  注释:得到的是文件(包含路径)

 

2、spring 纯jdbc插入数据后获取插入数据的id的方法

//参数
final Map<Integer, Object> insertParams = new HashMap<Integer, Object>();
insertParams.put(1, domain.getName());
insertParams.put(2, domain.getType());
insertParams.put(3, domain.getIsDisplay());
insertParams.put(4, domain.getSortId());
insertParams.put(5, domain.getPid());
insertParams.put(6, domain.getNote());
insertParams.put(7, domain.getStoresId());

//org.springframework.jdbc.support.KeyHolder 用来在插入数据后获取插入数据的id
KeyHolder keyHolder = new GeneratedKeyHolder();

jdbcTemplate.update(new PreparedStatementCreator() {
	@Override
	public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
		String insertSql = "INSERT INTO t_hierarchy (name,type,isDisplay,sortId,pid,note,storesId) VALUES (?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement ps = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);
		
		//设置参数
		Set<Entry<Integer,Object>> entrySet = insertParams.entrySet();
		for (Entry<Integer, Object> entry : entrySet) {
			ps.setObject(entry.getKey(), entry.getValue());
		}
		return ps;
	}
}, keyHolder);

//keyHolder.getKey().intValue() 得到插入数据时生成的id(id设置为自动增长)
domain.setId(keyHolder.getKey().intValue());

 

3、spring 管理jdbc后的常用方法

      1、查询并迭代

public List findZBB() {  
	this.jdbcTemplate = Factory.getJdbcTemplate();	//得到连接  
	List zbList = new ArrayList();                  //返回页面的list  
	String sql = "select * from onduty where o_date > (sysdate - 1) and o_date < (sysdate + 1) order by DECODE(o_name1,'值班所领导',1,'值班科长',2,'值班民警',3)";  
	List findList = jdbcTemplate.queryForList(sql);  
	Iterator tt = findList.iterator();  
	while (tt.hasNext()) {  
		JobBean zbb = new JobBean();  
		Map jdMap = (Map) tt.next();  
		zbb.setJobid(jdMap.get("o_id") == null ? "" : jdMap.get("o_id").toString());        // 值班排班id  
		zbb.setJpost(jdMap.get("o_name1") == null ? "" : jdMap.get("o_name1").toString());  // 职位  
		zbb.setName(jdMap.get("o_name2") == null ? "" : jdMap.get("o_name2").toString());   // 添加值班姓名  
		zbb.setMobile(jdMap.get("o_name3") == null ? "" : jdMap.get("o_name3").toString()); // 添加值班人固定电话  
		zbb.setPhone(jdMap.get("o_name4") == null ? "" : jdMap.get("o_name4").toString());  // 添加值班人手机  
		zbList.add(zbb);  
	}  
	return zbList;  
} 

       

          2、查询唯一返回的值

public String backLsh(){  
	String sql = "select case" +  
		 " when to_number(substr(t.slid, 13, 8)) <" +  
		 " to_number(to_char(sysdate, 'yyyymmdd')) then" +  
		 " 'HLW-RK-CSDJ-' || to_char(sysdate, 'yyyymmdd') || '0001'" +  
		 " when to_number(substr(t.slid, 13, 8)) =" +  
		 " to_number(to_char(sysdate, 'yyyymmdd')) then" +  
		 " substr(t.slid," +  
		 " 1," +  
		 " length(t.slid) -" +  
		 " length(to_number(substr(t.slid, 21, 4)) + 1)) ||" +  
		 " to_char(to_number(substr(t.slid, 21, 4)) + 1)" +  
		 " end" +  
		 " from (select slbh slid" +  
		 " from slbh_list where  ywbh = 'HLW-RK-CSDJ') t";  
	  
	return this.getSession().createSQLQuery(sql).uniqueResult().toString();  
}

 

        3、执行更新

public boolean saveRkslbhzdz(String slbh){  
	String sql = "update slbh_list set slbh='" + slbh + "'where ywbh = 'HLW-RK-CSDJ'";     
	this.getSession().createSQLQuery(sql).executeUpdate();  
	return true;  
}  

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics