|
| 1 | +<!--- |
| 2 | +Current Directory : /in28Minutes/git/spring-boot-examples/spring-boot-2-jdbc-with-h2 |
| 3 | +--> |
| 4 | + |
| 5 | +## Complete Code Example |
| 6 | + |
| 7 | + |
| 8 | +### /pom.xml |
| 9 | + |
| 10 | +```xml |
| 11 | +<?xml version="1.0" encoding="UTF-8"?> |
| 12 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 13 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 14 | + <modelVersion>4.0.0</modelVersion> |
| 15 | + |
| 16 | + <groupId>com.in28minutes.springboot.rest.example</groupId> |
| 17 | + <artifactId>spring-boot-2-jdbc-with-h2</artifactId> |
| 18 | + <version>0.0.1-SNAPSHOT</version> |
| 19 | + <packaging>jar</packaging> |
| 20 | + |
| 21 | + <name>spring-boot-2-jdbc-with-h2</name> |
| 22 | + <description>Spring Boot 2, JDBC and H2 - Example Project</description> |
| 23 | + |
| 24 | + <parent> |
| 25 | + <groupId>org.springframework.boot</groupId> |
| 26 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 27 | + <version>2.0.0.M6</version> |
| 28 | + <relativePath/> <!-- lookup parent from repository --> |
| 29 | + </parent> |
| 30 | + |
| 31 | + <properties> |
| 32 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 33 | + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
| 34 | + <java.version>1.8</java.version> |
| 35 | + </properties> |
| 36 | + |
| 37 | + <dependencies> |
| 38 | + <dependency> |
| 39 | + <groupId>org.springframework.boot</groupId> |
| 40 | + <artifactId>spring-boot-starter-jdbc</artifactId> |
| 41 | + </dependency> |
| 42 | + <dependency> |
| 43 | + <groupId>org.springframework.boot</groupId> |
| 44 | + <artifactId>spring-boot-starter-web</artifactId> |
| 45 | + </dependency> |
| 46 | + |
| 47 | + <dependency> |
| 48 | + <groupId>org.springframework.boot</groupId> |
| 49 | + <artifactId>spring-boot-devtools</artifactId> |
| 50 | + <scope>runtime</scope> |
| 51 | + </dependency> |
| 52 | + <dependency> |
| 53 | + <groupId>com.h2database</groupId> |
| 54 | + <artifactId>h2</artifactId> |
| 55 | + <scope>runtime</scope> |
| 56 | + </dependency> |
| 57 | + <dependency> |
| 58 | + <groupId>org.springframework.boot</groupId> |
| 59 | + <artifactId>spring-boot-starter-test</artifactId> |
| 60 | + <scope>test</scope> |
| 61 | + </dependency> |
| 62 | + </dependencies> |
| 63 | + |
| 64 | + <build> |
| 65 | + <plugins> |
| 66 | + <plugin> |
| 67 | + <groupId>org.springframework.boot</groupId> |
| 68 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 69 | + </plugin> |
| 70 | + </plugins> |
| 71 | + </build> |
| 72 | + |
| 73 | + <repositories> |
| 74 | + <repository> |
| 75 | + <id>spring-snapshots</id> |
| 76 | + <name>Spring Snapshots</name> |
| 77 | + <url>https://repo.spring.io/snapshot</url> |
| 78 | + <snapshots> |
| 79 | + <enabled>true</enabled> |
| 80 | + </snapshots> |
| 81 | + </repository> |
| 82 | + <repository> |
| 83 | + <id>spring-milestones</id> |
| 84 | + <name>Spring Milestones</name> |
| 85 | + <url>https://repo.spring.io/milestone</url> |
| 86 | + <snapshots> |
| 87 | + <enabled>false</enabled> |
| 88 | + </snapshots> |
| 89 | + </repository> |
| 90 | + </repositories> |
| 91 | + |
| 92 | + <pluginRepositories> |
| 93 | + <pluginRepository> |
| 94 | + <id>spring-snapshots</id> |
| 95 | + <name>Spring Snapshots</name> |
| 96 | + <url>https://repo.spring.io/snapshot</url> |
| 97 | + <snapshots> |
| 98 | + <enabled>true</enabled> |
| 99 | + </snapshots> |
| 100 | + </pluginRepository> |
| 101 | + <pluginRepository> |
| 102 | + <id>spring-milestones</id> |
| 103 | + <name>Spring Milestones</name> |
| 104 | + <url>https://repo.spring.io/milestone</url> |
| 105 | + <snapshots> |
| 106 | + <enabled>false</enabled> |
| 107 | + </snapshots> |
| 108 | + </pluginRepository> |
| 109 | + </pluginRepositories> |
| 110 | + |
| 111 | + |
| 112 | +</project> |
| 113 | +``` |
| 114 | +--- |
| 115 | + |
| 116 | +### /src/main/java/com/in28minutes/springboot/jdbc/h2/example/SpringBoot2JdbcWithH2Application.java |
| 117 | + |
| 118 | +```java |
| 119 | +package com.in28minutes.springboot.jdbc.h2.example; |
| 120 | + |
| 121 | +import org.slf4j.Logger; |
| 122 | +import org.slf4j.LoggerFactory; |
| 123 | +import org.springframework.beans.factory.annotation.Autowired; |
| 124 | +import org.springframework.boot.CommandLineRunner; |
| 125 | +import org.springframework.boot.SpringApplication; |
| 126 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 127 | + |
| 128 | +import com.in28minutes.springboot.jdbc.h2.example.student.Student; |
| 129 | +import com.in28minutes.springboot.jdbc.h2.example.student.StudentJdbcRepository; |
| 130 | + |
| 131 | +@SpringBootApplication |
| 132 | +public class SpringBoot2JdbcWithH2Application implements CommandLineRunner { |
| 133 | + |
| 134 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 135 | + |
| 136 | + @Autowired |
| 137 | + StudentJdbcRepository repository; |
| 138 | + |
| 139 | + public static void main(String[] args) { |
| 140 | + SpringApplication.run(SpringBoot2JdbcWithH2Application.class, args); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void run(String... args) throws Exception { |
| 145 | + |
| 146 | + logger.info("Student id 10001 -> {}", repository.findById(10001L)); |
| 147 | + |
| 148 | + logger.info("Inserting -> {}", repository.insert(new Student(10010L, "John", "A1234657"))); |
| 149 | + |
| 150 | + logger.info("Update 10003 -> {}", repository.update(new Student(10001L, "Name-Updated", "New-Passport"))); |
| 151 | + |
| 152 | + repository.deleteById(10002L); |
| 153 | + |
| 154 | + logger.info("All users -> {}", repository.findAll()); |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | +--- |
| 159 | + |
| 160 | +### /src/main/java/com/in28minutes/springboot/jdbc/h2/example/student/Student.java |
| 161 | + |
| 162 | +```java |
| 163 | +package com.in28minutes.springboot.jdbc.h2.example.student; |
| 164 | + |
| 165 | +public class Student { |
| 166 | + private Long id; |
| 167 | + private String name; |
| 168 | + private String passportNumber; |
| 169 | + |
| 170 | + public Student() { |
| 171 | + super(); |
| 172 | + } |
| 173 | + |
| 174 | + public Student(Long id, String name, String passportNumber) { |
| 175 | + super(); |
| 176 | + this.id = id; |
| 177 | + this.name = name; |
| 178 | + this.passportNumber = passportNumber; |
| 179 | + } |
| 180 | + |
| 181 | + public Student(String name, String passportNumber) { |
| 182 | + super(); |
| 183 | + this.name = name; |
| 184 | + this.passportNumber = passportNumber; |
| 185 | + } |
| 186 | + |
| 187 | + public Long getId() { |
| 188 | + return id; |
| 189 | + } |
| 190 | + |
| 191 | + public void setId(Long id) { |
| 192 | + this.id = id; |
| 193 | + } |
| 194 | + |
| 195 | + public String getName() { |
| 196 | + return name; |
| 197 | + } |
| 198 | + |
| 199 | + public void setName(String name) { |
| 200 | + this.name = name; |
| 201 | + } |
| 202 | + |
| 203 | + public String getPassportNumber() { |
| 204 | + return passportNumber; |
| 205 | + } |
| 206 | + |
| 207 | + public void setPassportNumber(String passportNumber) { |
| 208 | + this.passportNumber = passportNumber; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public String toString() { |
| 213 | + return String.format("Student [id=%s, name=%s, passportNumber=%s]", id, name, passportNumber); |
| 214 | + } |
| 215 | + |
| 216 | +} |
| 217 | +``` |
| 218 | +--- |
| 219 | + |
| 220 | +### /src/main/java/com/in28minutes/springboot/jdbc/h2/example/student/StudentJdbcRepository.java |
| 221 | + |
| 222 | +```java |
| 223 | +package com.in28minutes.springboot.jdbc.h2.example.student; |
| 224 | + |
| 225 | +import java.sql.ResultSet; |
| 226 | +import java.sql.SQLException; |
| 227 | +import java.util.List; |
| 228 | + |
| 229 | +import org.springframework.beans.factory.annotation.Autowired; |
| 230 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| 231 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 232 | +import org.springframework.jdbc.core.RowMapper; |
| 233 | +import org.springframework.stereotype.Repository; |
| 234 | + |
| 235 | +@Repository |
| 236 | +public class StudentJdbcRepository { |
| 237 | + @Autowired |
| 238 | + JdbcTemplate jdbcTemplate; |
| 239 | + |
| 240 | + class StudentRowMapper implements RowMapper<Student> { |
| 241 | + @Override |
| 242 | + public Student mapRow(ResultSet rs, int rowNum) throws SQLException { |
| 243 | + Student student = new Student(); |
| 244 | + student.setId(rs.getLong("id")); |
| 245 | + student.setName(rs.getString("name")); |
| 246 | + student.setPassportNumber(rs.getString("passport_number")); |
| 247 | + return student; |
| 248 | + } |
| 249 | + |
| 250 | + } |
| 251 | + |
| 252 | + public List<Student> findAll() { |
| 253 | + return jdbcTemplate.query("select * from student", new StudentRowMapper()); |
| 254 | + } |
| 255 | + |
| 256 | + public Student findById(long id) { |
| 257 | + return jdbcTemplate.queryForObject("select * from student where id=?", new Object[] { id }, |
| 258 | + new BeanPropertyRowMapper<Student>(Student.class)); |
| 259 | + } |
| 260 | + |
| 261 | + public int deleteById(long id) { |
| 262 | + return jdbcTemplate.update("delete from student where id=?", new Object[] { id }); |
| 263 | + } |
| 264 | + |
| 265 | + public int insert(Student student) { |
| 266 | + return jdbcTemplate.update("insert into student (id, name, passport_number) " + "values(?, ?, ?)", |
| 267 | + new Object[] { student.getId(), student.getName(), student.getPassportNumber() }); |
| 268 | + } |
| 269 | + |
| 270 | + public int update(Student student) { |
| 271 | + return jdbcTemplate.update("update student " + " set name = ?, passport_number = ? " + " where id = ?", |
| 272 | + new Object[] { student.getName(), student.getPassportNumber(), student.getId() }); |
| 273 | + } |
| 274 | + |
| 275 | +} |
| 276 | +``` |
| 277 | +--- |
| 278 | + |
| 279 | +### /src/main/resources/application.properties |
| 280 | + |
| 281 | +```properties |
| 282 | +# Enabling H2 Console |
| 283 | +spring.h2.console.enabled=true |
| 284 | +#Turn Statistics on |
| 285 | +spring.jpa.properties.hibernate.generate_statistics=true |
| 286 | +logging.level.org.hibernate.stat=debug |
| 287 | +# Show all queries |
| 288 | +spring.jpa.show-sql=true |
| 289 | +spring.jpa.properties.hibernate.format_sql=true |
| 290 | +logging.level.org.hibernate.type=trace |
| 291 | +``` |
| 292 | +--- |
| 293 | + |
| 294 | +### /src/main/resources/data.sql |
| 295 | + |
| 296 | +``` |
| 297 | +insert into student |
| 298 | +values(10001,'Ranga', 'E1234567'); |
| 299 | +
|
| 300 | +insert into student |
| 301 | +values(10002,'Ravi', 'A1234568'); |
| 302 | +``` |
| 303 | +--- |
| 304 | + |
| 305 | +### /src/main/resources/schema.sql |
| 306 | + |
| 307 | +``` |
| 308 | +create table student |
| 309 | +( |
| 310 | + id integer not null, |
| 311 | + name varchar(255) not null, |
| 312 | + passport_number varchar(255) not null, |
| 313 | + primary key(id) |
| 314 | +); |
| 315 | +``` |
| 316 | +--- |
| 317 | + |
| 318 | +### /src/test/java/com/in28minutes/springboot/jdbc/h2/example/SpringBoot2JdbcWithH2ApplicationTests.java |
| 319 | + |
| 320 | +```java |
| 321 | +package com.in28minutes.springboot.jdbc.h2.example; |
| 322 | + |
| 323 | +import org.junit.Test; |
| 324 | +import org.junit.runner.RunWith; |
| 325 | +import org.springframework.boot.test.context.SpringBootTest; |
| 326 | +import org.springframework.test.context.junit4.SpringRunner; |
| 327 | + |
| 328 | +@RunWith(SpringRunner.class) |
| 329 | +@SpringBootTest |
| 330 | +public class SpringBoot2JdbcWithH2ApplicationTests { |
| 331 | + |
| 332 | + @Test |
| 333 | + public void contextLoads() { |
| 334 | + } |
| 335 | + |
| 336 | +} |
| 337 | +``` |
| 338 | +--- |
0 commit comments