Mooc項目開發筆記(二):講師模塊配置、代碼生成、框架運行測試

一、講師管理模塊配置

1、在service下面service-edu模塊中創建配置文件

將resources目錄設置爲resources目錄類型
在這裏插入圖片描述
將java目錄設置爲source目錄類型

在這裏插入圖片描述

resources目錄下創建文件 application.properties

# 服務端口
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

2、創建MP代碼生成器

在test/java目錄下創建包cn.hanzhuang42.service_edu,創建代碼生成器:CodeGenerator.java

public class CodeGenerator {

    @Test
    public void main1() {

        // 1、創建代碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        gc.setOutputDir("C:\\Users\\韓壯\\IdeaProjects\\mooc\\service\\service_edu" + "/src/main/java");
        gc.setAuthor("Miracle42");
        gc.setOpen(false); //生成後是否打開資源管理器
        gc.setFileOverride(false); //重新生成時文件是否覆蓋
        /*
         * mp生成service層代碼,默認接口名稱第一個字母有 I
         * UcenterService
         * */
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setIdType(IdType.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/mooc?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("webuser");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        //最後生成的包名爲 cn.hanzhuang42.eduservice.*
        pc.setParent("cn.hanzhuang42");
        pc.setModuleName("eduservice"); //模塊名
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、逆向工程(表->實體類)策略配置
        StrategyConfig strategy = new StrategyConfig();
        //需要逆向工程的表
        //多張表 strategy.setInclude("edu_teacher","biao2","baio3",...);
        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();
    }
}

運行之後就會在service_edu模塊的java目錄下生成下面內容:

在這裏插入圖片描述

二、編寫後臺管理api接口

1、編寫controller代碼

@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    EduTeacherService teacherService;

    @GetMapping("findAll")
    public List<EduTeacher> finaAll() {
        //調用service的方法是實現查詢所有的操作
        List<EduTeacher> list = teacherService.list(null);
        return list;
    }
    
}

2、創建SpringBoot配置類

在edu包下創建config包,創建EduConfig.java

package cn.hanzhuang42.eduservice.config;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("cn.hanzhuang42.eduservice.mapper")
public class EduConfig {

}

3、創建SpringBoot啓動類

創建啓動類 EduApplication.java,注意啓動類的創建位置

@SpringBootApplication
public class EduApplication {

    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class, args);
    }

}

4、統一返回的json時間格式

默認情況下json時間格式帶有時區,並且是世界標準時間,和我們的時間差了八個小時

所以在application.properties中設置

#返回json的全局時間格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

5、運行啓動類

訪問http://localhost:8080/eduservice/teacher

得到json數據

在這裏插入圖片描述

三、講師邏輯刪除功能

1、配置邏輯刪除插件

EduConfig中配置

/**
 * 邏輯刪除插件
 */
@Bean
public ISqlInjector sqlInjector() {
    return new LogicSqlInjector();
}

2、添加邏輯刪除標記註解

在實體類的邏輯刪除標記屬性上添加@TableLogic註解

@ApiModelProperty(value = "邏輯刪除 1(true)已刪除, 0(false)未刪除")
@TableLogic
private Boolean isDeleted;

3、EduTeacherController添加刪除方法

    //邏輯刪除講師
    @DeleteMapping("{id}")
    public boolean deleteById(@PathVariable("id") String id) {
        boolean success = teacherService.removeById(id);
        return success;
    }

4、使用postman測試刪除

在這裏插入圖片描述
返回true,查看數據庫中可以看到is_deleted的值已經更改

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章