谷粒学院-3-讲师模块crud
一、数据库部分
创建一个谷粒学院的数据库,gulicollege
进入该数据库
执行脚本创建讲师crud表
CREATE TABLE `edu_teacher` (
`id` CHAR(19) NOT NULL COMMENT '讲师ID',
`name` VARCHAR(20) NOT NULL COMMENT '讲师姓名',
`intro` VARCHAR(500) NOT NULL DEFAULT '' COMMENT '讲师简介',
`career` VARCHAR(500) DEFAULT NULL COMMENT '讲师资历,一句话说明讲师',
`level` INT(10) UNSIGNED NOT NULL COMMENT '头衔 1高级讲师 2首席讲师',
`avatar` VARCHAR(255) DEFAULT NULL COMMENT '讲师头像',
`sort` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '排序',
`is_deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '逻辑删除 1(true)已删除, 0(false)未删除',
`gmt_create` DATETIME NOT NULL COMMENT '创建时间',
`gmt_modified` DATETIME NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='讲师';
结果:
二、工程结构和创建
采用父子工程结构:
- 父工程(springboot工程,pom类型管理依赖版本和放公共依赖)
- 子模块(maven工程,比如讲师模块)
- 不一定存在的子模块(maven工程)
- 子模块(maven工程,比如讲师模块)
项目对应的工程结构目录:
- guli-parent:在线教学根目录(父工程),管理四个子模块:
- canal-client:canal数据库表同步模块(统计同步数据)
- common:公共模块父节点
- common-util:工具类模块,所有模块都可以依赖于它
- service-base:service服务的base包,包含service服务的公共配置类,所有service模块依赖于它
- spring-security:认证与授权模块,需要认证授权的service服务依赖于它
- infrastructure:基础服务模块父节点
- api-gateway:api网关服务
- service:api接口服务父节点
- service-acl:用户权限管理api接口服务(用户管理、角色管理和权限管理等)
- service-cms:cms api接口服务
- service-edu:教学相关api接口服务
- service-msm:短信api接口服务
- service-order:订单相关api接口服务
- service-oss:阿里云oss api接口服务
- service-statistics:统计报表api接口服务
- service-ucenter:会员api接口服务
- service-vod:视频点播api接口服务
1、创建父工程
流程:
主要分为两步:创工程和改pom.xml配置文件
项目名
初始依赖(不选,版本随便选一个,后期在pom文件修改)
删掉src目录
以下修改父工程pom文件
修改springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.5.3</version>-->
<!--选择课程对应版本-->
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
添加packaging类型为pom类型
<artifactId>guli-parent</artifactId>
<packaging>pom</packaging>
在父工程中只存放公共依赖和依赖的版本管理,
所以需要先把全部依赖都清除
然后修改properties标签内容
<properties>
<java.version>1.8</java.version>
<guli.version>0.0.1-SNAPSHOT</guli.version>
<mybatis-plus.version>3.0.5</mybatis-plus.version>
<velocity.version>2.0</velocity.version>
<swagger.version>2.7.0</swagger.version>
<aliyun.oss.version>2.8.3</aliyun.oss.version>
<jodatime.version>2.10.1</jodatime.version>
<poi.version>3.17</poi.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>2.6</commons-io.version>
<httpclient.version>4.5.1</httpclient.version>
<jwt.version>0.7.0</jwt.version>
<aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
<aliyun-sdk-oss.version>3.1.0</aliyun-sdk-oss.version>
<aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
<aliyun-java-vod-upload.version>1.4.11</aliyun-java-vod-upload.version>
<aliyun-sdk-vod-upload.version>1.4.11</aliyun-sdk-vod-upload.version>
<fastjson.version>1.2.28</fastjson.version>
<gson.version>2.8.2</gson.version>
<json.version>20170516</json.version>
<commons-dbutils.version>1.7</commons-dbutils.version>
<canal.client.version>1.1.0</canal.client.version>
<docker.image.prefix>zx</docker.image.prefix>
<cloud-alibaba.version>0.2.2.RELEASE</cloud-alibaba.version>
</properties>
添加对应这个版本参数的依赖管理
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun.oss.version}</version>
</dependency>
<!--日期时间工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!--commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!--aliyun-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-java-sdk-core.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun-sdk-oss.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>${aliyun-java-sdk-vod.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-vod-upload</artifactId>
<version>${aliyun-java-vod-upload.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>${aliyun-sdk-vod-upload.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>${canal.client.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
依赖报错解决:
aliyun-sdk-vod-upload引入报错解决办法!!!
打开链接,去阿里云官网下载:https://help.aliyun.com/document_detail/51992.html?spm=a2c4g.11186623.6.1029.2dab6cecZfMGvO
下载得到对应jar包
执行命令行
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-java-vod-upload -Dversion=1.4.13 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.13.jar
即把这个包下载到maven仓库中,版本为1.4.13
2、创建子模块
创建service子模块(一个maven的空白项目,类型为pom类型)
流程:
创建maven非模板项目,更改为pom类型
<packaging>pom</packaging>
删除src目录
导入依赖
<dependencies>
<!--这些依赖暂时还不需要,开始时加上可能会报错-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<!-- </dependency>-->
<!-- <!–hystrix依赖,主要是用 @HystrixCommand –>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>-->
<!-- </dependency>-->
<!-- <!–服务注册–>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </dependency>-->
<!-- <!–服务调用–>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!--commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!--gson-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3、创建子子模块
注意着仍然是一个maven工程,但在service模块的web-starter影响下,这同时也是一个web项目
流程:
创建maven工程,叫service-edu
三、service_edu子模块初创
流程:
(1)配置参数
在resource文件夹下创建application.properties文件夹
注意:如果springboot标志性logo没有出现,说明项目创建错误,需要删除再重新创建一次,大概率就好了
# 服务端口
server.port=8001
# 服务名
spring.application.name=service-edu
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT-8
(2)使用代码生成器
加入自动生成代码类
package com.wang.service_edu;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
public class CodeGenerator {
@Test
public void run() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("testjava");
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
gc.setServiceName("%sService"); //去掉Service接口的首字母I,因为不加的话会自动在每个类前加上一个I,如IUserService
gc.setIdType(IdType.ID_WORKER); //主键策略,如果id用了long类型就是ID_WORKER,如果时string类型就是ID_WORKER——STR
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//开启Swagger2模式
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/gulicollege?serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("eduservice"); //模块名
pc.setParent("com.wang");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("edu_teacher"); //可以多个表,用逗号隔开
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}
}
结果:
完善自动生成代码
需要加入数据源datasource配置类扫描mapper接口
@Configuration
@MapperScan("com.wang.eduservice.mapper")
public class EduDataSourceConfig {
}
创建时间和更新时间自动填充
pojo类中
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = "更新时间")
//填充方法中既可以用于insert方法也可以用于update方法
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
metaObjectConfig
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("gmtCreate",new Date(),null);
this.setFieldValByName("gmtModified",new Date(),null);
//逻辑删除数据库字段创建时就自动填入0
this.setFieldValByName("isDeleted",0,null);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("gmtModified",new Date(),null);
}
}
(3)令子模块可运行
先了解一下mp对我们service的封装:
类似于mapper接口,在继承了一个mp对象的时候就会自动封装,我们后来只需要调用即可
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
}
开始写接口
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
@Autowired
EduTeacherService eduTeacherService;
@GetMapping("fillAll")
public List<EduTeacher> fillAllTeachers(){
//暂时先这样写
List<EduTeacher> list = eduTeacherService.list(null);
return list;
}
}
创建启动类,注意是要在该子模块的主包即com.wang.eduservice包下创建
@SpringBootApplication
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class,args);
}
}
运行即可
(4)讲师删除
删除功能
需要先配置好逻辑删除插件和自动填充插件,并在pojp类上的id_deleted字段上添加对应注释
然后再写接口
@DeleteMapping("{id}")
public boolean removeById(@PathVariable("id") String id){
return eduTeacherService.removeById(id);
}
(5)解决跨域问题
在Controller类上添加注解@CrossOrigin即可
(6)postman测试
接下来我们需要配置swagger,需要一个公共模块去配置swagger,然后再次测试
三、创建commons子模块
1、流程:
这部分主要在common中配置swagger环境
创建common子模块,maven工程
pom文件修改添加<packaging>pom</packaging>
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided </scope>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>provided </scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>-->
</dependencies>
然后在下面又再创建子子模块common-base
建包com.wang.servicebase
直接在下面创建swaggerConfig类
package com.wang.servicebase;
import com.google.common.base.Predicates;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("WebApi") //api组的名字
.apiInfo(webApiInfo()) //文档基本呢信息
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*"))) //只要不是这两个文件夹的下的controller都会建立swagger文档
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.description("为人师开发文档")
.contact(
new Contact("江南一点南","https://www.cnblogs.com/CoderWangEx/","1847193435@qq.com")
).version("v1.0")
.title("API测试文档")
.license("Apache2.0")
.licenseUrl("http://www.apache.org/license/LICENSE-2.0")
.build();
}
}
创建完毕
2、在其他模块中中引入commom_base的依赖和文件
以下以service_edu为例
在pom文件中导入service_base的依赖,这样就会把service_base中的依赖和创建的文件全部导入到对应模块中
像这样
<dependency>
<groupId>com.wang</groupId>
<artifactId>common_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
但注意:导入了我们在common_base中写好的swagger配置文件,但是这个文件没有导入到IOC容器中
所以添加一个包的扫描规则
@SpringBootApplication
@ComponentScan({"com.wang"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class,args);
}
}
重新启动
访问:http://localhost:8001/swagger-ui.html
3、完善讲师模块的swagger部分
流程:
给pojo类的相关类上添加@ApiModelProper注解
@ApiModelProperty(value = "创建时间",example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = "更新时间",example = "2019-01-01 8:00:00")
//填充方法中既可以用于insert方法也可以用于update方法
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
控制器也需要修改
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {
@Autowired
EduTeacherService eduTeacherService;
@ApiOperation(value = "查看全部讲师",notes = "查看全部讲师")
@GetMapping("findAll")
public List<EduTeacher> fillAllTeachers(){
List<EduTeacher> list = eduTeacherService.list(null);
return list;
}
@ApiOperation(value = "删除讲师",notes = "根据id删除讲师")
@ApiImplicitParam(paramType = "path",name = "id",value = "讲师id",required = true)
@DeleteMapping("{id}")
public boolean removeById(@PathVariable("id") String id){
return eduTeacherService.removeById(id);
}
}
测试: