• Springboot MybatisPlus整合多数据源


    一、pom文件

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>multiple-data-sources</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>multiple-data-sources</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- mybatisplus 生成工具 -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.4.0</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.2</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.72</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.3.7.RELEASE</version>
                    <configuration>
                        <mainClass>com.example.multipledatasources.MultipleDataSourcesApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    二、创建数据表

    为了演示方便,这里创建三个数据库,数据库中表创建一样的。

    1、数据库如下图自行创建

    2、创建表

    CREATE TABLE `ht_student` (
      `id` int NOT NULL AUTO_INCREMENT,
      `name` varchar(255) DEFAULT NULL,
      `age` int NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、多数据源DataSource配置类

    咱们配置了三个数据源,所以配置三个类

    1、数据源一:MysqlDataSourceConfig主数据源

    
    
    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * 数据源一配置文件
     */
    @Configuration
    @MapperScan(basePackages = "com.example.orm.mysql1.mapper", sqlSessionFactoryRef = "sqlSessionFactory1")
    public class MysqlDataSourceConfig {
        @Primary
        @Bean(name = "mysqlDataSource1")
        @ConfigurationProperties(prefix = "spring.datasource.mysql")
        public DataSource dataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Primary
        @Bean(name = "sqlSessionFactory1")
        public SqlSessionFactory sqlSessionFactory1(@Qualifier("mysqlDataSource1") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql1/*Mapper.xml"));
            //驼峰命名
            factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            //輸出日志
            factoryBean.getObject().getConfiguration().setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
            return factoryBean.getObject();
        }
    
        @Primary
        @Bean(name = "mysqlTransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource1") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "mysqlSqlSessionTemplate1")
        @Primary
        public SqlSessionTemplate testSqlSessionTemplate(
                @Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory1) {
            return new SqlSessionTemplate(sqlSessionFactory1);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2、数据源二:MysqlDataSource2Config

    
    
    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * 数据源二配置文件
     */
    @Configuration
    @MapperScan(basePackages = "com.example.orm.mysql2.mapper", sqlSessionFactoryRef = "sqlSessionFactory2")
    public class MysqlDataSource2Config {
    
        @Bean(name = "mysqlDataSource2")
        @ConfigurationProperties(prefix = "spring.datasource.mysql.v1")
        public DataSource dataSource() {
            return DataSourceBuilder.create().build();
        }
    
    
        @Bean(name = "sqlSessionFactory2")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource2") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql2/*Mapper.xml"));
            //驼峰命名
            factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            //輸出日志
            factoryBean.getObject().getConfiguration().setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
            return factoryBean.getObject();
        }
    
    
        @Bean(name = "mysqlTransactionManager2")
        public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource2") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "mysqlSqlSessionTemplate2")
        public SqlSessionTemplate testSqlSessionTemplate(
                @Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    3、数据源三:MysqlDataSource3Config

    
    
    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * 数据源三配置文件
     **/
    @Configuration
    @MapperScan(basePackages = "com.example.orm.mysql3.mapper", sqlSessionFactoryRef = "sqlSessionFactory3")
    public class MysqlDataSource3Config {
    
        @Bean(name = "mysqlDataSource3")
        @ConfigurationProperties(prefix = "spring.datasource.mysql.v2")
        public DataSource dataSource() {
            return DataSourceBuilder.create().build();
        }
    
    
        @Bean(name = "sqlSessionFactory3")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource3") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            factoryBean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql3/*Mapper.xml"));
            //驼峰命名
            factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            //輸出日志
            factoryBean.getObject().getConfiguration().setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
            return factoryBean.getObject();
        }
    
    
        @Bean(name = "mysqlTransactionManager3")
        public DataSourceTransactionManager transactionManager(@Qualifier("mysqlDataSource3") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "mysqlSqlSessionTemplate3")
        public SqlSessionTemplate testSqlSessionTemplate(
                @Qualifier("sqlSessionFactory3") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    四、MybatisPlusGeneratorTest根据数据表生成实体类、mapper、xml

    代码里面有详细的注解,可自行配置,配置完成后直接运行即可。

    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
    import com.baomidou.mybatisplus.generator.config.GlobalConfig;
    import com.baomidou.mybatisplus.generator.config.PackageConfig;
    import com.baomidou.mybatisplus.generator.config.StrategyConfig;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    
    
    public class MybatisPlusGeneratorTest {
    
        public static void main(String[] args) {
    
            AutoGenerator autoGenerator = new AutoGenerator();
    
            //全局配置
            GlobalConfig gc = new GlobalConfig();
            String oPath = System.getProperty("user.dir");//得到当前项目的路径
            gc.setOutputDir(oPath + "/multiple-data-sources/src/main/java/");   //生成文件输出根目录
            gc.setOpen(false);//生成完成后不弹出文件框
            gc.setFileOverride(true);  //文件覆盖
            gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
            gc.setEnableCache(false);// XML 二级缓存
            gc.setBaseResultMap(true);// XML ResultMap
            gc.setSwagger2(true); //实体属性 Swagger2 注解
    
            // 自定义文件命名,注意 %s 会自动填充表实体属性!
            gc.setControllerName("%sController");
            gc.setServiceName("%sApi");
            gc.setServiceImplName("%sService");
            gc.setMapperName("%sMapper");
            gc.setXmlName("%sMapper");
            autoGenerator.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setDbType(DbType.MYSQL);   //设置数据库类型
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123456");
            dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");  //指定数据库
            autoGenerator.setDataSource(dsc);
    
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);      // 表名生成策略
    
            strategy.setInclude(new String[]{"ht_student"});     // 需要生成的表
    
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
    
            autoGenerator.setStrategy(strategy);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent("com.example.generate");
            pc.setService("service");
            pc.setServiceImpl("service.impl");
            pc.setMapper("mapper");
            pc.setEntity("entity");
            pc.setXml("xml");
            autoGenerator.setPackageInfo(pc);
    
            // 执行生成
            autoGenerator.execute();
    
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    五、实体类entity

    因为表都一样,这里拿一个实体类举例,其他复制即可。

    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    @TableName(value = "ht_student")
    public class HtStudentV1 implements Serializable {
    
        @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
    
        private String name;
    
        private Integer age;
    
    }  
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    六、mapper层

    也拿一个举例,其他复制即可。

    
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.example.orm.mysql1.entity.HtStudentV1;
    
    public interface HtStudentV1Mapper extends BaseMapper {
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    七、dao/service层

    是对CURD的扩展

    
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.example.orm.mysql1.entity.HtStudentV1;
    import com.example.orm.mysql1.mapper.HtStudentV1Mapper;
    import org.springframework.stereotype.Service;
    
    
    @Service
    public class HtStudentV1Service extends ServiceImpl {
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    八、XML

    拿一个举例子

    
    
    
    
    
        
        
            
            
            
        
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    九、接口层

    import com.alibaba.fastjson.JSONObject;
    import com.example.common.Response;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/multiple")
    public interface TestMultipleDataSourcesApi {
    
        @GetMapping(path = "/test")
        Response test();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    十、业务层

    import com.alibaba.fastjson.JSONObject;
    import com.example.api.TestMultipleDataSourcesApi;
    import com.example.common.Response;
    import com.example.orm.mysql1.dao.HtStudentV1Service;
    import com.example.orm.mysql1.entity.HtStudentV1;
    import com.example.orm.mysql2.dao.HtStudentV2Service;
    import com.example.orm.mysql2.entity.HtStudentV2;
    import com.example.orm.mysql3.dao.HtStudentV3Service;
    import com.example.orm.mysql3.entity.HtStudentV3;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.Random;
    
    @Service
    public class TestMultipleDataSourcesService implements TestMultipleDataSourcesApi {
    
        @Autowired
        private HtStudentV1Service htStudentV1Service;
    
        @Autowired
        private HtStudentV2Service htStudentV2Service;
    
        @Autowired
        private HtStudentV3Service htStudentV3Service;
    
        @Override
        public Response test() {
    
            Random rand = new Random();
    
            HtStudentV1 htStudentV1 = new HtStudentV1();
            htStudentV1.setName("小明" + rand.nextInt(100));
            htStudentV1.setAge(rand.nextInt(30));
    
            HtStudentV2 htStudentV2 = new HtStudentV2();
            htStudentV2.setName("小明" + rand.nextInt(100));
            htStudentV2.setAge(rand.nextInt(30));
    
            HtStudentV3 htStudentV3 = new HtStudentV3();
            htStudentV3.setName("小明" + rand.nextInt(100));
            htStudentV3.setAge(rand.nextInt(30));
    
            htStudentV1Service.getBaseMapper().insert(htStudentV1);
            htStudentV2Service.getBaseMapper().insert(htStudentV2);
            htStudentV3Service.getBaseMapper().insert(htStudentV3);
    
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("htStudentV1", htStudentV1Service.list());
            jsonObject.put("htStudentV2", htStudentV2Service.list());
            jsonObject.put("htStudentV3", htStudentV3Service.list());
            return Response.ok(jsonObject);
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    十一、Response返回体

    
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    public class Response implements Serializable {
    
        private static final long serialVersionUID = 1L;
        protected T data;
        protected int code;
        protected String message;
    
        public Response() {
        }
    
        public Response(T data) {
            this.data = data;
        }
    
        public Response(T data, int code, String message) {
            this.data = data;
            this.code = code;
            this.message = message;
        }
    
        public static  Response ok(T data) {
            return new Response(data, 200, "成功");
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    十二、启动项目并测试

    地址栏输入:http://localhost:8080/multiple/test
    每调用一次,就会在三个库的三个表中插入一条数据

    返回结果如下:

    
    {
    	"data": {
    		"htStudentV1": [{
    			"id": 1,
    			"name": "39.68371366230235姓名",
    			"age": 10
    		}, {
    			"id": 2,
    			"name": "小明90",
    			"age": 24
    		}],
    		"htStudentV2": [{
    			"id": 1,
    			"name": "29.84551559999581姓名",
    			"age": 10
    		}, {
    			"id": 2,
    			"name": "小明24",
    			"age": 24
    		}],
    		"htStudentV3": [{
    			"id": 1,
    			"name": "66.13283505246086姓名",
    			"age": 10
    		}, {
    			"id": 2,
    			"name": "小明63",
    			"age": 23
    		}]
    	},
    	"code": 200,
    	"message": "成功"
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

  • 相关阅读:
    Kubernetes
    STM32与ZigBee无线通信技术在工业自动化中的应用
    (阿里云)Linux部署springboot项目全过程
    大龄程序员三战考研变身考研战神
    Python图像处理丨基于OpenCV和像素处理的图像灰度化处理
    可变形注意力转换器综述
    Django实现音乐网站 ⒇
    【算法】高精度减法(大数减法)
    思科Cisco分公司内网互通
    把地毯图片放在地板图片上,并去掉地毯两个角用地板填充
  • 原文地址:https://blog.csdn.net/weixin_43969830/article/details/126990465