1
votes

I have a legacy spring mvc application which is using spring jdbc as ORM. Now I wanted to use spring boot and instead of mvc I will be converting it in to RestAPI. I have problem with database query part. Using propertyplaceholder xml confi the external sql query properties file is configured in the old application. Using Spring boot and latest annotation methods how can I configure this. My understanding is

  1. Place the query properties file in src/main/resources directory
  2. In DAO create property name same as the key ( name ) of the sql.
  3. create getter and setter for the key property

Is this is the right approach ? if yes if I use this how I will get the query in my DAO class ?. If not what is the best method.

2
Why do you want to put the SQL in a property file? - Simon Martinelli
few are large queries. Since it is an already exisiting application I wanted to use it as it is. - JAVA_CAT

2 Answers

2
votes

It is not a good approach but you can do it in several ways and I am always approaching to use JPA when you work with database and spring boot

application.yml

 emp:
    eid: "select eid from employee"
    name: "select ename from employee"

create a class for Employee under the model package

 @ConfigurationProperties("emp")
  @Getter
  @Setter
  public class Employee
 {
      private List<Integer> eid;
      private List<String> ename;
 }

create a bean instance for Employee under the config package

@Configuration
public class Config
{
     @Bean
     public Employee getEmployee()
    {
         return new Employee();
    }
}

then you can call Employee instance whenever you need in your project

@RestController
public class EmpController
{
    @Autowired
    Employee emp;
}
1
votes

After posting this question I didn't get much response. So I did some work from my side and the fixed it same way as I mentioned in the question. I didn't find any other method.

First a java class ApplicationPropertyConfig for PropertySourcesPlaceholderConfigurer . Property source file is my sql properties file.

@Configuration
@PropertySource("classpath:appSql.properties")
public class ApplicationPropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

appSql.properties

selectallemployees=select * from employee;

created another class with name ApplicationSQLWrapper.java which is annotated with @Component. This contains the property name same as the sql properties key with @value annotation.

@Component
public class ApplicationSQLWrapper{ 
    
    @Value("${selectallemployees}")
    private String selectallemployees;

//getter and setter

This SQL's can be accessed from DAO class by creating an Object of the Component class.