SpringBoot project integrates ACTable to quickly generate database tables from entity classes

1.Installation

ACTable dependency


    com.gitee.sunchenbin.mybatis.actable
    mybatis-enhance-actable
    1.5.0.RELEASE

Use mybatis-plus to avoid jar package version conflicts. You can refer to the following methods to introduce dependencies.


    com.gitee.sunchenbin.mybatis.actable
    mybatis-enhance-actable
    1.5.0.RELEASE
   
       
com.baomidou
            mybatis-plus-annotation
       

   

The abnormality is as follows: an OrderBy annotation ClassNotFindException exception is reported during the project startup process.

2.SpringBoot configuration method

1. The application.properties configuration file of the project (choose one of the two)

  1. Configure the table creation mode of actable, the package path of the model on which the table is built, and the type of database. Configure according to your own situation.

  2. Configure the xml used by actable into the mapperlocations of mybatis. The value of this item is fixed: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/l

The code example is as follows:

# actable configuration information
actable.table.auto=update
actable.model.pack=com.yours.model
actable.database.type=mysql
actable.index.prefix=Self-defined index prefix #If this configuration item is not set, actable_idx_ will be used by default
actable.unique.prefix=Self-defined unique constraint prefix #If this configuration item is not set, actable_uni_ will be used by default
# mybatis’ own configuration information, the key may also be: mybatis.mapperLocations
mybatis.mapper-locations=classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml

PS: If you use mybatis-plus, you need to configure the classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml here: mybatis-plus.mapper-locations

2. Project application.yml configuration file (choose one of the two)

# actable configuration information
actable:
  table:
# none The system does not perform any processing.
# create After the system starts, all tables will be deleted first, and then the tables will be rebuilt according to the structure configured in the model. This operation will destroy the original data.
# update After the system starts, it will automatically determine which tables are newly created, which fields need to be modified, etc., which fields need to be deleted, and which fields need to be added. This operation will not destroy the original data.
# add After the system is started, it will only perform new additions, such as adding tables/adding fields/adding new indexes/adding unique constraints, but will not perform modification and deletion operations (only in version 1.0.9. RELEASE and above supported).
    auto: none
  model:
pack: scanned package path
  database:
type: mysql //Supported databases, currently only mysql is supported
  index:
prefix: xxx\_idx #Self-defined index prefix #If this configuration item is not set, actable\_idx\_ will be used by default
  unique:
prefix: xxx\_uni #Self-defined unique constraint prefix #If this configuration item is not set, actable\_uni\_ will be used by default

Configuration analysis:

The springboot2.0+ startup class requires the following configuration (required)

1.@ComponentScan configuration, path “com.gitee.sunchenbin.mybatis.actable.manager.*”

2.@MapperScan configuration, path “com.gitee.sunchenbin.mybatis.actable.dao.*”

The code example is as follows:

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients
@MapperScan(“com.gitee.sunchenbin.mybatis.actable.dao.*”)
@ComponentScan(basePackages = {“com.gitee.sunchenbin.mybatis.actable.manager.*”})
public class OrderApiApplication {   
    public static void main(String[] args) {      
        SpringApplication.run(OrderApiApplication.class, args);   
    }
}

The springboot3.0+ startup class needs to be configured as follows (required). Failure to automatically execute table creation requires that the code actively trigger execution after springboot is initialized.

1.@ComponentScan configuration, path “com.gitee.sunchenbin.mybatis.actable.manager.*”

2.@MapperScan configuration, path “com.gitee.sunchenbin.mybatis.actable.dao.*”

The code example is as follows:

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients
@MapperScan(“com.gitee.sunchenbin.mybatis.actable.dao.*”)
@ComponentScan(basePackages = {“com.gitee.sunchenbin.mybatis.actable.manager.*”})
public class OrderApiApplication {   
    public static void main(String[] args) {      
        ConfigurableApplicationContext run = SpringApplication.run(OrderApiApplication.class, args);
// Get the core processing class of actable in the container
        StartUpHandler bean =run.getBean(StartUpHandler.class, args);
// Manually execute actable's table creation method
        bean.startHandler();
    }
}

