Created
July 29, 2013 14:49
-
-
Save agnaldo4j/6104851 to your computer and use it in GitHub Desktop.
Acesso a dados com Scala e SpringData.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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