UReport2 integrates SpringBoot-configuration data source

UReport2 integrates SpringBoot-configure data source
1. Built-in data source
2. Directly connected data source
3. SpringBean mode data source
3.1 Create Spring Bean
3.2 Configure data source
3.2.1 Configure data source whose return object is a Map collection
3.2.2 Configure return object Data source for POJO collection

Ureport2 configures the data source in the following three ways:


1. Built-in data source


Create a configuration class to implement the BuildinDatasource interface

@EnableAutoConfiguration
@Configuration
public class UReportConfig implements BuildinDatasource {

    @Resource
    DataSource dataSource;

    /**
     * 返回数据源的名称
     * @return
     */
    @Override
    public String name() {
        return "内置数据源";
    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据源失败!");
        }
        return null;
    }
}

 After starting the project, as shown in the figure below, the configuration is successful

insert image description here

 

2. Direct connection to data source

Configure as shown below

insert image description here

Data source name: define by yourself, write whatever you want, as long as you can distinguish it.
Connection user name: database user name
Connection password: database password
Driver name: consistent with the configuration file
Connection URL: consistent with the configuration file
Click "test connection" to display the connection If successful, the configuration is successful

3. SpringBean way data source


In the Spring bean dataset configuration, we can click the "Select Method" button on the right for the method name to select the method defined in the class corresponding to the current Bean, but the requirements for the method here are: the method must have three parameters, in order String, String, Map, in turn corresponding to the data source name, data set name and external parameter Map

3.1 Create Spring Bean

@Component
public class DataSourceBean {

    /**
     * 第一种:返回Map集合
     * @param dsName
     * @param dataSetName
     * @param parameters
     * @return
     */
    public List<Map<String, Object>> loadReportData(String dsName, String dataSetName, Map<String, Object> parameters) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> m1 = new HashMap<String, Object>();
        m1.put("year", "2000");
        m1.put("month", "1");
        m1.put("amount", 72847);
        list.add(m1);

        Map<String, Object> m2 = new HashMap<String, Object>();
        m2.put("year", "2000");
        m2.put("month", "2");
        m2.put("amount", 28429);
        list.add(m2);
        return list;
    }

    /**
     * 第二种:返回对象集合
     * @param dsName
     * @param datasetName
     * @param parameters
     * @return
     */
    public List<UserVo> loadData(String dsName, String datasetName, Map<String, Object> parameters) {
        List<UserVo> list = new ArrayList<UserVo>();
        for (int i = 0; i < 10; i++) {
            UserVo m = new UserVo();
            m.setId(String.valueOf(i));
            m.setUserNode(RandomStringUtils.random(10, true, false));
            m.setUserName("name-"+i);
            m.setSex((i%2)+"");
            list.add(m);
        }
        return list;
    }
}

3.2 Configure data source

insert image description here

 

Click "Save" to complete the Bean ID configuration
Data source name: Define your own
Bean ID: The name of the created Bean

3.2.1 Configure the data source whose return object is a Map collection


Right-click the data source name, select "Add Dataset", and configure the dataset, as shown in the figure below

insert image description here

 

Dataset name: Define your own
Method name: The method name in the Bean
Return object: Specify the full name of the method return class for generating fields, if not specified, you need to add fields manually

Click "OK" to complete the data set configuration.
We do not specify the return object here, choose to manually add fields, right-click on Dataset 1 , select Add Field , and enter the key of the Map in the return class. After the addition is completed, the result is shown in the figure belowinsert image description here

 

Fill the fields into the form to test whether the data set configuration is successful, as shown in the figure below, the configuration is successfulinsert image description hereinsert image description here

3.2.2 Configure the data source whose return object is a POJO collection


The previous configurations are the same, but the configuration of the returned object is different. If the configuration is correct and the data set is refreshed, the properties of the object will be displayed automatically, as shown in the figure below

insert image description here

insert image description here 

 

 Fill the fields into the form to test whether the data set configuration is successful, as shown in the figure below, the configuration is successful

insert image description here

 

 insert image description here

Guess you like

Origin blog.csdn.net/qq_22905801/article/details/130678734