3.Commonly used annotations

1.@Table

Description: Table name annotation

Attributes

type

Is it required?

default value

describe

name

String

no

“”

If left blank, the table name will be converted according to the camel case conversion method. For example, ProductLabel will be automatically converted to product_label.

value

comment

String

no

“”

Table comments can also be replaced by the @TableComment annotation

charset

MySqlCharsetConstant

no

MySqlCharsetConstant.DEFAULT

Table character set, you can also use the @TableCharset annotation instead. The default value means not filling in. The default value of the database shall prevail.

engine

MySqlEngineConstant

no

MySqlEngineConstant.DEFAULT

Table storage engine, you can also use the @TableEngine annotation instead. The default value means not filling in. The default value of the database shall prevail.

isSimple

boolean

no

false

Whether to enable simple mode configuration. It is not enabled by default. After it is enabled, Field does not write annotation @Column, and fields can also be created using the default camel case conversion method.

excludeFields

String[]

no

“serialVersionUID”

The name of the attribute that needs to be excluded. The excluded attributes will not be involved in table creation.

2.@IgnoreTable

Description: Ignore the current entity table creation logic and set it on the entity class. If set, the entity will not participate in table creation and update tables.

3.@TableComment

Description: Table annotation annotation

Attributes

type

Is it required?

default value

describe

value

String

yes

Table comments, same as @Table.comment() attribute

3.@Column

Description: Field annotation

Attributes

type

Is it required?

default value

describe

value

String

no

“”

Field name

name

type

MySqlTypeConstant

no

MySqlTypeConstant.DEFAULT

Field type can also be replaced by @ColumnType. The default value identifier is left blank and ACTable automatic type conversion rules are used.

length

int

no

255

The default length of the field. When the default is 255, the actual default length of the type will be read.

decimalLength

int

no

0

The number of decimal points to retain. When the default is 0, the actual default length of the novel point of this type will be read.

isNull

boolean

no

true

Whether it can be null, true means it can, false means it cannot, the default is true, you can also use @IsNotNull instead

isKey

boolean

no

false

Whether it is the primary key, the default is false, you can also use @IsKey instead

isAutoIncrement

boolean

no

false

Whether to automatically increment, the default is false, you can also use @IsAutoIncrement instead

defaultValue

String

no

ColumnUtils.DEFAULTVALUE

Default value, the default is DEFAULT, which means not to fill in, the default is null, it can be replaced by @DefaultValue

Only String and Boolean use string parameter passing method using '#{}', other types use native method to splice sql using '${}'

The timestamp field type must use: MySqlTypeConstant.TIMESTAMP

Creation time timestamp can be used

defaultValue=”CURRENT_TIMESTAMP”

Update time timestamp can be used

defaultValue=”NULL ON UPDATE CURRENT_TIMESTAMP”

comment

String

no

“”

Data table field comments can also be replaced by @ColumnComment

4.@IgnoreUpdate

Description: Mark the current field to participate in creation but not update, to solve the problem that when using @DefaultValue(“NULL ON UPDATE CURRENT_TIMESTAMP”), the current field will be updated every time the project is restarted.

Attributes

type

Is it required?

default value

describe

value

String

yes

Whether to ignore update operations on the current field

MySqlTypeConstant

Field type optional enum value list

describe

DEFAULT

Leave the default value identifier blank and use ACTable automatic type conversion rules.

INT

int type, default length 11, no decimal point length

VARCHAR

Varchar type, default length 255, no decimal point length

DECIMAL

decimal type, default length 10, decimal point default length 2

TEXT

text type, no need to set the length

……..

There are too many to list one by one. Check the code yourself. In the enumeration, lengthCount indicates how many lengths are required (0: no need to set the length, 1: need to set a length, 2: need to set the decimal point length), lengthDefault indicates the default length, and decimalLengthDefault indicates Decimal point default length

ACTable automatic type conversion rules

Javatype of data

