JAVAEE——实现Maven 配置mybatis反向生成实体类、接口及mapping映射文件操作

本文介绍使用MyBatis Generator自动生成代码的过程,包括配置Maven项目、数据库表准备、pom.xml配置、generatorConfig.xml配置及代码生成命令。通过此方法减少手动编写重复代码的工作量,提高开发效率。

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

实现流程:

第一步:

         新建Maven项目

           

第二步:

             在数据库中把你用的表全部先建好,其中连数据库表中属性都需要弄好

            

第三步:

        配置pom.xml文件,在pom.xml加入如下代码,保存即可

	<build>
		<finalName>mavenlianxi</finalName>
		 <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
 
            <plugin>
                  <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <plugin>
            	<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>9.2.8.v20150217</version>
				<configuration>
					<httpConnector>
						<port>80</port>
					</httpConnector>
					<stopKey>shutdown</stopKey>
					<stopPort>9966</stopPort>
				</configuration>
            </plugin>
        </plugins>
	</build>

第四步:

       在src/main/resource下配置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> 

<!-- 数据库驱动--> 

 <classPathEntry location="C:\MavenJars\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar"/> 
 <context id="DB2Tables" targetRuntime="MyBatis3"> 

 <commentGenerator> 
 <property name="suppressDate" value="true"/> 
 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 
 <property name="suppressAllComments" value="true"/> 

 </commentGenerator> 

 <!--数据库链接URL,用户名、密码 --> 

 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://39.106.193.131:3306/carproject" userId="root" password="123456"> 
 </jdbcConnection> 

 <javaTypeResolver> 
 <property name="forceBigDecimals" value="false"/> 
 </javaTypeResolver> 

 <!--生成Model类存放位置-->  
 <javaModelGenerator targetPackage="com.javen.entitys" targetProject="src/main/java"> 
 <property name="enableSubPackages" value="true"/> 
 <property name="trimStrings" value="true"/> 
 </javaModelGenerator> 

 <!-- 生成映射文件的包名和位置--> 
 <sqlMapGenerator targetPackage="com.javen.mapping" targetProject="src/main/java"> 
 <property name="enableSubPackages" value="true"/> 
 </sqlMapGenerator> 

 <!-- 生成DAO的包名和位置--> 
 <javaClientGenerator type="XMLMAPPER" targetPackage="com.javen.dao" targetProject="src/main/java"> 
 <property name="enableSubPackages" value="true"/> 
 </javaClientGenerator> 

 <!-- 要生成的表 tableName 是数据库中的表名或视图名  domainObjectName 是实体类名--> 
 
  <table tableName="notice" domainObjectName="Notice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="carinfo" domainObjectName="Carinfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="carnumber" domainObjectName="Carnumber" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="leavingmessage" domainObjectName="Leavingmessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="login" domainObjectName="Login" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="maintenanceplan" domainObjectName="Maintenanceplan" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="maintenancerecord" domainObjectName="Maintenancerecord" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="original_parameter" domainObjectName="OriginalParameter" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  <table tableName="userexchangeinformation" domainObjectName="UserexChangeInformation" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
 
 </context> 

</generatorConfiguration>

其中讲一下,需要改的地方。

location:你的mysql链接jar包的路径

targetPackage:生成的包名,(推荐一般是com.什么

targetProject:需要生成的项目,默认放在src/main/java路径下

tableName:表名

domainObjectName:需要生成的实体类名

第五步:

      右击maven项目,打开maven build(或者mvn),在Goals中输入mybatis-generator:generate

然后就自动生成了一系列文件了。


好处:这样我们可以不需要进行什么费力人工的操作了,也防止我们在定义一些类的接口,类的定义,和Mapper而可能出现的一些拼写错误。

 

坏处:前期不推荐直接这样操作,如果我们接触一个软件直接接触最简单的地方,那么以后我们遇到深沉次的问题或者这个方法用不了了,也不会修改。所以建议从底层框架搭建,如何搭建框架学起,这仅仅是一种处理渠道,但不是时时有用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值