工程结构
开源插件ACTable,基于Mybatis开发的增强插件,实现自动建表、并支持共通CUDR。
关键代码
工程==>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- 开源插件ACTable --> <dependency> <groupId>com.gi;/groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.1.1.RELEASE</version> </dependency> <!-- pagehelper分页 --> <dependency> <groupId>com.gi;/groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> <!-- Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency> <!-- lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
配置文件==>a
spring: datasource: driver-class-name: com.my url: jdbc:mysql://127.0.0.1:3306/demo2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid. druid: initial-size: 8 min-idle: 1 max-active: 20 max-wait: 60000 time-between-eviction-runsMillis: 60000 min-evictable-idle-timeMillis: 300000 validation-query: select 'x' FROM DUAL test-on-borrow: false test-on-return: false max-open-prepared-statements: 20 max-pool-prepared-statement-per-connection-size: 20 filters: stat connection-properties: druid.;druid. initialization-mode: always
配置文件==>a
myba myba myba #create:系统启动后,会将所有的表删除掉,然后根据entity类中配置的结构重新建表,该操作会破坏原有数据。 #update:系统启动后,会根据entity类中配置的结构对表字段进行增删改操作 #------增:添加数据库表;根据实体向数据库表中添加字段 #------删:根据实体删除数据库表中的字段;不能实现删除项目实体类而删除数据库表 #------改:修改数据库字段的名字、属性 #none:系统不做任何处理
配置类==>MyBatisActableConfig
package com.w; import com.alibaba.druid.; import org.myba; import org.; import org.; import org.; import org.; import org.; import org.; import org.; @Configuration @ComponentScan(basePackages = {"com.gi.*"}) public class MyBatisActableConfig { @Value("${}") private String driver; @Value("${}") private String url; @Value("${}") private String username; @Value("${}") private String password; @Bean public PropertiesFactoryBean configProperties() throws Exception { PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); ("classpath*:a")); return propertiesFactoryBean; } @Bean public DruidDataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); da(driver); da(url); da(username); da(password); da(30); da(10); da("SELECT 1"); da(true); return dataSource; } @Bean public DataSourceTransactionManager dataSourceTransactionManager() { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); da(dataSource()); return dataSourceTransactionManager; } @Bean(name = "createTableSqlSessionFactory") public SqlSessionFactoryBean sqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); (dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); ("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml")); ("com.w.*"); return sqlSessionFactoryBean; } }
配置类==>MyBatisMapperScannerConfig
package com.w; import org.myba; import org.; import org.; import org.; @Configuration @AutoConfigureAfter) public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() throws Exception { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); ma("com.w.*;com.gi.*"); ma("createTableSqlSessionFactory"); return mapperScannerConfigurer; } }
实体类==>User
package com.w; import com.baomidou.myba; import com.baomidou.myba; import com.gi; import com.gi; import com.gi; import com.gi; import com.gi; import com.gi; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.ma; import java.; import java.u; @Data @AllArgsConstructor @NoArgsConstructor @Table(name = "t_user")//设置表名 public class User extends BaseModel { @TableId(type = IdTy)//mybatis-plus主键注解 @IsAutoIncrement //自增 @IsKey //actable主键注解 @Column(comment = "用户ID") //对应数据库字段,不配置name会直接采用属性名作为字段名comment是注解 private Long id; @Column(comment = "用户名") private String username; @Column(comment = "昵称") // Column不指定name,字段名称就为:nickName private String nickName; @Column(name = "mail", comment = "邮箱") // Column指定name,字段名称就为:mail private String email; @Column(name = "user_status", comment = "用户状态") private Integer userStatus; @Column(name = "password", comment = "密码") private String password; @Column(name = "password2", comment = "确认密码") // name名称不可以重复 private String rePassword; @Column(name = "create_time", comment = "创建时间") private Date createTime; @Column(name = "update_time", comment = "修改时间") // 不支持 LocalDateTime private Date updateTime; @Column(name = "number", type = MySqlTy, length = 5) private Long number; @Column(name = "lifecycle", type = MySqlTy, length = 1) private String lifecycle; @Column(name = "dekes", type = MySqlTy, length = 5, decimalLength = 2) private Double dekes; @Column(name = "big_decimal", type = MySqlTy, length = 10, decimalLength = 2) private BigDecimal bigDecimal; }
启动类==>DemoApplication
package com.w; import org.; import org.; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { S); } }
运行效果