+spring sql example
This commit is contained in:
parent
0f9d9e1b4a
commit
93d5cb7a99
27
Java/spring/sql/build.gradle
Normal file
27
Java/spring/sql/build.gradle
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '3.1.4'
|
||||||
|
id 'io.spring.dependency-management' version '1.1.3'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.example'
|
||||||
|
version = '0.0.1-SNAPSHOT'
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = '17'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
runtimeOnly 'com.mysql:mysql-connector-j'
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named('test') {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
1
Java/spring/sql/settings.gradle
Normal file
1
Java/spring/sql/settings.gradle
Normal file
@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'sql'
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.example.sql;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface SheepRepository extends JpaRepository<SqlApplication.Sheep, Long> {}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.example.sql;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import org.slf4j.*;
|
||||||
|
import org.springframework.boot.*;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SqlApplication {
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public
|
||||||
|
class Sheep {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
public Long id;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
Sheep(String name_) {
|
||||||
|
this.name = name_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SqlApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public
|
||||||
|
CommandLineRunner cmdlr(SheepRepository repository) {
|
||||||
|
return (args) -> {
|
||||||
|
repository.save(new Sheep("Ross"));
|
||||||
|
repository.save(new Sheep("Joe"));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/spring
|
||||||
|
spring.datasource.username=spring
|
||||||
|
spring.datasource.password=passwd
|
||||||
|
spring.jpa.hibernate.ddl-auto=create
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.sql;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class SqlApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user