MySQL,并创建示例数据库
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id', `username` varchar(60) NOT NULL DEFAULT '' COMMENT '姓名', `password` char(32) NOT NULL DEFAULT '' COMMENT '密码', `salt` char(6) NOT NULL DEFAULT '' COMMENT '盐值', `status` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT 'status 0 正常 1 已注销', PRIMARY KEY (`uid`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户'; -- ---------------------------- -- Table structure for user_detail -- ---------------------------- DROP TABLE IF EXISTS `user_detail`; CREATE TABLE `user_detail` ( `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '主键id', `address` varchar(300) NOT NULL DEFAULT '' COMMENT '地址', `nickname` varchar(30) NOT NULL DEFAULT '' COMMENT '昵称', `sex` tinyint(1) NOT NULL DEFAULT '0' COMMENT '性别 0 未知 1 男 2 女', `about` varchar(255) NOT NULL DEFAULT '' COMMENT '关于', PRIMARY KEY (`uid`), UNIQUE KEY `nickname` (`nickname`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户详情';
Git
Postman
git地址:https://codeup.aliyun.com/61e4df994b66e5c57455d0a2/base/code-generation
注:需要运维开通下载权限
git clone git @codeup .aliyun.com:61e4df994b66e5c57455d0a2/base/code-generation.git |
# 输出目录 outputDir=C:\\generator\\ # 作者 author=tongwen # 包信息 package.parent=com.cto.user # 数据库前缀 db.prefix=MdbUh # 数据库缩写 db.abbr=mdb-uh # 数据库驱动(修改为本机信息) db.driverName=com.mysql.jdbc.Driver db.url=jdbc:mysql://121.36.19.143:3306/code_generator?useUnicode=true&useSSL=false&characterEncoding=utf8 db.username=root db.password=Root123456 # 表名,多个用,号分隔 db.table.name=user,user_detail # 是否生成业务 isCreateBusiness=true
5、运行CodeGeneration入口脚本
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="<a href="http://maven.apache.org/POM/4.0.0" "="" style="text-decoration-line: none; border-radius: 0px; background: 0px center; border: 0px; inset: auto; float: none; height: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://maven.apache.org/POM/4.0.0" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="text-decoration-line: none; border-radius: 0px; background: 0px center; border: 0px; inset: auto; float: none; height: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd" "="" style="text-decoration-line: none; border-radius: 0px; background: 0px center; border: 0px; inset: auto; float: none; height: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cto.user</groupId> <artifactId>crud</artifactId> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <!-- springboot 依赖版本 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- springboot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.2</version> </dependency> <!-- join 查询 --> <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-plus-join</artifactId> <version>1.2.1</version> </dependency> <!-- 多数据源 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.7</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> <!-- swagger --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.3</version> </dependency> <!-- hutool工具 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.8</version> </dependency> <!-- 公用工具 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> </dependencies> </project>
package com.cto.user; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; /** * 启动类 * @author tongwen */ @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @MapperScan("com.cto.user.mapper") @ComponentScan(basePackages = "com.cto") public class UserApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(UserApplication.class); } public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
server: port: 8001 spring: autoconfigure: #自动化配置 例外处理 exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure datasource: type: com.alibaba.druid.pool.DruidDataSource dynamic: primary: mdb-uh # 设置默认的数据源或者数据源组,默认值即为master datasource: # MySQL-用户Home,修改为自己的数据库信息 mdb-uh: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://121.36.19.143:3306/code_generator?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai username: root password: "Root123456" mybatis-plus: mapper-locations: classpath:com/cto/user/mapper/*Mapper.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启sql日志 #实体扫码多个package用逗号或者分号分隔 type-aliases-package: com.cto.user.entity global-config: datacenter-id: 1 worker-id: 1 # 配置日志 logging: file: name: # 接口文档 swagger: enabled: true title: 用户服务 base-package: com.cto.user version: V1.0 description: 用户服务 license: Apache License, Version 2.0 license-url: https://www.apache.org/licenses/LICENSE-2.0.html terms-of-service-url: http://localhost:8001/doc.html contact: tongwen@51cto.com
Wrapper : 条件构造抽象类,最顶端父类
AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : Entity 对象封装操作类,不是用lambda语法
UpdateWrapper : Update 条件封装,用于Entity对象更新操作
AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper
@SpringBootApplication
springboot的主启动注解
@MapperScan
在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
@ComponentScan
自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中
@Override
告诉读代码的人,这是一个重写的方法
@Autowired
字段在构造bean之后,在调用任何配置方法之前被注入
@Data
该注解通常用在实体bean上,不需要写出set和get方法,但是具备实体bean所具备的方法,简化编程提高变成速度
没有难的技术,当你弄清它的原理时,你会发现原来如此简单~ 欢迎加群【536501561】讨论