Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 2.19 KB

README.md

File metadata and controls

82 lines (63 loc) · 2.19 KB

Maven Central JavaDoc

Spring JDBC integration

Add dependency

		<dependency>
			<groupId>org.simpleflatmapper</groupId>
			<artifactId>sfm-springjdbc</artifactId>
			<version>2.9.1</version>
		</dependency>

Create mapper

See JdbcTemplateMapperFactoryTest for more examples.

class MyDao {
	private final RowMapper<DbObject> mapper =
		JdbcTemplateMapperFactory.newInstance().newRowMapper(DbObject.class);

	public void doSomething() {
		List<DbObject> results = template.query(DbHelper.TEST_DB_OBJECT_QUERY, mapper);
	}

	public void doSomethingElse() {
		 template
		 	.query(TEST_DB_OBJECT_QUERY,
		 		mapper.newResultSetExtractor((o) -> System.out.println(o.toString())));
	}
}

SqlParameterSource

class MyDao {
	private final SqlParameterSourceFactory<DbObject> parameterSourceFactory =
		JdbcTemplateMapperFactory.newInstance().newSqlParameterSourceFactory(DbObject.class);

	public void insertObject(DbObject object) {
        template.update(
            "INSERT INTO DBOBJECTS(id, name, email) VALUES(:id, :name, :email)",
            parameterSourceFactory.newSqlParameterSource(object));

	}

	public void insertObjects(Collection<DbObject> objects) {
        template.batchUpdate(
            "INSERT INTO DBOBJECTS(id, name, email) VALUES(:id, :name, :email)",
            parameterSourceFactory.newSqlParameterSources(objects));
	}
}

Crud

class MyDao {

	JdbcTemplateCrud<DbObject, Long> objectCrud;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		objectCrud =
			JdbcTemplateMapperFactory
				.newInstance()
				.<DbObject, Long>crud(DbObject.class, Long.class)
				.to(template, "TEST_DB_OBJECT");
	}

	public void insertObject(DbObject object) {
        crud.create(object);
	}

	public void insertObjects(Collection<DbObject> objects) {
		crud.create(objects);
	}
}