效果图
代码
1.打开idea设置,在Plugins中安装lombok
注:我之前安装过
2. 在pom.xml文件将需要用到的依赖mybatis、mysql、热部署引入更新
注:右边又一个m点击即可更新
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.cqtc</groupId>
<artifactId>eshop_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>eshop</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 工程主入口 -->
<mainClass>cn.eshop.EshopApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>C:/Program Files/Java/jdk1.8.0_211/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. 在src——main——java目录下创建包cn.eshop,再创建①表现层包controller、②实体层包domain、③接口包mapper
4.创建一个数据库和表,并在里面写好内容
5.在resources文件下,创建一个application.yml
#端口号
server:
port: 8081
#数据库连接信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: 12345678
6.在domain文件中创建一个实体类Category
注:用驼峰命名法
package cn.eshop.domain;
import lombok.Data;
@Data
public class Category {
private int catId;
private String catName;
private int catPid;
private int catLevel;
private int catDeleted;
private String catIcon;
private String catSrc;
}
7.在mapper文件中创建接口文件CategoryMapper
package cn.eshop.mapper;
import java.util.List;
import cn.eshop.domain.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CategoryMapper {
@Select("select cat_id,cat_name,cat_pid,cat_icon from sp_category")
@Results({
@Result(column="cat_id",property="catId"),
@Result(column="cat_name",property="catName"),
@Result(column="cat_pid",property="catPid"),
@Result(column="cat_icon",property="catIcon")
})
public List<Category> getCatgoryData();
}
8.在comtroller文件夹中创建一个HelloController类
package cn.eshop.controller;
import cn.eshop.domain.Category;
import cn.eshop.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
@Autowired
private CategoryMapper cgm;
/**
* 分类页面接口
*/
@RequestMapping("/category")
@CrossOrigin(allowedHeaders = "*")
public List<Category> getCategory(){
return cgm.getCatgoryData();
}
}