java - How to access database using Spring MVC -


I am going through reference documents - using the Spring Architecture to connect the database to a web application.

Here, the code creates a JDBC template using datasets.

  Public Sector JdbcCorporateEventDao Corporate Aventador {Private JdbcTemplate jdbcTemplate; Public Zero SetDataSource (Datasource Data Source) {this.jdbc Template = New JDBC Template (Datasource); } // JDBC-supported support for methods on corporateEventDao ... ...  

The data source is defined in an XML file -

  & Lt; ? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Bem xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schema location = "http: // Www .springframework.org / schema / beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> & Lt; Bean id = "Corporate Avantao" class = "com.example.jdbcCorporateEventDao" & gt; & Lt; Property Name = "Data Source" Riff = "Data Sources" / & gt; & Lt; / Bean & gt; & Lt ;! - DataSource (Parameter for configuration via a PropertyPlaceHolderConfigurer) - & gt; & Lt; Bean id = "data source" deleted-method = "off" class = "org.apache.commons.dbcp.basicDataSource" & gt; & Lt; Property name = "driverClassName" value = "$ {jdbc.driverClassName}" /> & Lt; Property name = "url" value = "$ {jdbc.url}" /> & Lt; Property name = "user name" value = "$ {jdbc.username}" /> & Lt; Property name = "password" value = "$ {jdbc.password}" /> & Lt; / Bean & gt; & Lt; / Bean & gt;  

I have a hard time understanding -

  1. Where to store this XML file? Is there a nomenclature for filename?
  2. How is the data source mapped from the XML to the code? For example, if I want to create another environment in the XML file, then how should the code be touched?

Appreciate your input

  • you It can be stored anywhere and give it the name of what you want! For example, if I have named my file datasource.exml and stored it under my src / main / resources /, then you make beans by importing that XML file into your spring context.

Config folder:

  & import; Resource processing = "classpath: config / datasource.xml" />  
  • I thinks are asking how data sources can be easily configured for different environments. There are several ways - I usually create some data source files (usually one mentioned above) in a directory.

      Datasource. DEV.xml datasource.QA.xml datasource.PROD. Xml  

Each of these is configured for a different database I then import into an environment variable that I call "env" I:

  & lt; Import Resources = "classpath: config / datasource. $ {Env} .xml" />  

If you are launching with Eclipse, you can specify it in run configuration under the "Environment" tab.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -