Experience summary 45-SPRINGMVC, multiple data sources

Source: Internet
Author: User

Sometimes you need to connect to multiple databases, data sources are inconsistent and need to be configured.

1, configuration XML. A data source, a template.

<bean id= "DataSource" class= "Com.base.MyDataSource" destroy-method= "Close" >
<property name= "Driverclassname" value= "${database.driverclassname}"/>
<property name= "url" value= "${database.url}"/>
<property name= "username" value= "${database.username}"/>
<property name= "Password" value= "${database.password}"/>
......
<bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

Multiple configurations can be repeated.

The corresponding class is inherited Basicdatasource. and encryption operations are possible.

2, public class.

@Autowired
protected JdbcTemplate JdbcTemplate;
@Autowired
Protected JdbcTemplate JdbcTemplate2;

Annotate multiple template.

3, inherit the public class, you can use the corresponding template database.

Note: In fact, the use of multiple databases is not very reasonable, you should design multiple projects, reduce coupling.

Even if a separate project provides an interface, it is also good and more secure to access only the interface of the project.

Experience summary 45-SPRINGMVC, multiple data sources

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.