Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Acesso a dados com Scala e SpringData.
import javax.sql.DataSource
import scala.collection.JavaConversions._
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.util.LinkedCaseInsensitiveMap
import org.springframework.jdbc.datasource.DriverManagerDataSource
/*
* Conexão a base de dados
*/
object DataSourceBuilder {
def dataSource: DataSource = dataSourceOrigin;
private def dataSourceOrigin(): DataSource = {
val dataSourceOrigin: DriverManagerDataSource = new DriverManagerDataSource();
dataSourceOrigin.setDriverClassName("com.mysql.jdbc.Driver");
dataSourceOrigin.setUrl("jdbc:mysql://[servidor]:3306/[base-de-dados]");
dataSourceOrigin.setUsername("user");
dataSourceOrigin.setPassword("password");
return dataSourceOrigin
}
}
/*
* Estrutura para consulta usando springdata
*/
object CameraSelector {
val SELECT_ALL:String = "SELECT * FROM SUA_TABELA"
def buildNewWith(dataSource:DataSource): CameraSelector = new CameraSelector(dataSource)
}
class CameraSelector(val dataSource:DataSource) {
def execute(): List[LinkedCaseInsensitiveMap[AnyRef]] = {
jdbcTemplate.queryForList(CameraSelector.SELECT_ALL).toList.asInstanceOf[List[LinkedCaseInsensitiveMap[AnyRef]]]
}
private def jdbcTemplate() : JdbcTemplate = new JdbcTemplate(dataSource)
}
/*
* Uso das classes anteriores
*/
CameraSelector.buildNewWith( DataSourceBuilder.dataSource ).execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.