Spring Boot: How to externalize JDBC datasource configuration?

后端 未结 2 830
别那么骄傲
别那么骄傲 2020-12-31 09:53

I have following Spring Boot controller code that works. (Some sensitive text was replaced)

package com.sample.server;

import java.sql.ResultSet;
import jav         


        
相关标签:
2条回答
  • 2020-12-31 10:04

    Change your controller to the following

    @RestController
    public class DetailReportController {
    
        @Autowired
        private JdbcTemplate jt;
    
        @RequestMapping(value="/report/detail", method=RequestMethod.GET)
        public List<UFGroup> detailReport() {
            List<UFGroup> results = jt.query(
                "select NID, SCode, SName from UFGroup",
                new RowMapper<UFGroup>(){
                    @Override
                    public UFGroup mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return new UFGroup(rs.getInt("NID"), rs.getString("SCode"), rs.getString("SName"));
                    }
                });
            return results;
        }
    
        private static class UFGroup
        {
            public int nid;
            public String scode;
            public String sname;
    
            public UFGroup(int nid, String scode, String sname)
            {
                this.nid = nid;
                this.scode = scode;
                this.sname = sname;
            }
        }
    }
    

    In src/main/resources add an application.properties with the following

    spring.datasource.driverClassName=net.sourceforge.jtds.jdbc.Driver
    spring.datasource.url=jdbc:jtds:sqlserver://111.11.11.11/DataBaseName
    spring.datasource.username=sa
    spring.datasource.password=password
    

    And simply start your application. No xml needed. Spring boot will create the DataSource and will add a default JdbcTemplate instance.

    Tip: Remove the dependency for org.apache.commons.dbcp spring-boot will give you the newer (and IMHO better) tomcat connection pool (which despite the name can be used entirely on its own).

    0 讨论(0)
  • 2020-12-31 10:14

    I will remodify your code in better approach.

    Firstly no need of using new operator in your code as you are using Spring so you can use the powerful feature of Spring i.e Dependency Injection.

    @RestController
    public class DetailReportController
    {       @Required
                private JdbcTemplate jt;
    
                //setter for the same
    
                @RequestMapping(value="/report/detail", method=RequestMethod.GET)
        public List<UFGroup> detailReport()
        {
    
            List<UFGroup> results = jt.query(
                "select NID, SCode, SName from UFGroup",
                new RowMapper<UFGroup>()
                {
                    @Override
                    public UFGroup mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        return new UFGroup(rs.getInt("NID"), rs.getString("SCode"),
                                rs.getString("SName"));
                    }
                });
    
            return results;
        }
    
        private static class UFGroup
        {
            public int nid;
            public String scode;
            public String sname;
    
            public UFGroup(int nid, String scode, String sname)
            {
                this.nid = nid;
                this.scode = scode;
                this.sname = sname;
            }
        }
    }
    

    application-context.xml

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="maxActive" value="100"/>
            <property name="maxIdle" value="30"/>
            <property name="maxWait" value="16000"/>
            <property name="minIdle" value="0"/>
        </bean>
    
       <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    
        <property name="location">
            <value>database.properties</value>
        </property>
    </bean>
    
    
        <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate;">
             <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="detailReportController" class="your class">
             <property name="jt" ref="jt"/>
        </bean>
    

    Another suggestion that you can move your code related to database in DAO classes,its very bad practice to have the same in Controller.Else if you want to stick to your approach use Properties class of java.util package

    See here

    0 讨论(0)
提交回复
热议问题