correspondMysqltype of data

java.lang.String

MySqlTypeConstant.VARCHAR

java.lang.Long

MySqlTypeConstant.BIGINT

java.lang.Integer

MySqlTypeConstant.INT

java.lang.Boolean

MySqlTypeConstant.BIT

java.math.BigInteger

MySqlTypeConstant.BIGINT

java.lang.Float

MySqlTypeConstant.FLOAT

java.lang.Double

MySqlTypeConstant.DOUBLE

java.lang.Short

MySqlTypeConstant.SMALLINT

java.math.BigDecimal

MySqlTypeConstant.DECIMAL

java.sql.Date

MySqlTypeConstant.DATE

java.util.Date

MySqlTypeConstant.DATE

java.sql.Timestamp

MySqlTypeConstant.DATETIME

java.sql.Time

MySqlTypeConstant.TIME

long

MySqlTypeConstant.BIGINT

int

MySqlTypeConstant.INT

boolean

MySqlTypeConstant.BIT

float

MySqlTypeConstant.FLOAT

double

MySqlTypeConstant.DOUBLE

short

MySqlTypeConstant.SMALLINT

char

MySqlTypeConstant.VARCHAR

5.@ColumnComment

Description: Field remarks annotation

Attributes

type

Is it required?

default value

describe

value

String

yes

Field comments, same as @Column.comment() attribute

6.@DefaultValue

Description: Field default value annotation

Attributes

type

Is it required?

default value

describe

value

String

yes

Field default value, same as @Column.defaultValue() property

Creation time timestamp can be used

@ColumnType(MySqlTypeConstant.TIMESTAMP)

@DefaultValue(“CURRENT_TIMESTAMP”)

Update time timestamp can be used

@ColumnType(MySqlTypeConstant.TIMESTAMP)
@IgnoreUpdate @DefaultValue(“NULL ON UPDATE CURRENT_TIMESTAMP”)

7.@IsAutoIncrement

Description: Field auto-increment annotation, same as @Column.isAutoIncrement

8.@IsKey

Description: The field is the annotation of the primary key, the same as @Column.isKey

9.@IsNotNull

Description: Annotation that the field is not empty, same as @Column.isNull()=false

10.@ColumnType

Description: Field type annotation

Attributes

type

Is it required?

default value

describe

value

MySqlTypeConstant

no

MySqlTypeConstant.DEFAULT

Field type can also be replaced by @Column.type. The default value identifier is left blank and ACTable automatic type conversion rules are used.

length

int

no

255

The default length of the field. When the default is 255, the actual default length of the type will be read.

decimalLength

int

no

0

The number of decimal points to retain. When the default is 0, the actual default length of the novel point of this type will be read.

11.@Index

Description: Annotation for setting the field to index

Attributes

type

Is it required?

default value

describe

value

String

no

“”

The name of the index, if not set, defaults to {actable_idx_current tag field name@Column's name}. If a name such as union_name is set, the system will add actable_idx_ prefix before the name by default, that is, actable_idx_union_name

columns

String[]

no

{}

The field name to be indexed. If you do not set the name, it defaults to the current marked field name @Column. You can set multiple joint indexes {“login_mobile”, “login_name”}

12.@Unique

Description: Annotations for setting unique constraints on fields

Attributes

type

Is it required?

default value

describe

value

String

no

“”

The name of the unique constraint. If not set, the default is {actable_uni_current tag field name@Column's name}. If a name such as union_name is set, the system will add actable_uni_ prefix before the name by default, that is, actable_uni_union_name

columns

String[]

no

{}

The field name of the unique constraint. If not set, the default is the name of the current marked field name @Column. You can set multiple to create a joint unique {“login_mobile”, “login_name”}

4. Compatible framework configuration

mybatis-plus configuration method

For springboot architecture projects, if you need to use mybatis-plus, you only need to rely on the following configuration:


com.baomidou
    mybatis-plus-boot-starter
    3.4.1

Change the contents of the mybatis.mapper-locations configuration to the mybatis-plus.mapper-locations configuration.