Mysql+mybatis 反转生成实体类和Mapper文件以及Mapper.xml文件
来源:互联网 发布:天津加工中心编程招聘 编辑:程序博客网 时间:2024/05/16 08:41
使用之前先在pom文件中加上如下依赖:
<!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency><!-- mybatis自动生成实体类 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
这两个依赖是程序能够自动生成实体类的基础,必不可少。下面是生成步骤:
①在当前项目中建立一个generator.xml文件,这个文件名字可以自取,也不能太随意
②generator.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> <!-- <properties resource="db.properties" /> --> <classPathEntry location="E:\Developments Tools\mysql-connector-java-5.1.33.jar" /> <context id="mysql2Beans" targetRuntime="MyBatis3"> <commentGenerator> <!-- 删除代码中带有 代码生成器的注释信息 --> <property name="suppressAllComments" value="false" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="你的数据库连接" userId="登录名" password="密码" /> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.lyt.usermanage.pojo" targetProject="E:\ConvertBean"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.lyt.usermanage.mapper" targetProject="E:\ConvertBean"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.lyt.usermanage.dao" targetProject="E:\ConvertBean" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <!--无论字段是什么类型,生成的类属性都是varchar --> <!-- <table schema="btupayprod" tableName="T_INFO_MARKETING_CFG" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle"> </table> --> <!-- <columnOverride column="AFTER_AMT" jdbcType="VARCHAR" /> --> <!-- <table schema="btupayprod" tableName="t_log_online_payment" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="fasle"> 无论字段是什么类型,生成的类属性都是varchar <columnOverride column="AFTER_AMT" jdbcType="VARCHAR" /> </table> --> <table schema="test" tableName="user_third" domainObjectName="LytUserThird" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table schema="test" tableName="user_stat" domainObjectName="LytUserStat" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="user_relation" domainObjectName="LytUserRelation" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
具体配置标签请跳转:Mybatis Generator详解
注意:table有多少写多少,这是个手动活
③反转生成的两种方式
方式一:编写工具类运行
package com.lyt.usermanage.utils;import java.io.File; import java.io.IOException; import java.sql.SQLException; 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.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class MyBatisGeneratorUtils { public static void main(String[] args) { try { System.out.println("start generator ..."); List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File(MyBatisGeneratorUtils.class.getResource("/mybatis/generator.xml").getFile()); 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); System.out.println("end generator!"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
方式二:配置bat文件执行
执行完成后你就可以到你配置的生成文件保存路径去找文件了
1 0
- Mysql+mybatis 反转生成实体类和Mapper文件以及Mapper.xml文件
- mybatis反转自动生成dao、实体类和mapper.xml
- 工具类生成mybatis的Mapper类和xml文件以及实体
- mybatis 自动生成实体类、mapper.xml文件
- maven插件mybatis-generator自动生成实体类、mapper.xml、mapper.java文件
- 使用Mybatis生成工具自动生成实体类和对应的mapper映射文件以及接口文件
- mybatis自动生成mapper.xml和接口实体类工具
- Mybatis自动生成实体类、mapper.xml和dao
- 如何通过mybatis逆向工程生成实体类以及dao和mapper.xml
- Mybatis逆向生成实体类,DAO层与mapper.xml文件
- doMoreDoBetter利用mybatis生成pojo、mapper接口、mapper XML文件
- mybatis自动生成mapper xml 映射 文件
- mybatis根据实体类产生mapper文件
- 整合maven+mybatis+generator生成java自定义model实体类,dao接口和mapper映射文件
- 分离mybatis的mapper.java和mapper.xml文件
- Eclipse使用Mybatis Generator生成pojo对象、mapper接口和mapper.xml文件
- mybatis反向生成实体和xml、mapper接口
- MyBatis Mapper XML 文件 01
- 虚拟机里面的centos不能上网,ping不通网关或者外网地址b
- java学习 基础变量
- java下载压缩包简化代码
- 【ARM-LInux开发】如何运行wayland
- UILabel显示固定行数的问题
- Mysql+mybatis 反转生成实体类和Mapper文件以及Mapper.xml文件
- java 中整型与字符型的相互转换
- java interrupt()方法
- linux search.
- linux基础命令
- week3_day1_SQL SERVER
- 文章标题
- Storm集成Kafka应用的开发
- zstu 4240 极差 zstu oj 4238 Save the Princess