美文网首页
spring jdbc

spring jdbc

作者: 谢贤byte | 来源:发表于2020-10-10 14:46 被阅读0次

    database.properties

    subject.driverClassName=oracle.jdbc.driver.OracleDriver
    subject.url=jdbc:oracle:thin:@127.0.0.1:instancename
    subject.username=username
    subject.password=password
    

    context.xml

    <bean id="subjectDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
        <property name="url" value="${subject.url}" />
        <property name="username" value="${subject.username}" />
        <property name="password" value="${subject.password}" />
        <property name="driverClassName" value="${subject.driverClassName}" />
    </bean>
    
    <!-- jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="subjectDataSource" />
    </bean>
    

    Controller.java

    @Controller
    public class GeneralRequestController { 
        
        @Autowired
        private JdbcTemplate jdbc;
        
        /**
         * 通用响应请求的方法
         * @return
         */
        @RequestMapping("generalRequest")
        @ResponseBody
        public List<Map<String, String>> generalRequest(HttpServletRequest request) {
            String tableName = request.getParameter("tableName");
            String sql = "select * from " + tableName;
    
            List<Map<String, String>> resultList = jdbc.query(sql, new RowMapper<Map<String,String>>() {
                public Map<String,String> mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Map<String,String> resultMap = new HashMap<String,String>();    
                    ResultSetMetaData meta = rs.getMetaData();
                    int count = meta.getColumnCount();
                    String columnName = "";
                    String value = "";
                    for(int i=0;i<count;i++) {
                        columnName = meta.getColumnName(i+1);
                        value = rs.getString(columnName);
                        resultMap.put(columnName, value);
                    }       
                    return resultMap;
                }
            });
            return resultList;
        }
    }
    

    context.xml

    <bean id="multipleDataSource" class="MultipleDataSource">
        <property name="defaultTargetDataSource" ref="subjectDataSource"></property>
        <property name="targetDataSources">
            <map>
                <entry key="default" value-ref="dataSource" />
                <entry key="subject" value-ref="subjectDataSource" />
            </map>
        </property>
    </bean>
        
    <!-- JdbcTemplate -->
    <bean id="subject" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="multipleDataSource" />
    </bean>
    

    MutipleDataSourcce.java

    public class MultipleDataSource extends AbstractRoutingDataSource {
        
        /**
         * 默认数据源
         */
        public static final String DEFAULT = "default";
        /**
         * 主题库
         */
        public static final String SUBJECT = "subject";
    
        private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<String>();
    
        public static void setDataSourceKey(String dataSource) {
            dataSourceKey.set(dataSource);
        }
    
        @Override
        protected Object determineCurrentLookupKey() {
            return dataSourceKey.get();
        }
    
        public static void clear(){
            dataSourceKey.remove();
        }
    }
    

    切换数据源

    MultipleDataSource.setDataSourceKey(MultipleDataSource.DEFAULT);
    

    相关文章

      网友评论

          本文标题:spring jdbc

          本文链接:https://www.haomeiwen.com/subject/yafrpktx.html