💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 创建测试数据库和表结构 ```mysql CREATE DATABASE `consumer` CHARACTER SET 'utf8' COLLATE 'utf8_bin'; USE `consumer`; CREATE TABLE `consumer` ( `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 `consumer`.`consumer`(`id`, `name`) VALUES (1, 'consumer-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-consumer</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> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*.xml</include> </includes> </resource> </resources> </build> </project> ``` # 创建主启动类 ```java package com.gosuncn; @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } ``` # 创建配置文件 ```yaml server: port: 8004 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///consumer?serverTimezone=UTC username: root password: root application: name: consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 mybatis: mapper-locations: classpath*:/com/gosuncn/dao/*.xml ``` # 创建实体类 ```java package com.gosuncn.entity; @Data public class Consumer { private Integer id; private String name; } ``` # 创建ConsumerMapper ```java package com.gosuncn.dao; @Mapper public interface ConsumerMapper { List<Consumer> findAll(); int createConsumer(Consumer consumer); } ``` # 创建ConsumerMapper.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.ConsumerMapper"> <select id="findAll" resultType="com.gosuncn.entity.Consumer"> SELECT `id`, `name` FROM `consumer` </select> <insert id="createConsumer" parameterType="com.gosuncn.entity.Consumer"> INSERT INTO `consumer`(`name`) VALUES (#{name}) </insert> </mapper> ``` # 创建ConsumerService ```java package com.gosuncn.service; @Service public class ConsumerService { @Autowired private ConsumerMapper consumerMapper; public Optional<List<Consumer>> findAll() { return Optional.ofNullable(consumerMapper.findAll()); } @Transactional public int createConsumer(Consumer consumer) { return consumerMapper.createConsumer(consumer); } } ``` # 创建ConsumerController ```java package com.gosuncn.controller; @RestController public class ConsumerController { @Autowired private ConsumerService consumerService; @GetMapping("/") public CommonResult findAll() { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), consumerService.findAll().orElse(null)); } @GetMapping("/consumer") public CommonResult createConsumer() { Consumer consumer = new Consumer(); consumer.setName("Hi Boy."); return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), consumerService.createConsumer(consumer)); } } ```