DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Safeguarding Cloud Databases: Best Practices and Risks Engineers Must Avoid
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Engineering Resilience Through Data: A Comprehensive Approach to Change Failure Rate Monitoring
  • Taming Billions of Rows: How Metadata and SQL Can Replace Your ETL Pipeline

Trending

  • Complete Guide: Managing Multiple GitHub Accounts on One System
  • Why 99% Accuracy Isn't Good Enough: The Reality of ML Malware Detection
  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery
  1. DZone
  2. Data Engineering
  3. Databases
  4. DB Queries in spring's application-context.xml.

DB Queries in spring's application-context.xml.

By 
Shantanu Sikdar user avatar
Shantanu Sikdar
·
Aug. 13, 14 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
5.2K Views

Join the DZone community and get the full member experience.

Join For Free

While solving one of my assignment, I realize the age long practice of writing db queries in java class file is really a big pain. They are difficult to read and understand. Further if you want to modify it again a lot of task.

Thus, to get rid of the pain, I put the db queries in spring’s context xml.

Later I realize using such injections using xml, its not only easier to maintain and understand also we can have module wise segregation of db queries in different <util:map> or in different "xyzDAO-queries.xml". Further its easier to add more customers to the existing application.

Please see the code snippet area for the context.xml called "userDAO-queries.xml". I've created Map <util:map/> with an id. In the Map I put the key as <entry key="QUERY" > and values as <value>.db queries goes here....</value> , which contains the query.

The java classes are DBQueriesImpl.java and UserDAOImpl.java for fetching the exact query from the "xyzDAO-queries.xml".

Please see the comments in the java class.

Context XML (userDAO-queries.xml) part :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <util:map id="FIND_ALL_USER" key-type="java.lang.String" value-type="java.lang.String" >
      <entry key="QUERY" >
          <value>SELECT
                    user_id,
                    user_name,
                    user_password,
                    user_fname,
                    user_mname,
                    user_lname,
                    user_create_time 
                FROM
                    ty_users
          </value>
      </entry>
    </util:map>

    <util:map id="VALIDATE_USER_CREDENTIAL" key-type="java.lang.String" value-type="java.lang.String" >
    <entry key="QUERY" >
      <value>SELECT
                user_id,
                user_name,
                user_password 
              FROM
                  ty_users 
              WHERE
                  user_name = ? 
                  and user_password = ?
      </value>
    </entry> 
    </util:map>

    <util:map id="FIND_SPECIFIC_USER" key-type="java.lang.String" value-type="java.lang.String" >
    <entry key="QUERY" >
    	<value>SELECT user_id, user_name, user_password FROM ty_users WHERE user_id=?</value>
    </entry> 
    </util:map>

    <util:map id="CREATE_USER" key-type="java.lang.String" value-type="java.lang.String" >
    <entry key="QUERY" >
    	<value>INSERT INTO ty_users (user_name, user_password) VALUES (?, ?) </value>
    </entry>
    </util:map>

</beans>

Java Code Part:
UserDAOImpl.java
public class UserDAOImpl implements UserDAO{
  
	private static final Logger logger = LoggerFactory.getLogger(UserDAOImpl.class);// logger
	DBQueries dbQuery = new DBQueriesImpl("userDAO-queries.xml");//
	DBConnection dbConn = ConnectionFactory.getInstance().getConnectionMySQL();
	
  	@Override
	public List authenitcUserDetails(String userName, String userPassword) {
		Map queryMap = null;
		String query ;
		List> mappedUserList = null;
		List listUser = null;
		try{
			listUser = new ArrayList();
			queryMap = dbQuery.getQueryBeanFromFactory("VALIDATE_USER_CREDENTIAL"); //passing the query-name or Map Id.
			query =  dbQuery.getQuery(queryMap);//passing the query-map to get the query.

			//rest of the code is common fetching code.
			mappedUserList = dbConn.dbQueryRead(query, new Object[]{userName,userPassword});
			if(mappedUserList.size()>0){
				listUser =  new QueryResultSetMapperImpl().mapRersultSetToObject(mappedUserList, User.class);
			}
		}catch(Exception  ex){
			logger.error("fetchAllUser Error:: ", ex);
		}
		return listUser;
	}
}


DBQueriesImpl.java
public class DBQueriesImpl implements DBQueries {
    private static final Logger logger = LoggerFactory.getLogger(DBQueriesImpl.class);
	private ApplicationContext queriesCtx ;
    
  	/**
     * @Desc: Loading the queryContext xml
     * @param queryContext
     */
    public DBQueriesImpl(String queryContext) {
    	queriesCtx = new ClassPathXmlApplicationContext(queryContext);
    }
	
  	/**
     * @Desc: Reading from the loaded application context and getting the query-map, .  
     */
    @Override
    public Map getQueryBeanFromFactory(String beanId){
      	Map queryMap = null;
      	if (queriesCtx != null && beanId != null) {
      		queryMap = (Map)queriesCtx.getBean(beanId);
      	}
    	return queryMap;
    }
  
   /**
    * @Desc: Getting the exact query from the query-map, .  
    */
    @Override
    public String getQuery(Map queryMap) {
  		String query=null;
    	try{
    		if(queryMap.containsKey(QueryConstants.QUERY_NODE)){
    			query = (String) queryMap.get(QueryConstants.QUERY_NODE);
    			queryMap.remove(QueryConstants.QUERY_NODE);
    		}else{
    			throw new NoSuchFieldError();
    		}
    	}catch(Exception excp){
    		excp.printStackTrace();
    	}
    	return query;
    }
}



With this kind of setting I can also solve few other problems. In one of my assignment I used this trick. In the application, various clients have different set of data requirements thus, different set of sql-queries. Now instead of writing java classes to met client requirement we just write the set of spring-context.xmls having sql and used those spring-context at runtime and fetch data.


 

Database

Opinions expressed by DZone contributors are their own.

Related

  • Safeguarding Cloud Databases: Best Practices and Risks Engineers Must Avoid
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Engineering Resilience Through Data: A Comprehensive Approach to Change Failure Rate Monitoring
  • Taming Billions of Rows: How Metadata and SQL Can Replace Your ETL Pipeline

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: