Java
/
Spring Boot
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
S
R
Q
Java Spring Boot Jdbc Mysql
mysql-connector-java spring-boot-starter-jdbc spring-boot-starter-data-jdbc
Schema
Create database table schema and add data.
create database db_example;
create user 'springuser'@'%' identified by 'ThePassword';
grant all on db_example.* to 'springuser'@'%';
create table users (
user_id int unsigned NOT NULL AUTO_INCREMENT,
email varchar(50) NOT NULL,
username varchar(50) NOT NULL,
PRIMARY KEY (user_id)
);
insert into users (email, username) values ('a@a.com', 'aaa');
insert into users (email, username) values ('b@b.com', 'bbb');
select * from users;
Dependencies
Start a Spring Web project and add dependencies to pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Config
Add datasource configuration properties.
# src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Application
Return rows from mysql database table.
/**
* Mysql App to get data (json)
*
* Query a database in Spring Boot without using JPA
* or creating a model class.
*
* The jdbcTemplate.queryForList() method is designed to
* return a list of rows from the database
*
* By returning the list of maps from the controller method,
* Spring will automatically convert it to a JSON object
*
* The Jackson library to get json is included as a transitive dependency
* of the spring-boot-starter-web dependency.
*/
package com.minte9.jdbc_mysql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@SpringBootApplication
@RestController
public class App {
// inject an instance of JdbcTemplate into the class
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@GetMapping("/")
public String users() {
String sql = "SELECT username FROM users";
List<String> users = jdbcTemplate.queryForList(sql, String.class);
return "Users: " + users.toString();
}
@GetMapping(path = "/getusers", produces = "application/json")
public List<Map<String, Object>> users_json() {
String sql = "SELECT email, username FROM users";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
return rows; // json
}
}
Run
Test and run the application.
mvn spring-boot:run
http://localhost:8080/users
// [aaa, bbb]
http://localhost:8080/users_json
// [{"email":"a@a.com","username":"aaa"}, ... }]
Archive
The .jar file is handy for deployment because it includes all the dependencies.
mvn package
cd target/
java -jar jdbc_mysql-0.0.1.jar
http://localhost:8080/users
// [aaa, bbb]
➥ Questions
Last update: 14 days ago