Mybatis逆向工程
Mybatis逆向工程
mybatis逆向工程是一个可以快速根据数据库表帮我们生成pojo实体类和mapper接口和mapper映射文件的一个插件,需要下载该项目。注意:只支持单表操作(单表的增删改查等sql可以帮助我们生成),关联查询需要自己写。
建议查阅tk-mybatis 更加好用的工具。
引入依赖
<!--mybatis逆向工程--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</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> <!--指定了驱动jar包的位置,这个是针对下载Jar包的方式,因为用了maven所以这个就用不上了--> <!-- <classPathEntry location="D:/mvn_repository_new/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar"/>--> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/qf-v2-v3_b?serverTimezone=Asia/Shanghai" userId="root" password="123456"> <property name="serverTimezone" value="UTC"/> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!--指定生成entity实体类的具体位置--> <javaModelGenerator targetPackage="com.qf.entity" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--指定生成mybatis映射xml文件的包名和位置--> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--指定生成mapper接口的具体位置--> <javaClientGenerator targetPackage="com.qf.mapper" targetProject="./src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--<!– 要生成entity/mapper的表名及自定义的DO名 –>--> <!--<table tableName="users" domainObjectName="User"/>--> <!--<table tableName="product_brand" domainObjectName="ProductBrand" />--> <!--mybatis generator代码生成器在默认的情况下会生成对于表实体类的一个Examle类, 可以更改生成器的配置可避免生成Examle类, enableCountByExample,enableUpdateByExample,enableDeleteByExample,enableSelectByExample等配置为false后, 就不会生成生成Examle类了 --> <table tableName="t_address" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
配置Generator的插件
<plugins> <!-- mybatis逆向工程插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> </dependencies> <configuration> <!--配置文件的路径--> <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> </plugin> </plugins>
进行单元测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath*:spring-context*.xml") public class TestGenerator { @Autowired TBusinessMapper tBusinessMapper; @Test public void testInsertBusiness(){ TBusiness tBusiness = new TBusiness(); tBusiness.setBsId(2L); tBusiness.settBsId(2L); tBusiness.setBsName("海尔冰箱"); tBusiness.setTbId(2L); tBusinessMapper.insert(tBusiness); } }
本文作者:最怕万一见温柔
本文链接:https://www.cnblogs.com/LoveForeverIT/p/18064200
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步