spring jdbcTemplate的使用方法

@Service
@Transactional
public class LoginAuditExecSqlManagerImpl extends JdbcDaoSupport implements
		LoginAuditExecSqlManager {
	@Autowired
	public void init(DataSource ds) {// spring容器会发现这里需要被注入,自动的把容器提供的类的实例(DataSource)注入到这里来。供程序使用。
		setDataSource(ds);
	}


	public List query(String sql) {
		JdbcTemplate template = getJdbcTemplate();
		List list = template.query(sql, new LoginAuditRow());
		return list;
	}

}
public class LoginAuditRow implements RowMapper {

	public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		LoginAudit loginAudit=new LoginAudit();
		loginAudit.setAppName(rs.getString("APPNAME"));
		loginAudit.setAuditClass(rs.getString("AUDITCLASS"));
		loginAudit.setAuditLevel(rs.getString("AUDITLEVEL"));
		loginAudit.setDbUser(rs.getString("DBUSER"));
		loginAudit.setErrMsg(rs.getString("ERRMSG"));
		loginAudit.setIpAddress(rs.getString("IPAddress"));
		loginAudit.setTimestamp(rs.getString("timestamp"));
		return loginAudit;
	}

} 

你可能感兴趣的:(spring,sql)