using JDBC with Spring
2017-07-18 00:40
176 查看
1、项目包结构:
2、文件说明:
public class Customer {private long id;
private String firstName, lastName;
public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
// getters & setters omitted for brevity
}
@SpringBootApplication public class Application implements CommandLineRunner { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String args[]) { SpringApplication.run(Application.class, args); } @Autowired JdbcTemplate jdbcTemplate; @Override public void run(String... strings) throws Exception { log.info("Creating tables"); jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); jdbcTemplate.execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); // Split up the array of whole names into an array of first/last names List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() .map(name -> name.split(" ")) .collect(Collectors.toList()); // Use a Java 8 stream to print out each tuple of the list splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); // Uses JdbcTemplate's batchUpdate operation to bulk load data jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); log.info("Querying for customer records where first_name = 'Josh':"); jdbcTemplate.query( "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh" }, (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) ).forEach(customer -> log.info(customer.toString())); } }
3、响应
相关文章推荐
- Spring Boot 之 Accessing Relational Data using JDBC with Spring
- How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway
- Using Quartz 1.8.5 with tomcat without Spring
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource
- iOS 动画:阻尼运动 usingSpringWithDamping: initialSpringVelocity: option
- Using JDBC with MySQL, Getting Started
- Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurati
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-使用工厂创建代理(Using the ProxyFactoryObject to create AOP proxies)
- How to build runnable JARs with Spring dependencies using Maven
- Spring. Data access using JDBC
- Spring JDBC Support with jdbc configuration in code directly
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- What the teacher said (using AspectJ with Spring part II).
- Using Flex with Spring
- Spring-boot JDBC with multiple DataSources sample
- 对收藏夹中AspectJ和Spring结合使用例子(What the teacher said (using AspectJ with Spring part II). )的补充
- Using JWT with Spring Security OAuth
- Using JWT with Spring Security OAuth
- 【技能库】--Spring JDBC framework with JdbcTemplate class(176)
- REST Security with JWT using Java and Spring Security