1.文件结构
二、创建数据库
1 /* 2 SQLyog Ultimate v8.32 3 MySQL - 8.0.11 : Database - gd 4 ********************************************************************* 5 */ 6 7 /*!40101 SET NAMES utf8 */; 8 9 /*!40101 SET SQL_MODE=''*/; 10 11 /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 12 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 13 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 14 /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 15 CREATE DATABASE /*!32312 IF NOT EXISTS*/`gd` /*!40100 DEFAULT CHARACTER SET utf8 */; 16 17 USE `gd`; 18 19 /*Table structure for table `association` */ 20 21 DROP TABLE IF EXISTS `association`; 22 23 CREATE TABLE `association` ( 24 `ass_id` varchar(10) NOT NULL, 25 `name` varchar(36) DEFAULT NULL, 26 `creation_time` time DEFAULT NULL, 27 `num` int(5) DEFAULT NULL, 28 PRIMARY KEY (`ass_id`) 29 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 30 31 /*Data for the table `association` */ 32 33 /*Table structure for table `student` */ 34 35 DROP TABLE IF EXISTS `student`; 36 37 CREATE TABLE `student` ( 38 `stu_id` varchar(10) NOT NULL, 39 `name` varchar(12) DEFAULT NULL, 40 `sex` varchar(6) DEFAULT NULL, 41 `age` int(3) DEFAULT NULL, 42 `birthDate` time DEFAULT NULL, 43 PRIMARY KEY (`stu_id`) 44 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 45 46 /*Data for the table `student` */ 47 48 /*Table structure for table `teacher` */ 49 50 DROP TABLE IF EXISTS `teacher`; 51 52 CREATE TABLE `teacher` ( 53 `tea_id` varchar(10) NOT NULL, 54 `name` varchar(12) DEFAULT NULL, 55 `sex` varchar(6) DEFAULT NULL, 56 `age` int(3) DEFAULT NULL, 57 `entrytime` time DEFAULT NULL, 58 PRIMARY KEY (`tea_id`) 59 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 60 61 /*Data for the table `teacher` */ 62 63 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 64 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 65 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 66 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
三、代码
1 import java.io.File; 2 import java.io.IOException; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.mybatis.generator.api.MyBatisGenerator; 7 import org.mybatis.generator.config.Configuration; 8 import org.mybatis.generator.config.xml.ConfigurationParser; 9 import org.mybatis.generator.exception.XMLParserException; 10 import org.mybatis.generator.internal.DefaultShellCallback; 11 12 public class GeneratorSqlmap { 13 14 public void generator() throws Exception{ 15 16 List<String> warnings = new ArrayList<String>(); 17 boolean overwrite = true; 18 //指定 逆向工程配置文件 19 File configFile = new File("generatorConfig.xml"); 20 ConfigurationParser cp = new ConfigurationParser(warnings); 21 Configuration config = cp.parseConfiguration(configFile); 22 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 23 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, 24 callback, warnings); 25 myBatisGenerator.generate(null); 26 27 } 28 public static void main(String[] args) throws Exception { 29 try { 30 GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); 31 generatorSqlmap.generator(); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 36 } 37 38 }
四、配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="testTables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 10 <property name="suppressAllComments" value="true" /> 11 </commentGenerator> 12 <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 13 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 14 connectionURL="jdbc:mysql://192.168.125.132:3306/gd" 15 userId="root" 16 password="123456"> 17 </jdbcConnection> 18 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 19 NUMERIC 类型解析为java.math.BigDecimal --> 20 <javaTypeResolver> 21 <property name="forceBigDecimals" value="false" /> 22 </javaTypeResolver> 23 24 <!-- targetProject:生成PO类的位置 --> 25 <javaModelGenerator targetPackage="dao.entity" 26 targetProject=".\src"> 27 <!-- enableSubPackages:是否让schema作为包的后缀 --> 28 <property name="enableSubPackages" value="false" /> 29 <!-- 从数据库返回的值被清理前后的空格 --> 30 <property name="trimStrings" value="true" /> 31 </javaModelGenerator> 32 <!-- targetProject:mapper映射文件生成的位置 --> 33 <sqlMapGenerator targetPackage="dao.mapper" 34 targetProject=".\src"> 35 <!-- enableSubPackages:是否让schema作为包的后缀 --> 36 <property name="enableSubPackages" value="false" /> 37 </sqlMapGenerator> 38 <!-- targetPackage:mapper接口生成的位置 --> 39 <javaClientGenerator type="XMLMAPPER" 40 targetPackage="dao.mapper" 41 targetProject=".\src"> 42 <!-- enableSubPackages:是否让schema作为包的后缀 --> 43 <property name="enableSubPackages" value="false" /> 44 </javaClientGenerator> 45 46 <!-- 指定数据库表 --> 47 <table schema="" tableName="student"></table> 48 <table schema="" tableName="teacher"></table> 49 </context> 50 </generatorConfiguration>
五、运行GeneratorSqlmap.java As Java Application
注意:
- 运行之前,删除之前生成的包,避免对刚产生的包及文件产生影响。
- 运行之后需要刷新。
- 如果没有刷新就再次运行会报错。