初始化项目
使用Spring Initializr
- 选择依赖
- MyBatis Framework
- JDBC API
- MySQL Driver
- Spring Web
填写其他必要信息后,点击GENERATE得到代码。
flyway
- 引入flyway
</dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.8.1</version>
<configuration>
<user>root</user>
<password>root</password>
<url>jdbc:mysql://localhost:3306/databasename?useSSL=false&allowPublicKeyRetrieval=true</url>
</configuration>
</plugin>
</plugins>
</build>
- 编写sql文件
将文件放在src/main/resources/db/migration/
,命名为V1__CreateUser.sql
文件命名规则列表。
CREATE TABLE USER
(
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(100),
TEL VARCHAR(20) UNIQUE,
AVATAR_URL VARCHAR(1024),
ADDRESS VARCHAR(1024),
CREATED_AT TIMESTAMP NOT NULL DEFAULT NOW(),
UPDATED_AT TIMESTAMP NOT NULL DEFAULT NOW()
);
- 使用Docker起数据库
docker run --name image-name-shop -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=shop -p 3307:3306 -d mysql
- 输入
mvn flyway:migrate
运行flyway。
MyBatis Generator
MyBatis Generator
- 引入plugin
<plugins>
...
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
- 添加配置文件
<?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>
<context id="wxshop" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/shop?useSSL=false&allowPublicKeyRetrieval=true"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!-- 生成User.java 和 UserExample.java -->
<javaModelGenerator targetPackage="com.wester.shop.generate" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成UserMapper.xml -->
<sqlMapGenerator targetPackage="db.mybatis" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成UserMapper.java -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wester.shop.generate" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 配置数据库名 表名 生成的文件前缀 -->
<table schema="shop" tableName="USER" domainObjectName="User" >
<property name="useActualColumnNames" value="false"/>
<generatedKey column="ID" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
- 输入
mvn mybatis-generator:generate
运行flyway。