一、添加POM引用
二、配置连接属性
三、代码
1、Application代码
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
public static void main(String[] args)
{
SpringApplication.run(Application.class);
}
}
2、Mapper代码
3、controller代码
import com.lubansoft.model.Poi;
import com.lubansoft.service.Poi1Service;
import com.lubansoft.service.PoiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RequestMapping("/poi")
@RestController
@Api("PoiController")
public class PoiController {
@Autowired
private PoiService poiService;
@Autowired
private Poi1Service poi1Service;
@ApiOperation("insert")
@PostMapping("/insert")
public String insert(@RequestParam("poi") Poi poi){
poiService.insert(Arrays.asList(poi));
return "插入成功";
}
@ApiOperation("获取全部主数据库")
@GetMapping("/getAll")
public List<Poi> getAll(){
return poiService.getAll();
}
@ApiOperation("获取全部从数据库")
@GetMapping("/getAll1")
public List<Poi> getAll1(){
return poi1Service.getAll();
}
}
4、model
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
@ApiModel("Poi")
@Getter
@Setter
public class Poi {
@ApiModelProperty("id")
private String id;
@ApiModelProperty("poiid")
private String poiid;
@ApiModelProperty("title")
private String title;
@ApiModelProperty("address")
private String address;
@ApiModelProperty("lon")
private String lon;
@ApiModelProperty("lat")
private String lat;
@ApiModelProperty("city")
private String city;
@ApiModelProperty("category_name")
private String category_name;
@ApiModelProperty("checkin_num")
private String checkin_num;
@ApiModelProperty("photo_num")
private String photo_num;
}
5、services
import com.baomidou.dynamic.datasource.annotation.DS; import com.lubansoft.mapper.PoiMapper; import com.lubansoft.model.Poi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @DS("master") @Service public class PoiService { @Autowired PoiMapper poiMapper; public Boolean insert(List<Poi> pois){ poiMapper.insert(pois); return Boolean.TRUE; } public List<Poi> getAll(){ return poiMapper.getAll(); } }
import com.baomidou.dynamic.datasource.annotation.DS; import com.lubansoft.mapper.PoiMapper; import com.lubansoft.model.Poi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @DS("slave") @Service public class Poi1Service { @Autowired PoiMapper poiMapper; public Boolean insert(List<Poi> pois){ poiMapper.insert(pois); return Boolean.TRUE; } public List<Poi> getAll(){ return poiMapper.getAll(); } }
6、swagger
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { Boolean swaggerEnabled=true; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_12).apiInfo(apiInfo()) // 是否开启 .enable(swaggerEnabled).select() // 扫描的路径包 .apis(RequestHandlerSelectors.basePackage("com.test.controller")) // 指定路径处理PathSelectors.any()代表所有的路径 .paths(PathSelectors.any()).build().pathMapping("/"); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot-Swagger3集成和使用-demo示例") .description("springboot | swagger") // 作者信息 .contact(new Contact("name", "个人主页url", "email")) .version("1.0.0") .build(); } }