🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 创建测试数据库和表结构 ```mysql CREATE DATABASE `provider` CHARACTER SET 'utf8' COLLATE 'utf8_bin'; USE `provider`; CREATE TABLE `provider` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '名称', PRIMARY KEY (`id`) USING BTREE ); INSERT INTO `provider`.`provider`(`id`, `name`) VALUES (1, 'provider-test'); ``` # 配置pom文件 ```xml <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloudframe</artifactId> <groupId>com.gosuncn</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sys-provider</artifactId> <dependencies> <dependency> <groupId>com.gosuncn</groupId> <artifactId>common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.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> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*.xml</include> </includes> </resource> </resources> </build> </project> ``` # 创建主启动类 ```java @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } ``` # 创建配置文件 ```yaml server: port: 8003 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///provider?serverTimezone=UTC username: root password: root application: name: provider mybatis: mapper-locations: classpath*:/com/gosuncn/dao/*.xml ``` # 创建实体类 ```java package com.gosuncn.entity; @Data public class Provider { private Integer id; private String name; } ``` # 创建ProviderMapper ```java package com.gosuncn.dao; @Mapper public interface ProviderMapper { List<Provider> findAll(); int createProvider(Provider provider); } ``` # 创建ProviderMapper.xml ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.gosuncn.dao.ProviderMapper"> <select id="findAll" resultType="com.gosuncn.entity.Provider"> SELECT `id`, `name` FROM `provider` </select> <insert id="createProvider" parameterType="com.gosuncn.entity.Provider"> INSERT INTO `provider`(`name`) VALUES (#{name}) </insert> </mapper> ``` # 创建ProviderService ```java package com.gosuncn.service; @Service public class ProviderService { @Autowired private ProviderMapper providerMapper; public Optional<List<Provider>> findAll() { return Optional.ofNullable(providerMapper.findAll()); } @Transactional public int createProvider(Provider provider) { return providerMapper.createProvider(provider); } } ``` # 创建ProviderController ```java package com.gosuncn.controller; @RestController public class ProviderController { @Autowired private ProviderService providerService; @GetMapping("/") public CommonResult findAll() { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), providerService.findAll().orElse(null)); } @GetMapping("/provider") public CommonResult createProvider() { Provider provider = new Provider(); provider.setName("Hi Girl."); return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), providerService.createProvider(provider)); } } ```