From d2588de740de2bfc0582835b15fda8987187c9d2 Mon Sep 17 00:00:00 2001 From: wxcmyx Date: Tue, 21 Jan 2025 15:21:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0demo=E7=9A=84springboot?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=92=8Cmybatis-flex=E9=92=88=E5=AF=B9spring?= =?UTF-8?q?boot3=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-springboot3/pom.xml | 76 +++++-------------- .../com/mybatisflex/admin/Application.java | 4 - .../com/mybatisflex/admin/model/Account.java | 35 +-------- 3 files changed, 21 insertions(+), 94 deletions(-) diff --git a/mybatis-flex-springboot3/pom.xml b/mybatis-flex-springboot3/pom.xml index 8574488..ebcbe69 100644 --- a/mybatis-flex-springboot3/pom.xml +++ b/mybatis-flex-springboot3/pom.xml @@ -6,90 +6,62 @@ com.mybatis-flex.samples mybatis-flex-springboot3 1.0-SNAPSHOT - + + org.springframework.boot + spring-boot-starter-parent + 3.4.1 + + 17 17 - - 1.8.2 - 8.0.27 - - 6.0.7 - 3.0.4 - 2.0.7 - 5.0.1 - - - 4.13.2 - - com.mybatis-flex mybatis-flex-spring-boot3-starter - ${mybatis-flex.version} + 1.10.5 + + + com.mybatis-flex + mybatis-flex-processor + 1.10.5 + provided - mysql mysql-connector-java - ${mysql.version} + 8.0.33 - - - - - - - com.zaxxer HikariCP - ${HikariCP.version} - - org.springframework.boot spring-boot-starter - ${spring-boot.version} - org.springframework.boot spring-boot-starter-web - ${spring-boot.version} - org.springframework spring-context-support - ${spring.version} - org.springframework.boot spring-boot-autoconfigure - ${spring-boot.version} - org.springframework spring-jdbc - ${spring.version} - org.springframework spring-test - ${spring.version} - - - - @@ -98,8 +70,6 @@ ${slf4j.version} - - junit @@ -107,11 +77,12 @@ ${junit.version} test - - + + org.projectlombok + lombok + provided + - - @@ -129,21 +100,12 @@ target/generated-sources - org.apache.maven.plugins maven-compiler-plugin 3.8.1 - - 1.8 - 1.8 - UTF-8 - - - - \ No newline at end of file diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/Application.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/Application.java index c80972c..f4947cb 100644 --- a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/Application.java +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/Application.java @@ -22,11 +22,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.mybatisflex.admin.mapper") public class Application { - - public static void main(String[] args) { SpringApplication.run(Application.class, args); } - - } diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Account.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Account.java index c144a77..62feea6 100644 --- a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Account.java +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Account.java @@ -18,47 +18,16 @@ package com.mybatisflex.admin.model; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.KeyType; import com.mybatisflex.annotation.Table; +import lombok.Data; import java.util.Date; @Table("tb_account") +@Data public class Account { - @Id(keyType = KeyType.Auto) private Long id; private String userName; private Integer age; private Date birthday; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public Integer getAge() { - return age; - } - - public void setAge(Integer age) { - this.age = age; - } - - public Date getBirthday() { - return birthday; - } - - public void setBirthday(Date birthday) { - this.birthday = birthday; - } } -- Gitee From 17a5cefc5a655129cae9170d88986f1907011f5a Mon Sep 17 00:00:00 2001 From: wxcmyx Date: Tue, 21 Jan 2025 17:08:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0demo=E7=9A=84springboot?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=92=8Cmybatis-flex=E9=92=88=E5=AF=B9spring?= =?UTF-8?q?boot3=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/admin/controller/AccountController.java | 7 +++++++ .../src/main/resources/application.yml | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/controller/AccountController.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/controller/AccountController.java index 3be78b9..909e2f6 100644 --- a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/controller/AccountController.java +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/controller/AccountController.java @@ -17,6 +17,8 @@ package com.mybatisflex.admin.controller; import com.mybatisflex.admin.mapper.AccountMapper; import com.mybatisflex.admin.model.Account; +import com.mybatisflex.core.paginate.Page; +import com.mybatisflex.core.query.QueryWrapper; import jakarta.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; @@ -47,4 +49,9 @@ public class AccountController { account2.setBirthday(new Date()); accountMapper.insert(account2); } + + @GetMapping("/page") + Page page() { + return accountMapper.paginate(1,10,QueryWrapper.create().where(Account::getAge).gt(1)); + } } diff --git a/mybatis-flex-springboot3/src/main/resources/application.yml b/mybatis-flex-springboot3/src/main/resources/application.yml index b7b68da..e5023a6 100644 --- a/mybatis-flex-springboot3/src/main/resources/application.yml +++ b/mybatis-flex-springboot3/src/main/resources/application.yml @@ -1,7 +1,10 @@ + # DataSource Config spring: datasource: # driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://127.0.0.1:3306/dbtest + url: jdbc:mysql://127.0.0.1:3306/mybatis-flex-dbtest username: root - password: 123456 \ No newline at end of file + password: 12345678 +server: + port: 8081 \ No newline at end of file -- Gitee From 5b617bb3c6c57ddbc7936dc1229746e4ec3cb615 Mon Sep 17 00:00:00 2001 From: wxcmyx Date: Wed, 22 Jan 2025 11:08:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0demo=E7=9A=84springboot?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=92=8Cmybatis-flex=E9=92=88=E5=AF=B9spring?= =?UTF-8?q?boot3=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-springboot3/pom.xml | 5 ++ .../com/mybatisflex/admin/code/Codegen.java | 89 +++++++++++++++++++ .../admin/mapper/ArticleMapper.java | 14 +++ .../com/mybatisflex/admin/model/Article.java | 58 ++++++++++++ .../src/main/resources/application.yml | 1 - 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/code/Codegen.java create mode 100644 mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/mapper/ArticleMapper.java create mode 100644 mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Article.java diff --git a/mybatis-flex-springboot3/pom.xml b/mybatis-flex-springboot3/pom.xml index ebcbe69..d2a9868 100644 --- a/mybatis-flex-springboot3/pom.xml +++ b/mybatis-flex-springboot3/pom.xml @@ -23,6 +23,11 @@ mybatis-flex-spring-boot3-starter 1.10.5 + + com.mybatis-flex + mybatis-flex-codegen + 1.10.1 + com.mybatis-flex mybatis-flex-processor diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/code/Codegen.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/code/Codegen.java new file mode 100644 index 0000000..457b9d1 --- /dev/null +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/code/Codegen.java @@ -0,0 +1,89 @@ +package com.mybatisflex.admin.code; + +import com.mybatisflex.codegen.Generator; +import com.mybatisflex.codegen.config.ColumnConfig; +import com.mybatisflex.codegen.config.GlobalConfig; +import com.zaxxer.hikari.HikariDataSource; + +public class Codegen { + + public static void main(String[] args) { + //配置数据源 + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex-dbtest?characterEncoding=utf-8"); + dataSource.setUsername("root"); + dataSource.setPassword("12345678"); + + //创建配置内容,两种风格都可以。 + GlobalConfig globalConfig = createGlobalConfigUseStyle1(); + //GlobalConfig globalConfig = createGlobalConfigUseStyle2(); + + //通过 datasource 和 globalConfig 创建代码生成器 + Generator generator = new Generator(dataSource, globalConfig); + + //生成代码 + generator.generate(); + } + + public static GlobalConfig createGlobalConfigUseStyle1() { + //创建配置内容 + GlobalConfig globalConfig = new GlobalConfig(); + + //设置根包 + globalConfig.setBasePackage("com.test"); + + //设置表前缀和只生成哪些表 + globalConfig.setTablePrefix("tb_"); + globalConfig.setGenerateTable("article", "tb_account"); + + //设置生成 entity 并启用 Lombok + globalConfig.setEntityGenerateEnable(true); + globalConfig.setEntityWithLombok(true); + //设置项目的JDK版本,项目的JDK为14及以上时建议设置该项,小于14则可以不设置 + globalConfig.setEntityJdkVersion(17); + + //设置生成 mapper + globalConfig.setMapperGenerateEnable(true); + + //可以单独配置某个列 +// ColumnConfig columnConfig = new ColumnConfig(); +// columnConfig.setColumnName("tenant_id"); +// columnConfig.setLarge(true); +// columnConfig.setVersion(true); +// globalConfig.setColumnConfig("tb_account", columnConfig); + + return globalConfig; + } + + public static GlobalConfig createGlobalConfigUseStyle2() { + //创建配置内容 + GlobalConfig globalConfig = new GlobalConfig(); + + //设置根包 + globalConfig.getPackageConfig() + .setBasePackage("com.test"); + + //设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表 + globalConfig.getStrategyConfig() + .setTablePrefix("tb_") + .setGenerateTable("tb_account", "tb_account_session"); + + //设置生成 entity 并启用 Lombok + globalConfig.enableEntity() + .setWithLombok(true) + .setJdkVersion(17); + + //设置生成 mapper + globalConfig.enableMapper(); + + //可以单独配置某个列 + ColumnConfig columnConfig = new ColumnConfig(); + columnConfig.setColumnName("tenant_id"); + columnConfig.setLarge(true); + columnConfig.setVersion(true); + globalConfig.getStrategyConfig() + .setColumnConfig("tb_account", columnConfig); + + return globalConfig; + } +} \ No newline at end of file diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/mapper/ArticleMapper.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/mapper/ArticleMapper.java new file mode 100644 index 0000000..46519f3 --- /dev/null +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/mapper/ArticleMapper.java @@ -0,0 +1,14 @@ +package com.mybatisflex.admin.mapper; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.admin.model.Article; + +/** + * 文章 映射层。 + * + * @author xcwang + * @since 2025-01-22 + */ +public interface ArticleMapper extends BaseMapper
{ + +} diff --git a/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Article.java b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Article.java new file mode 100644 index 0000000..90b5f6f --- /dev/null +++ b/mybatis-flex-springboot3/src/main/java/com/mybatisflex/admin/model/Article.java @@ -0,0 +1,58 @@ +package com.mybatisflex.admin.model; + +import com.mybatisflex.annotation.Id; +import com.mybatisflex.annotation.KeyType; +import com.mybatisflex.annotation.Table; +import java.io.Serializable; +import java.time.LocalDateTime; + +import java.io.Serial; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 文章 实体类。 + * + * @author xcwang + * @since 2025-01-22 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Table("article") +public class Article implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * ID + */ + @Id(keyType = KeyType.Auto) + private Long id; + + /** + * 标题 + */ + private String title; + + /** + * 内容 + */ + private String content; + + /** + * 创建时间 + */ + private LocalDateTime createAt; + + /** + * 创建者 + */ + private Long createBy; + +} diff --git a/mybatis-flex-springboot3/src/main/resources/application.yml b/mybatis-flex-springboot3/src/main/resources/application.yml index e5023a6..1dfdd81 100644 --- a/mybatis-flex-springboot3/src/main/resources/application.yml +++ b/mybatis-flex-springboot3/src/main/resources/application.yml @@ -1,4 +1,3 @@ - # DataSource Config spring: datasource: -- Gitee