spring-guide之spring-jdbc
来源:互联网 发布:sopcast网络电视节目源 编辑:程序博客网 时间:2024/05/10 18:13
spring-guide之spring-jdbc
创建一个关系数据的spring-jdbc
其中将使用jdk 8新特性来撸代码。mark一下java 8的IBM的新特性描述
https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
gradle的配置
创建gradle的配置文件build.gradle、
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'idea'apply plugin: 'spring-boot'jar { baseName = 'spring-guide' version = '0.1.0'}springBoot { mainClass = "gs.relational.data.access.Application"}repositories { mavenCentral()}sourceCompatibility = 1.8targetCompatibility = 1.8dependencies { compile( "org.springframework.boot:spring-boot-starter-web", "org.springframework.boot:spring-boot-starter", "org.springframework:spring-web", "com.fasterxml.jackson.core:jackson-databind", //gs.relational.data.access鍔犲叆 "org.springframework:spring-jdbc", "com.h2database:h2", "junit:junit" ) testCompile('org.springframework.boot:spring-boot-starter-test') }
代码实现
目标根据first_name查出该数据的last_name。其中数据库是h2database内存test版本。
创建gs.relational.data.access目录。
创建跟数据库的对应实体类,Customer.java
public class Customer { private Long id; private String firstName, lastName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Customer(Long id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; }}
定义一个spring-boot启动类
@SpringBootApplicationpublic class Application implements CommandLineRunner { private static final Logger log = LoggerFactory.getLogger(Application.class); @Autowired JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { log.info("create tables"); jdbcTemplate.execute("drop table customers if exists"); jdbcTemplate.execute("create table customers(id serial, first_name varchar(265), last_name varchar(265))"); List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Long") .stream().map(name -> name.split(" ")) .collect(Collectors.toList()); log.info(String.format("splitUpNames: %s", splitUpNames.get(0))); splitUpNames.forEach(name -> log.info(String.format("insert customer record fo %s %s", name[0], name[1])));// splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); jdbcTemplate.batchUpdate("insert into customers(first_name, last_name) values (?,?)", splitUpNames); log.info("query for customer records where first_name is 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())); }}
其中用到的java8的新特性,参考IBM的描述
https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
编译并运行
gradlew build;java -jar build/libs/spring-guide-0.1.0.jar2016-09-24 00:18:58.189 INFO 10300 --- [ main] gs.relational.data.access.Application : create tables2016-09-24 00:18:58.258 INFO 10300 --- [ main] gs.relational.data.access.Application : splitUpNames: John2016-09-24 00:18:58.273 INFO 10300 --- [ main] gs.relational.data.access.Application : insert customer record fo John Woo2016-09-24 00:18:58.300 INFO 10300 --- [ main] gs.relational.data.access.Application : insert customer record fo Jeff Dean2016-09-24 00:18:58.305 INFO 10300 --- [ main] gs.relational.data.access.Application : insert customer record fo Josh Long2016-09-24 00:18:58.379 INFO 10300 --- [ main] gs.relational.data.access.Application : query for customer records where first_name is Josh2016-09-24 00:18:58.388 INFO 10300 --- [ main] gs.relational.data.access.Application : gs.relational.data.access.Customer@3a079870
1 0
- spring-guide之spring-jdbc
- Spring学习之Spring JDBC
- spring-guide之rest-service
- spring-guide之scheduled-task
- spring-guide之consuming-rest
- spring-guide之uploading-file
- Spring之JDBC
- Spring DAO之JDBC
- Spring DAO之JDBC
- Spring之JDBC
- Spring JDBC之NamedParameterJdbcTemplate
- spring jdbc之query
- spring之JDBC
- 初见Spring之JDBC
- Spring之JDBC Template
- Spring源代码分析之Spring JDBC
- Spring数据源Spring数据源配置之JDBC
- Spring JDBC-Spring事务管理之ThreadLocal基础知识
- Android学习笔记之屏幕宽高、状态栏宽高、标题宽高以及left()、top()、right()、bottom()
- 多措并举保障受灾群众基本生活
- Codeforces Round #373 (Div. 2)
- 数组旋转
- Asteroids POJ3041 二分图最小顶点覆盖
- spring-guide之spring-jdbc
- 浅谈微信公众号营销背后的技术
- Leetcode 48. Rotate Image (Medium) (java)
- 习题2--注释和#符号
- 北大机试最小生成树
- 最大下标距离
- <理解共享信道访问协议系列1>共享信道前世今生
- const与define的异同
- 分享一个Andriod Base类