springboot引入sqlite3,mybaits自动生成器连接sqlite
在Spring Boot项目中引入SQLite3并使用MyBatis Generator (MBG) 自动生成Mapper、Model和Mapper XML文件的步骤如下:
- 添加依赖到
pom.xml
:
<!-- SQLite JDBC Driver -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>YOUR_SQLITE_JDBC_VERSION</version>
</dependency>
<!-- MyBatis Generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>YOUR_MYBATIS_GENERATOR_VERSION</version>
</dependency>
- 配置
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>
<context id="SqliteContext" targetRuntime="MyBatis3">
<jdbcConnection driverClass="org.sqlite.JDBC"
connectionURL="jdbc:sqlite:path_to_your_sqlite_db_file.db" />
<javaModelGenerator targetPackage="com.yourpackage.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.yourpackage.mapper" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yourpackage.mapper" targetProject="src/main/java"/>
<table tableName="your_table_name">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
- 在Spring Boot应用中运行MyBatis Generator:
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MBGLauncher {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
确保替换\`YOUR\_SQLI
评论已关闭