Buonasera informatici, ho proceduto nel seguente modo:
- Creato un pacchetto tramite initilizr web. assegnando dipendenze: JPA e MySql.
( Riporto di seguito POM.xml )
- Creata classe @Entity User con i relativi get and setter.
- Creata interfaccia IUserRepository
- Uso del metodo/bean CommandLineRunner
(Secondo me manca una dipendenza) riporto pom.xml:
codice:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jobmanagements</groupId>
<artifactId>homemanagement</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>homemanagement</name>
<description>Home Management</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build
codice:
package repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.jobmanagements.homemanagement.Entities.User;
@Repository
publicinterface IUserRepository extends CrudRepository<User, Long>{
@Override
default <S extends User> S save(S entity) {
// TODO Auto-generated method stub
returnnull;
}
}
codice:
publicstaticvoid main(String[] args) {
SpringApplication.run(HomeManagementApplication.class, args);
}
@Bean
CommandLineRunner commandLineRunner(IUserRepository userRep) {
return args -> {
User user = new User("Rossi", "Mario", "Url");
userRep.save(user);
};
}
L'applicativo crea la tabella User ma da questo errore in fase di avvio:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method commandLineRunner in com.jobmanagements.homemanagement.HomeManagementAp plication required a bean of type 'repository.IUserRepository' that could not be found.
Action:
Consider defining a bean of type 'repository.IUserRepository' in your configuration.
Inoltre io vorrei salvare tramite JButton nel DB ma per il momento son in fase di apprendimento ancora.