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
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);
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); }
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; } }; }
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; } }; }
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); }
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