praktikum6
This commit is contained in:
BIN
Binary file not shown.
+15
-9
@@ -1,34 +1,40 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
|
||||||
https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>3.3.5</version>
|
<version>3.3.5</version>
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>de.componentware</groupId>
|
<groupId>de.componentware</groupId>
|
||||||
<artifactId>spring-demo</artifactId>
|
<artifactId>spring-demo</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>spring-demo</name>
|
<name>spring-demo</name>
|
||||||
<description>Spring Boot Demo – Componentware Übung 6</description>
|
<description>Spring Boot Demo – Componentware Übung 6</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>21</java.version>
|
<java.version>21</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.46</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.example.demo.praktikum6;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Auto implements Serializable {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
public String hersteller;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
public String typ;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
public String kennzeichen;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package org.example.demo.praktikum6;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class AutoDAO {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LogEntryDAO logEntryDAO;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void createAuto(String hersteller, String typ, String kennzeichen) {
|
||||||
|
try {
|
||||||
|
Auto auto = new Auto();
|
||||||
|
auto.hersteller = hersteller;
|
||||||
|
auto.typ = typ;
|
||||||
|
auto.kennzeichen = kennzeichen;
|
||||||
|
em.persist(auto);
|
||||||
|
logEntryDAO.createLogEntry("Auto angelegt: " + hersteller + " " + typ + " (" + kennzeichen + ")");
|
||||||
|
} catch (Exception e) {
|
||||||
|
logEntryDAO.createLogEntry("Fehler beim Anlegen des Autos: " + e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package org.example.demo.praktikum6;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class LogEntry {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Date date;
|
||||||
|
public LocalDateTime time;
|
||||||
|
public String message;
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package org.example.demo.praktikum6;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class LogEntryDAO {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void createLogEntry(String message) {
|
||||||
|
LogEntry entry = new LogEntry();
|
||||||
|
entry.date = new Date();
|
||||||
|
entry.time = LocalDateTime.now();
|
||||||
|
entry.message = message;
|
||||||
|
em.persist(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<LogEntry> getAllLogEntries() {
|
||||||
|
return em.createQuery("SELECT l FROM LogEntry l", LogEntry.class).getResultList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.example.demo.praktikum6;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TestClient implements CommandLineRunner {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AutoDAO autoDAO;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LogEntryDAO logEntryDAO;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) {
|
||||||
|
autoDAO.createAuto("BMW", "E32", "NE-IN 0000");
|
||||||
|
|
||||||
|
List<LogEntry> logs = logEntryDAO.getAllLogEntries();
|
||||||
|
System.out.println("=== Log-Einträge (" + logs.size() + ") ===");
|
||||||
|
for (LogEntry entry : logs) {
|
||||||
|
System.out.println("[" + entry.time + "] " + entry.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,10 @@ package org.example.demo.uebung6;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication(scanBasePackages = "org.example.demo")
|
||||||
|
@EntityScan(basePackages = "org.example.demo")
|
||||||
public class SpringDemoApplication {
|
public class SpringDemoApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -1 +1,7 @@
|
|||||||
spring.application.name=spring-demo
|
spring.application.name=spring-demo
|
||||||
|
|
||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
|||||||
Reference in New Issue
Block a user