spring boot

package com.javatechie.multiple.ds.api.book.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.javatechie.multiple.ds.api.model.book.Book;

@Repository
public interface BookRepository extends JpaRepository<Book, Integer>{

}


 

package com.javatechie.multiple.ds.api.config;

import java.util.HashMap;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "bookEntityManagerFactory", transactionManagerRef = "bookTransactionManager", basePackages = {
"com.javatechie.multiple.ds.api.book.repository" })
public class BookDBConfig {

@Bean(name = "bookDataSource")
@ConfigurationProperties(prefix = "spring.book.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "bookEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean bookEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("bookDataSource") DataSource dataSource) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return builder.dataSource(dataSource).properties(properties)
.packages("com.javatechie.multiple.ds.api.model.book").persistenceUnit("Book").build();
}

@Bean(name = "bookTransactionManager")
public PlatformTransactionManager bookTransactionManager(
@Qualifier("bookEntityManagerFactory") EntityManagerFactory bookEntityManagerFactory) {
return new JpaTransactionManager(bookEntityManagerFactory);
}
}

 

 


 

package com.javatechie.multiple.ds.api.config;

import java.util.HashMap;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = {
"com.javatechie.multiple.ds.api.user.repository" })
public class UserDBConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.user.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return builder.dataSource(dataSource).properties(properties)
.packages("com.javatechie.multiple.ds.api.model.user").persistenceUnit("User").build();
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}

 

 


 

 

@Table(name="BOOK_TB")
public class Book {

@Id
private int id;
private String name;

}

 

 


 

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="search_entry_table")
public class Places {
@Id
private int set_id;

private int set_active;
private String set_name;
private String set_type;

public int getSet_id() {
return set_id;
}

public void setSet_id(int set_id) {
this.set_id = set_id;
}

public int getSet_active() {
return set_active;
}

public void setSet_active(int set_active) {
this.set_active = set_active;
}

public String getSet_name() {
return set_name;
}

public void setSet_name(String set_name) {
this.set_name = set_name;
}

public String getSet_type() {
return set_type;
}

public void setSet_type(String set_type) {
this.set_type = set_type;
}

@Override
public String toString() {
return "Places{" +
"set_id=" + set_id +
", set_active=" + set_active +
", set_name='" + set_name + '\'' +
", set_type='" + set_type + '\'' +
'}';
}
}

 


 

package com.javatechie.multiple.ds.api.model.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="USER_TB")
public class User {
@Id
private int id;
private String userName;

}


 

package com.javatechie.multiple.ds.api.user.repository;

import com.javatechie.multiple.ds.api.model.user.Places;
import com.javatechie.multiple.ds.api.model.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PlaceRepository extends JpaRepository<Places, Integer> {

/* @Query("SELECT u FROM User u WHERE u.status = 1")
List<Places> getData();*/
}

 

 


 

package com.javatechie.multiple.ds.api.user.repository;

import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.javatechie.multiple.ds.api.model.user.User;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

/* @Query("SELECT u FROM User u WHERE u.status = 1")
List<String> getData();*/
}

 


 

package com.javatechie.multiple.ds.api;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.PostConstruct;

import com.javatechie.multiple.ds.api.model.user.Places;
import com.javatechie.multiple.ds.api.user.repository.PlaceRepository;
import org.springframework.beans.factory.annotation.Autowired;
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 com.javatechie.multiple.ds.api.book.repository.BookRepository;
import com.javatechie.multiple.ds.api.model.book.Book;
import com.javatechie.multiple.ds.api.model.user.User;
import com.javatechie.multiple.ds.api.user.repository.UserRepository;

@SpringBootApplication
@RestController
public class SpringBootMultipleDsApplication {

@Autowired
private BookRepository bookRepository;

@Autowired
private UserRepository userRepository;

@Autowired
PlaceRepository placeRepository;

@PostConstruct
public void addData2DB() {
/*userRepository.saveAll(Stream.of(new User(744, "John"), new User(455, "Pitter")).collect(Collectors.toList()));
*/bookRepository.saveAll(
Stream.of(new Book(111, "Core Java"), new Book(222, "Spring Boot")).collect(Collectors.toList()));
}

@GetMapping("/getPlaces")
public List<Places> getPlaces() {

//return userRepository.findAll();

return placeRepository.findAll();
}

@GetMapping("/getBooks")
public List<Book> getBooks()
{
return bookRepository.findAll();
}

public static void main(String[] args) {
SpringApplication.run(SpringBootMultipleDsApplication.class, args);
}
}--

 

=---------------

 

 

server.port=8085
#MYSQL
spring.user.datasource.jdbcUrl=jdbc:mysql://localhost:3306/quicktour
spring.user.datasource.username=root
spring.user.datasource.password=
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver

#H2 databse
spring.book.datasource.jdbcUrl=jdbc:h2:mem:testdb;;MODE=MySQL
spring.book.datasource.platform=h2
spring.book.datasource.username= sa
spring.book.datasource.password=
spring.book.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=none
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

 

 

 

 

 

 

 

 

Leave a Reply

Back To Top