Mybatis Plus 逆向工程介绍
这篇文章主要介绍了Mybatis Plus 逆向工程,文章通过Mybatis Plus 逆向工程相关资料及示例代码详细介绍,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考一下
目录
一、创建数据库
二、配置pom.xml 文件
三、在项目同级目录建立mgb.xml
四、在测试类中写入方法
一、创建数据库
注:给tbl_emp的表创建外键
数据库以及表的建立:
二、配置pom.xml 文件
1 2 3 4 5 | <dependency> <groupId>org.mybatis.generator< /groupId > <artifactId>mybatis-generator-core< /artifactId > <version>1.3.5< /version > < /dependency > |
导入mybatis-generator的jar包:
注:如果存在导入不进去的情况
在项目-右键-maven-updataproject
三、在项目同级目录建立mgb.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?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= "DB2Tables" targetRuntime= "MyBatis3" > <!-- 没有注释 --> <commentGenerator> <property name= "suppressAllComments" value= "true" /> </commentGenerator> <!-- 配置数据连接信息 --> <jdbcConnection driverClass= "com.mysql.jdbc.Driver" connectionURL= "jdbc:mysql://localhost:3306/user" userId= "root" password= "123456" > </jdbcConnection> <javaTypeResolver > <property name= "forceBigDecimals" value= "false" /> </javaTypeResolver> <!--指定javabean的生成位置 --> <javaModelGenerator targetPackage= "com.crud.bean" targetProject= ".\src\main\java" > <property name= "enableSubPackages" value= "true" /> <property name= "trimStrings" value= "true" /> </javaModelGenerator> <!-- 指定sql映射文件的生成位置 --> <sqlMapGenerator targetPackage= "mapper" targetProject= ".\src\main\resources" > <property name= "enableSubPackages" value= "true" /> </sqlMapGenerator> <!--指定dao接口生成的位置 --> <javaClientGenerator type= "XMLMAPPER" targetPackage= "com.crud.dao" targetProject= ".\src\main\java" > <property name= "enableSubPackages" value= "true" /> </javaClientGenerator> <!--table 指定每个表的生成策略 tablename则是自己建立的表的名字 domainObjectName 这个表生成类的名字 --> <table tableName= "tbl_emp" domainObjectName= "Employee" ></table> <table tableName= "tbl_dept" domainObjectName= "Department" ></table> </context> </generatorConfiguration> |
四、在测试类中写入方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.crud.text; import java.io.File; import java.util.ArrayList; import java.util.List; 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; public class MGBTest { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true ; //下面写入自己建立的xml文件 File configFile = new File( "mbg.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 ); } } |
刷新项目(按F5)
到此这篇关于Mybatis Plus 逆向工程介绍的文章就介绍到这了
原文链接:https://blog.csdn.net/qq_48164590/article/details/122422938