Mybatis Plus代码自动生成工具类与Service,Mapper层写法规范

Mybatis Plus代码自动生成与Service,Mapper层写法规范


使用自动生成器先引入依赖

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-generatorartifactId>
    <version>3.3.1.tmpversion>
dependency>
<dependency>
    <groupId>org.apache.velocitygroupId>
    <artifactId>velocity-engine-coreartifactId>
    <version>2.0version>
dependency>
<dependency>
    <groupId>io.springfoxgroupId>
    <artifactId>springfox-swagger2artifactId>
    <version>2.6.1version>
dependency>

<dependency>
    <groupId>io.springfoxgroupId>
    <artifactId>springfox-swagger-uiartifactId>
    <version>2.6.1version>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.20version>
dependency>
<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.3.1.tmpversion>
dependency>

在建立好数据库后,我们可以使用Mybatis Plus提供的自动代码生成器,将其写成一个工具类,当有表新增时,可以及时调用该工具类生成

public class GenerateTableUtil {
    public static void generate(String parent,String moduleName,String ...table){

        AutoGenerator mpg = new AutoGenerator();

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        // 输出目录
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("zhou");
        gc.setServiceName("%sService");
        gc.setFileOverride(true);
        gc.setOpen(false);
//        gc.setServiceName("%Service");
        gc.setSwagger2(true);  //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUsername("root");
        dsc.setPassword("david1");
        dsc.setUrl("jdbc:mysql://49.235.45.85:3306/db1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 3. 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent(parent);
        pc.setEntity("entiry");
        pc.setController("controller");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 4.策略设置
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 重点,设置映射的表名
        strategy.setInclude(table);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
//        // 设置逻辑删除字段
//        strategy.setLogicDeleteFieldName("deleted");
//        // 自动填充字段
//        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
//        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//        strategy.setTableFillList(List.of(createTime,updateTime));
//        // 乐观锁字段
//        strategy.setVersionFieldName("version");
//        strategy.setRestControllerStyle(true); // Controller用rest风格
//        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setRestControllerStyle(true); // Controller用rest风格
//        strategy.setControllerMappingHyphenStyle(true);

        mpg.setStrategy(strategy);

        mpg.execute();

    }

}

工具类生成后,可以获得如下的目录,但是怎么编写Service类是一个问题,下面给出一个方案

  1. 在Service层的接口写出需要使用的方法
public interface DepartmentService extends IService<Department> {

    void addDepartment(Department department);

    void deleteDepartmentById(int id);

    void updateDepartmentById(Department department);

    Department selectDepartmentById(int id);
}
  1. 之后使用其应用Impl类重写这些方法,我们可以使用Mybatis Plus提供的baseMapper使用Mybatis Plus提供的方法
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public void addDepartment(Department department) {
        baseMapper.insert(department);
    }

    @Override
    public void deleteDepartmentById(int id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void updateDepartmentById(Department department) {
        baseMapper.updateById(department);
    }

    @Override
    public Department selectDepartmentById(int id) {
        return baseMapper.selectById(id);
    }
}
  1. 在需要的地方进行注入
@SpringBootTest
class RedisDemoApplicationTests {

    @Autowired
    DepartmentServiceImpl departmentService;

    @Test
    void contextLoads() throws JsonProcessingException {
        departmentService.addDepartment(new Department(4,"LOL游戏部"));
        departmentService.deleteDepartmentById(4);
        departmentService.updateDepartmentById(new Department(3,"产品部"));
    }
}

至此,我们便可以以Service层方式调用数据库方法,形成一个使用规范

你可能感兴趣的:(Mybatis Plus代码自动生成工具类与Service,Mapper层写法规范)