Jump to content

User:IOIOCL

From Wikipedia, the free encyclopedia

<dependency>

     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>


import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource;

@Configuration public class DataSourceConfig {

   @Primary
   @Bean
   @Qualifier("dataSource")
   public DataSource dataSource(DataSourceProperties dataSourceProperties) {
       return dataSourceProperties.initializeDataSourceBuilder().build();
   }

}


import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;

@Component @ConfigurationProperties(prefix = "custom.datasource") public class DatabaseProperties {

   private String url;
   private String username;
   private String password;
   // Getters and setters

}


import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param;

public interface UserRepository extends JpaRepository<User, Long> {

   @Query("SELECT u FROM User u WHERE u.username = :username")
   User findByUsername(@Param("username") String username);
   @Query("SELECT u FROM User u WHERE u.email = :email")
   List<User> findByEmail(@Param("email") String email);
   @Query("SELECT u FROM User u WHERE u.age > :age")
   List<User> findByAgeGreaterThan(@Param("age") int age);

}


<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>

spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update


@Entity public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   private String username;
   private String email;
   // Getters and setters

}


public interface UserRepository extends JpaRepository<User, Long> {

   // You can define custom queries here if needed

}

@Service public class UserService {

   private final UserRepository userRepository;
   @Autowired
   public UserService(UserRepository userRepository) {
       this.userRepository = userRepository;
   }
   public User saveUser(User user) {
       return userRepository.save(user);
   }
   public List<User> getAllUsers() {
       return userRepository.findAll();
   }

custom.datasource.url=jdbc:mysql://localhost:3306/mydb custom.datasource.username=root custom.datasource.password=mypassword

import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;

@Configuration public class DataSourceConfig {

   private final DatabaseProperties databaseProperties;
   public DataSourceConfig(DatabaseProperties databaseProperties) {
       this.databaseProperties = databaseProperties;
   }
   @Bean
   public DataSource dataSource() {
       return DataSourceBuilder
               .create()
               .url(databaseProperties.getUrl())
               .username(databaseProperties.getUsername())
               .password(databaseProperties.getPassword())
               .build();
   }

}

import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration @EntityScan(basePackages = "com.example.domain") @EnableJpaRepositories(basePackages = "com.example.repository", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager") public class JpaConfig { }