"Spring-boot Multi-database" Spring-boot JDBC with multiple DataSources sample

Source: Internet
Author: User

Application.properties
Spring.ds_items.driverclassname=org.postgresql.driver spring.ds_items.url=jdbc:postgresql://srv0/test Spring.ds_ Items.username=test0 spring.ds_items.password=test0   

Databaseitemsconfig.java
Package SB;  Import org.springframework.boot.autoconfigure.jdbc.TomcatDataSourceConfiguration; Import org.springframework.boot.context.properties.ConfigurationProperties; Import Org.springframework.context.annotation.Bean; Import org.springframework.context.annotation.Configuration; Import org.springframework.jdbc.core.JdbcTemplate;  Import Javax.sql.DataSource;  @Configuration @ConfigurationProperties (name = "Spring.ds_items") public class Databaseitemsconfig extends tomcatdatasourceconfiguration {      @Bean (name = "Dsitems") public     DataSource DataSource () {         return Super.datasource ();     }      @Bean (name = "Jdbcitems") public     jdbctemplate JdbcTemplate (DataSource dsitems) {         return new JdbcTemplate ( Dsitems);     



Databaseusersconfig.java
Package SB;  Import org.springframework.boot.autoconfigure.jdbc.TomcatDataSourceConfiguration; Import org.springframework.boot.context.properties.ConfigurationProperties; Import Org.springframework.context.annotation.Bean; Import org.springframework.context.annotation.Configuration; Import org.springframework.jdbc.core.JdbcTemplate;  Import Javax.sql.DataSource;  @Configuration @ConfigurationProperties (name = "Spring.ds_users") public class Databaseusersconfig extends tomcatdatasourceconfiguration {      @Bean (name = "Dsusers") public     DataSource DataSource () {         return Super.datasource ();     }      @Bean (name = "Jdbcusers") public     jdbctemplate JdbcTemplate (DataSource dsusers) {         return new JdbcTemplate ( dsusers);     }  



Itemrepository.java
Package SB; Import Org.slf4j.Logger; Import Org.slf4j.LoggerFactory; Import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.beans.factory.annotation.Qualifier; Import Org.springframework.jdbc.core.JdbcTemplate; Import Org.springframework.jdbc.core.RowMapper;  Import Org.springframework.stereotype.Repository; Import Java.sql.ResultSet;  Import java.sql.SQLException;      @Repository public class Itemrepository {protected final Logger log = Loggerfactory.getlogger (GetClass ());      @Autowired @Qualifier ("Jdbcitems") protected jdbctemplate jdbc;     Public Item GetItem (long id) {return jdbc.queryforobject ("select * from Sb_item WHERE id=?", Itemmapper, id); } private static final rowmapper<item> Itemmapper = new rowmapper<item> () {public Item Maprow (R             Esultset rs, int rowNum) throws SQLException {Item item = new Item (Rs.getlong ("id"), rs.getstring ("title")); Item.price = rs.getdouble("id");         return item; }     };  }



Userrepository.java
Package SB; Import Org.slf4j.Logger; Import Org.slf4j.LoggerFactory; Import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.beans.factory.annotation.Qualifier; Import Org.springframework.jdbc.core.JdbcTemplate; Import Org.springframework.jdbc.core.RowMapper;  Import Org.springframework.stereotype.Repository; Import Java.sql.ResultSet;  Import java.sql.SQLException;      @Repository public class Userrepository {protected final Logger log = Loggerfactory.getlogger (GetClass ());      @Autowired @Qualifier ("jdbcusers") protected jdbctemplate jdbc;     Public User GetUser (long id) {return jdbc.queryforobject ("select * from Sb_user WHERE id=?", Usermapper, id); } private static final rowmapper<user> Usermapper = new rowmapper<user> () {public User Maprow (R             Esultset rs, int rowNum) throws SQLException {User user = new User (Rs.getlong ("id"), rs.getstring ("name")); User.alias = rs.getstring ("Alias");         return user; }     };  }



Controller.java
Package SB;  Import Org.slf4j.Logger; Import Org.slf4j.LoggerFactory; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.RequestParam; Import Org.springframework.web.bind.annotation.RestController;  @RestController public class Controller {     protected final Logger log = Loggerfactory.getlogger (GetClass ());      @Autowired     private userrepository users;      @Autowired     private itemrepository items;      @RequestMapping ("test") public     String test () {         log.info ("test");         return "OK";     }      @RequestMapping ("user") public     user GetUser (@RequestParam ("id") long ID) {         log.info ("Get user");         return Users.getuser (ID);     }      @RequestMapping ("item") Public     Item GetItem (@RequestParam ("id") long ID) {         log.info ("Get item");         return Items.getitem (ID);     }  



Application.java
Package SB;  Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.EnableAutoConfiguration; Import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; Import Org.springframework.context.annotation.ComponentScan; Import org.springframework.context.annotation.Configuration;  @EnableAutoConfiguration (exclude = Datasourceautoconfiguration.class) @Configuration @ComponentScan (basepackages = " SB ") public class Application {public      static void Main (string[] args) throws Throwable {         springapplication app = New Springapplication (Application.class);         App.run ();     

"Spring-boot Multi-database" Spring-boot JDBC with multiple DataSources sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.