build.gradle
|
compile group: 'com.h2database', name: 'h2', version: '1.4.196' compile group: 'org.springframework', name: 'spring-core', version: '5.0.3.RELEASE' compile group: 'org.springframework', name: 'spring-context', version: '5.0.3.RELEASE' compile group: 'org.springframework', name: 'spring-beans', version: '5.0.3.RELEASE' compile group: 'org.springframework', name: 'spring-jdbc', version: '5.0.3.RELEASE' |
@Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
@ComponentScan({ "org.ma.lessons.spring" }) @Configuration public class SpringRootConfig { @Autowired DataSource dataSource; @Bean public JdbcTemplate getJdbcTemplate() { return new JdbcTemplate(dataSource); } @Bean public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); EmbeddedDatabase db = builder .setType(EmbeddedDatabaseType.H2) .addScript("db/sql/create-db.sql") .addScript("db/sql/insert-data.sql") .build(); return db; } } |
|
public static void main(String ... args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRootConfig.class); JdbcTemplate db = ctx.getBean(JdbcTemplate.class); log.info(String.valueOf(db)); } |
JdbcTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public static void main(String ... args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRootConfig.class); DataSource ds = ctx.getBean(DataSource.class); JdbcTemplate db = ctx.getBean(JdbcTemplate.class); log.info("count = " + db.queryForList("select * from solar").size()); List<String> planetList = new ArrayList<>(); planetList.add("Saturn"); planetList.add("Uranus"); planetList.add("Neptune"); db.batchUpdate("insert into SOLAR (name) values (?)", new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setString(1, planetList.get(i)); } @Override public int getBatchSize() { return planetList.size(); } }); log.info("count = " + db.queryForList("select * from solar").size()); } |
NamedParameterJdbcTemplate SimpleJdbcInsert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static void main(String ... args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRootConfig.class); DataSource ds = ctx.getBean(DataSource.class); JdbcTemplate db = ctx.getBean(JdbcTemplate.class); log.info("count = " + db.queryForList("select * from solar").size()); SimpleJdbcInsert insertIntoSolar = new SimpleJdbcInsert(ds).withTableName("solar") .usingColumns("name") .usingGeneratedKeyColumns("id"); Map<String, Object> param = new HashMap(); param.put("name", "Saturn"); insertIntoSolar.execute(param); log.info("count = " + db.queryForList("select * from solar").size()); param.clear(); param.put("name", "Uranus"); Number id = insertIntoSolar.executeAndReturnKey(param); log.info("id = " + id); SqlParameterSource parameters = new MapSqlParameterSource() .addValue("name", "Neptune"); id = insertIntoSolar.executeAndReturnKey(parameters); log.info("id = " + id); } |
SimpleJdbcCall MappingSqlQuery SqlUpdate StoredProcedure Execute stored procedure with Spring extends StoredProcedure