spring boot逆向工程

本文详细介绍了如何使用Eclipse自动搭建SpringBoot项目,并配置pom.xml文件引入所需Maven依赖,包括SpringBoot启动器、MyBatis、MySQL驱动等。同时,分享了如何设置generatorConfig.xml进行MyBatis逆向工程,自动生成实体类、Mapper接口及XML映射文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于spring boot项目搭建以及pom.xml文件的maven包本人就放代码了,因为我使用eclipse自动搭建spring boot项目,以下就是pom.xml

<?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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>zzxm</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zzxm</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- mybatis逆向工程插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<configuration>
					<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

写一个generatorConfig.xml放在src/main/resources里面,这个就是逆向工程的配置

<?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>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>
	<context id="zbyTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/user" 
            userId="root"
			password="admin">
		</jdbcConnection>

		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator targetPackage="com.pojo"
			targetProject="src/main/java">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.mapper" 
			targetProject="src/main/resources">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.mapper" 
			targetProject="src/main/java">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->

		<!-- 数据库中的表名-->
		<table tableName="user"></table>
		<table tableName="administrator"></table>
		
	</context>
</generatorConfiguration>

然后就好了,使用命令:项目右击 -->  run as -->  maven build -->  mybatis-generator:generate

 

### Spring Boot MyBatis Generator Reverse Engineering Code Generation Tutorial Incorporating MyBatis Generator into a Spring Boot project allows for the automatic creation of Java classes based on existing database tables, streamlining development processes significantly[^1]. The following sections detail how to set up and utilize MyBatis Generator within a Spring Boot environment. #### Prerequisites Ensure that Maven or Gradle is installed as these tools will be used to manage dependencies. Additionally, have an operational MySQL (or any other supported RDBMS) instance with at least one table ready for reverse engineering purposes. #### Adding Dependencies Add necessary dependencies in `pom.xml` when using Maven: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- For generating code --> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> ``` For Gradle users, add similar configurations accordingly. #### Configuration File Setup Create a configuration file named `generatorConfig.xml`. Place it under resources directory which specifies details about JDBC connection along with targetProject where generated files should reside: ```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> <!-- Database Connection Information --> <context id="DB2Tables" targetRuntime="MyBatis3Simple"> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC" userId="root" password="password"/> <!-- Specify Where Generated Files Should Go --> <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java"/> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"/> <!-- Define Which Tables To Generate From --> <table tableName="example_table" domainObjectName="ExampleTable"/> </context> </generatorConfiguration> ``` This setup ensures proper mapping between SQL queries and POJOs while also creating mapper interfaces automatically. #### Running the Generator With everything configured correctly, run the plugin via command line (`mvn mybatis-generator:generate`) or through IDE plugins designed specifically for executing Maven goals. After successful execution, check specified directories for newly created model entities alongside corresponding XML mappers. By adhering closely to above guidelines, developers can efficiently leverage MyBatis Generator's capabilities without needing extensive manual coding efforts related to data access layers. --related questions-- 1. How does integrating MyBatis Generator impact application performance? 2. What alternatives exist besides MyBatis for ORM frameworks compatible with Spring Boot applications? 3. Can MyBatis Generator support multiple databases simultaneously during reverse engineering process? 4. Is there any way to customize templates used by MyBatis Generator for more tailored outputs? 5. Are there specific scenarios where not using an ORM tool like MyBatis might prove beneficial over traditional JDBC approaches?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZBY52031

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值