Adds Spring JDBC. Creates a DataSource Spring Bean with name "mainDataSource" from v0.63 this will be based on HikariCP.
Simply add to the classpath
Maven
<dependency>
<groupId>com.aol.microservices</groupId>
<artifactId>micro-jdbc</artifactId>
<version>x.yz</version>
</dependency>
Gradle
compile 'com.aol.microservices:micro-jdbc:x.yz'
A datasource can be configured by setting the following properties in application.properties, instance.properties or via the Microserver annotation
db.connection.driver: (e.g. (org.hsqldb.jdbcDriver)
db.connection.url: (e.g. jdbc:hsqldb:mem:aname)
db.connection.username: (e.g. admin)
db.connection.password: (e.g. password)
db.connection.dialect: (e.g. org.hibernate.dialect.HSQLDialect)
db.connection.hibernate.showsql: (e.g. true | false)
db.connection.ddl.auto: (e.g. create-drop)
The Microserver annotation can also be used to set some default properties, or they can be set in an application.properties or instance.properties file (see wiki for more details).
The important properties for us to set are the datasource properties
@Microserver(properties={"db.connection.driver","org.hsqldb.jdbcDriver",
"db.connection.url","jdbc:hsqldb:mem:aname",
"db.connection.username", "sa"})
public class MyMainClass {
}
The micro-data plugin also facilitate JDBC use via the Spring JDBCTemplate. A Spring called SQL will be created that contains a JDBCTemplate, simply inject SQL into your classes.
e.g.
@Rest
@Path("/persistence")
public class PersistentResource {
private final SQL dao;
@Autowired
public PersistentResource(SQL dao) {
this.dao = dao;
}
@GET
@Produces("text/plain")
@Path("/create")
public String createEntity() {
dao.getJdbc().update("insert into t_jdbc VALUES (1,'hello','world',1)");
return "ok";
}
@GET
@Produces("application/json")
@Path("/get")
public JdbcEntity get() {
return dao.getJdbc().<JdbcEntity> queryForObject("select * from t_jdbc", new BeanPropertyRowMapper(JdbcEntity.class));
}
}