mysql+mybatis+generator整合
2022/4/21 2:12:31
本文主要是介绍mysql+mybatis+generator整合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在ORM框架中经常使用mybatis+generator来实现快速逆向生成代码,如下使用mysql数据库简单记录下
1.创建maven工程并选择jdk
2.在pom中添加引入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-demo-project</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>mybatisGenerator-demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <!--注入web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.5.RELEASE</version> </dependency> <!--注入lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.14</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> <exclusions> <exclusion> <artifactId>swagger-annotations</artifactId> <groupId>io.swagger</groupId> </exclusion> </exclusions> </dependency> <dependency> <artifactId>swagger-annotations</artifactId> <groupId>io.swagger</groupId> <version>1.5.22</version> </dependency> <!-- 加载mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!-- 加载jdbc连接数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.7.RELEASE</version> </plugin> <!--generator插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <!--对mysql解析 可省略 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> </dependencies> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
3.配置文件中添加数据库配置
server: port: 8080 spring: datasource: name: mysql url: jdbc:mysql://127.0.0.1:3306/devdatabase username: devuser password: admin mybatis: mapper-locations: classpath:mapping/*.xml #注意resources文件夹下目录层级使用"/"而不是"." type-aliases-package: com.mybatisgeneratordemo.entity # 注意:对应实体类的路径
4.在配置文件夹resources创建generatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 数据库驱动位置(本地mybatis-generator-core包的位置) --> <classPathEntry location="D:\repository\org\mybatis\generator\mybatis-generator-core\1.3.7\mybatis-generator-core-1.3.7.jar"/> <context id="context" targetRuntime="MyBatis3Simple"> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!-- 数据库的url、用户名、密码 --> <jdbcConnection userId="root" password="root" driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/devdatabase"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.mybatisgeneratordemo.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator targetPackage="com.mybatisgeneratordemo.mapper" type="XMLMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 生成那些表 tableName表名,domainObjectName应于数据库表的javaBean类名--> <table schema="BuyrUser" tableName="buyr_user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> </context> </generatorConfiguration>
5.创建数据库表:
CREATE TABLE `buyr_user` (
`USER_ID` bigint(19) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`USER_NAME` varchar(90) NOT NULL COMMENT '用户名称',
`NICK_NAME` varchar(90) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '用户昵称',
`USER_INTRO` varchar(300) DEFAULT NULL COMMENT '个性签名',
`AVATAR` varchar(1000) DEFAULT NULL COMMENT '头像图片',
`EMAIL` varchar(255) DEFAULT NULL COMMENT '邮件地址',
`PHONE` varchar(255) DEFAULT NULL COMMENT '手机号',
`USER_PASS` varchar(255) DEFAULT NULL COMMENT '密码',
`PASS_SALT` varchar(255) DEFAULT NULL COMMENT '密码盐',
`USER_STATUS` varchar(32) DEFAULT '1' COMMENT '用户状态',
`USER_SCORE` int(11) DEFAULT NULL COMMENT '用户打分',
`TOTAL_COST_AMT` decimal(24,6) DEFAULT NULL COMMENT '累计消费金额',
`LAST_LOGIN_TIME` timestamp NULL DEFAULT NULL COMMENT '最后登录时间',
`REVISION` int(11) DEFAULT NULL COMMENT '乐观锁',
`CREATED_BY` varchar(32) DEFAULT NULL COMMENT '创建人',
`CREATED_TIME` timestamp NULL DEFAULT NULL COMMENT '创建时间',
`UPDATED_BY` varchar(32) DEFAULT NULL COMMENT '更新人',
`UPDATED_TIME` timestamp NULL DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='买家';
6.maven工具窗点击并运行mybatis-generator:generate
运行窗展示 BUILD SUCCESS 并自动生成个对应文件, 同时注意修改实体中主键id的类型为String,防止Long长度过长前端数据进度丢失;
项目结构如下:
这篇关于mysql+mybatis+generator整合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-02MySQL 3主集群搭建
- 2024-12-25如何部署MySQL集群资料:新手入门教程
- 2024-12-24MySQL集群部署资料:新手入门教程
- 2024-12-24MySQL集群资料详解:新手入门教程
- 2024-12-24MySQL集群部署入门教程
- 2024-12-24部署MySQL集群学习:新手入门教程
- 2024-12-24部署MySQL集群入门:一步一步搭建指南
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解