项目结构如下:
说明:
config:此目录是swagger配置的扫描路径之类的信息。
entity:此目录是实体类的存放地址。
mapper:此目录存放连接sql层的最后一步。
service:此目录主要是进行业务处理。
web:显而易见,入口层。
以此类推如下:
application.properties:数据库配置层。
mybatis.properties:mybatis配置扫描层。
Swagger内容如下展示:
@Configuration
@EnableSwagger2
@ComponentScan("com.demo.web")
public class Swagger {
@Bean
public Docket createRestApi(){
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder().title("Spring Boot 整合 swagger ui")
.description("Spring Boot 整合 swagger ui 和spring-data-JPA")
.version("1.0")
.build();
return apiInfo;
}
}
pom.xml文件内容如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.6 com.demo springboot_mybatis 0.0.1-SNAPSHOT springboot_mybatis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test com.aLibaba druid 1.1.10 mysql mysql-connector-java runtime log4j log4j 1.2.12 io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 io.swagger swagger-annotations 1.5.19 dom4j dom4j 1.1 org.springframework.batch spring-batch-core 4.1.1.RELEASE dom4j dom4j 1.1 org.apache.xmlbeans xmlbeans 2.6.0 xml-apis xml-apis 1.4.01 org.projectlombok lombok 1.18.4 src/main/java ***.properties org.springframework.boot spring-boot-maven-plugin
application.properties文件:
#数据源配置 spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/*****?useUnicode=true&characterEncoding=utf8 spring.datasource.username = **** spring.datasource.password = ****
mybatis.properties文件:
#mybatis 配置 mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:com/demo/mapper/xml/*.xml mybatis.type-aliases-package=com.demo
启动类如下:
@SpringBootApplication
@MapperScan(value = "com.demo.mapper") //此处是扫描mapper的路径
@PropertySource({"classpath:mybatis.properties",
"classpath:application.properties"})// 读取.properties 文件路径
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
实体类(MFnnclChange)如下:
@ApiModel("实体类")
@Data
public class MFnnclChange implements Serializable {
@ApiModelProperty("主键")
private Long id;
@JsonFormat(pattern="yyyy-MM-dd")
private Date applyYmd;
private Long newBrnchCd;
}
controller层代码(只是一个简单的全查操作):
@RestController
@Api("数据一览查询")
@RequestMapping("/mfnnclChange")
public class MFnnclChangeController {
@Autowired
private MFnnclChangeService mFnnclChangeService;
@ApiOperation("获取数据一览")
@GetMapping(value = "/list")
public List getMcorpList(){
return mFnnclChangeService.findAll();
}
}
service层:
@Service
public interface MFnnclChangeService {
//数据一览查询
public List findAll();
}
impl层:
@Service
public class MFnnclChangeImpl implements MFnnclChangeService{
@Autowired
private MFnnclChangeMapper mFnnclChangeMapper;
//一览查询
@Override
public List findAll(){
return mFnnclChangeMapper.findAll();
}
}
mapper层:
public interface MFnnclChangeMapper {
//一览查询
List findAll();
}
xml代码:
id, apply_ymd, new_brnch_cd SELECT FROM m_fnncl_change
最终启动后端程序,我们在页面访问
http://localhost:8080/swagger-ui.html
可得到以下画面:
点击其中的进行查询可得到以下结果集:
至此,此项目基础搭建和运行完了…



