概述
简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。
支持基本的增删改查,以及QBC风格的条件查询。
但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
• 官方文档地址 http://www.mybatis.org/generator/
• 官方工程地址 https://github.com/mybatis/generator/releases
pom.xml 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <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> |
generatorConfig.xml 配置
配置 generatorConfig.xml
放在项目的 resources
下
MBG的配置文件(重要几处配置)
1)jdbcConnection配置数据库连接信息
2)javaModelGenerator配置javaBean的生成策略
3)sqlMapGenerator 配置sql映射文件生成策略
4)javaClientGenerator配置Mapper接口的生成策略
5)table 配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<?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="/Users/zhaomin/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar" /> <context id="test" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 --> <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true --> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://192.168.0.67:3306/medical_assistant" userId="root" password="edata@Mysql123"> </jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean的生成策略 文件夹自己定义--> <javaModelGenerator targetPackage="cn.com.edata.medical.model.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql映射生成策略: --> <sqlMapGenerator targetPackage="cn.com.edata.medical.model.dao" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定mapper接口所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.edata.medical.model.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定要逆向分析哪些表:根据表要创建javaBean 可选参数如下: enableCountByExample enableUpdateByExample enableDeleteByExample enableSelectByExample selectByExampleQueryId --> <table tableName="t_classical_prescription" domainObjectName="ClassicalPrescription"></table> <table tableName="t_classical_prescription_list" domainObjectName="ClassicalPrescriptionList"></table> <table tableName="t_classical_prescription_tag" domainObjectName="ClassicalPrescriptionTag"></table> <table tableName="t_drug" domainObjectName="Drug"></table> <table tableName="t_dynasty" domainObjectName="Dynasty"></table> <table tableName="t_role" domainObjectName="Role"></table> <table tableName="t_tag" domainObjectName="Tag"></table> <table tableName="t_u2r" domainObjectName="U2R"></table> <table tableName="t_user" domainObjectName="User"></table> </context> </generatorConfiguration> |
mvn 运行生成
mvn mybatis-generator:generate -e
原创文章,转载请注明: 转载自LoserZhao – 诗和远方[ http://www.loserzhao.com/ ]
本文链接地址: http://www.loserzhao.com/java/mybatis-generator.html
文章的脚注信息由WordPress的wp-posturl插件自动生成
0 条评论。