# 代码生成问题

# Windows系统生成代码报错

character to be escaped is missing

Exception in thread "main" java.lang.IllegalArgumentException: character to be escaped is missing
	at java.util.regex.Matcher.appendReplacement(Matcher.java:809)
	at java.util.regex.Matcher.replaceAll(Matcher.java:955)
	at java.lang.String.replaceAll(String.java:2223)
	at io.geekidea.springbootplus.generator.CodeGenerator.init(CodeGenerator.java:86)
	at io.geekidea.springbootplus.generator.CodeGenerator.generator(CodeGenerator.java:111)
	at io.geekidea.springbootplus.generator.CodeGenerator.generator(CodeGenerator.java:72)
	at io.geekidea.springbootplus.generator.SpringBootPlusGenerator.main(SpringBootPlusGenerator.java:103)

修改CodeGenerator.java

generator/src/main/java/io/geekidea/springbootplus/generator/CodeGenerator.java



 


    // 如果包路径为空,转换包名称路径
    if (StringUtils.isNotBlank(parentPackage)) {
        generatorProperties.setParentPackagePath(parentPackage.replaceAll("\\.", Matcher.quoteReplacement(File.separator)));
    }

File.separator修改为:Matcher.quoteReplacement(File.separator)

最新代码已修复此问题

# 有表前缀,如何去掉表前缀生成

# 例如:tb_customer

如果期望生成的文件名称如下

├─java
│  └─com
│      └─example
│          ─customer
│            ├─controller
│            │      CustomerController.java
│            ├─entity
│            │      Customer.java
│            ├─mapper
│            │      CustomerMapper.java
│            ├─param
│            │      CustomerPageParam.java
│            └─service
│                │  CustomerService.java
│                └─impl
│                        CustomerServiceImpl.java
└─resources
    └─mapper
        └─customer
              CustomerMapper.xml

则在代码生成器中设置表前缀

generator/src/main/java/io/geekidea/springbootplus/generator/SpringBootPlusGenerator.java


 

 

    // 设置表信息
    generatorProperties.addTable("tb_customer","id");
    // 设置表前缀
    generatorProperties.setTablePrefix(Arrays.asList("tb_"));

# 生成的Customer实体类主要信息



 










@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@TableName("tb_customer")
@ApiModel(value = "Customer对象")
public class Customer extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @NotNull(message = "不能为空")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    // ...
}

# 同时生成多张表,并去掉表前缀

当表的主键名称都为id时,可按照以下方式,同时生成多张表,并去掉表前缀





 




 

 

    // 设置基本信息
    generatorProperties
            .setMavenModuleName("example")
            .setParentPackage("com.example")
            .setModuleName("shop")
            .setAuthor("geekidea")
            .setFileOverride(true);

    // 设置表信息
    generatorProperties.setTables("tb_account","shop_goods","shop_order");
    // 设置表前缀
    generatorProperties.setTablePrefix(Arrays.asList("tb_","shop_"));
├── java
│   └── com
│       └── example
│           └── shop
│               ├── controller
│               │   ├── AccountController.java
│               │   ├── GoodsController.java
│               │   └── OrderController.java
│               ├── entity
│               │   ├── Account.java
│               │   ├── Goods.java
│               │   └── Order.java
│               ├── mapper
│               │   ├── AccountMapper.java
│               │   ├── GoodsMapper.java
│               │   └── OrderMapper.java
│               ├── param
│               │   ├── AccountPageParam.java
│               │   ├── GoodsPageParam.java
│               │   └── OrderPageParam.java
│               └── service
│                   ├── AccountService.java
│                   ├── GoodsService.java
│                   ├── OrderService.java
│                   └── impl
│                       ├── AccountServiceImpl.java
│                       ├── GoodsServiceImpl.java
│                       └── OrderServiceImpl.java
└── resources
    └── mapper
        └── shop
            ├── AccountMapper.xml
            ├── GoodsMapper.xml
            └── OrderMapper.xml

# 主键列名称不是id怎么生成




 
 



    // 设置表信息
    generatorProperties
            .addTable("tb_account")
            .addTable("shop_goods","goods_id")
            .addTable("shop_order","order_Id");
    // 设置表前缀
    generatorProperties.setTablePrefix(Arrays.asList("tb_","shop_"));



 






 




@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@TableName("shop_goods")
@ApiModel(value = "Goods对象")
public class Goods extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @NotNull(message = "goodsId不能为空", groups = {Update.class})
    @ApiModelProperty("主键")
    @TableId(value = "goods_id", type = IdType.AUTO)
    private Long goodsId;
    // ...
}



 






 




@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@TableName("shop_order")
@ApiModel(value = "Order对象")
public class Order extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @NotNull(message = "orderId不能为空", groups = {Update.class})
    @ApiModelProperty("主键")
    @TableId(value = "order_id", type = IdType.AUTO)
    private Long orderId;
    // ...
}

# 主键自增使用UUID/雪花算法

如果需要更改主键生成策略,需修改`application.yml`中的`id-type`为对应枚举值

对应枚举值:com.baomidou.mybatisplus.annotation.IdType.java

############################### mybatis-plus start #################################
mybatis-plus:
  global-config:
    db-config:
      # 全局默认主键类型
      id-type: auto

# 生成代码修改IdTypeapplication.yml中的id-type值一致

  • IdType
  • AUTO:自增
  • ASSIGN_ID:雪花算法,全局唯一
  • ASSIGN_UUID:UUID.replace("-",""),全局唯一
  • 更多方式请查看 com.baomidou.mybatisplus.annotation.IdType.java




 


    // 全局配置
    generatorProperties.getMybatisPlusGeneratorConfig().getGlobalConfig()
            .setOpen(true)
            .setSwagger2(true)
            .setIdType(IdType.ASSIGN_ID)
            .setDateType(DateType.ONLY_DATE);

# 生成的XML文件中没有SQL语句问题

当生成策略为`SINGLE`,生成单表操作代码,此时,XML为空配置

generator/src/main/java/io/geekidea/springbootplus/generator/config/GeneratorStrategy.java

/**
 * 生成策略
 * SIMPLE 生成最基本的代码
 * SINGLE 生成单表操作代码
 * ALL 生成所有的代码
 *
 * @author geekidea
 * @date 2020/3/13
 **/
public enum GeneratorStrategy {
    SIMPLE, SINGLE, ALL
}

generator/src/main/java/io/geekidea/springbootplus/generator/SpringBootPlusGenerator.java

默认生成策略为GeneratorStrategy.SINGLE),生成单表操作



 


    // 生成配置
    generatorProperties.getGeneratorConfig()
            .setGeneratorStrategy(GeneratorStrategy.SINGLE)
            .setGeneratorEntity(true)

# 如果XML需要生成基本列和自定义分页查询,修改策略为ALL



 


    // 生成配置
    generatorProperties.getGeneratorConfig()
            .setGeneratorStrategy(GeneratorStrategy.ALL)
            .setGeneratorEntity(true)

# 生成的FooBarMapper.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.example.foobar.mapper.FooBarMapper">

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, name, foo, bar, remark, state, version, create_time, update_time
    </sql>

    <select id="getFooBarById" resultType="com.example.foobar.vo.FooBarQueryVo">
        select
        <include refid="Base_Column_List"/>
        from foo_bar where id = #{id}
    </select>

    <select id="getFooBarPageList" parameterType="com.example.foobar.param.FooBarPageParam" resultType="com.example.foobar.vo.FooBarQueryVo">
        select
        <include refid="Base_Column_List"/>
        from foo_bar
    </select>

</mapper>