首页 > 其他 > 详细

mybatis逆向工程的使用,工具。

时间:2020-04-26 00:11:48      阅读:49      评论:0      收藏:0      [点我收藏+]

一、什么是逆向工程

MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java. po.) 。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码。

通过表生成Mapper,mapper.xml,domain

二、使用

下载地址:

http://download.csdn.net/download/monkey_wen/10020047

创建项目,并导入以下三个包:mybatis-3.2.7.jar、mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.25-bin.jar

创建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>  
<!-- 数据库驱动-->  
    <classPathEntry  location="WebContent/WEB-INF/lib/mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--数据库链接URL,用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test1" userId="root" password="123456">  
        </jdbcConnection>  
        <!-- 
                处理oracle里面的number类型的 
                BigDecimal price  === oracle num(10,2)
        -->
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->  
        <javaModelGenerator targetPackage="com.gx.domain" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="com.gx.mapping" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成Mapper的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.gx.mapper" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
        <table tableName="user" domainObjectName="User" 
        enableCountByExample="false" enableUpdateByExample="false" 
        enableDeleteByExample="false" enableSelectByExample="false" 
        selectByExampleQueryId="false"></table>
        <!-- 可添加多张表,如上 -->
        <!-- java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite -->
    </context>  
</generatorConfiguration>  

创建GeneratorSqlmap.java去生成

public class GeneratorSqlmap {
     public static void generator() throws Exception{
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            //指定 逆向工程配置文件
            File configFile = new File("config/generatorConfig.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);
        } 
    
    public static void main(String[] args) {
        try {
            generator();
            System.out.println("生成成功,请刷新");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果:

技术分享图片

三、把文件生成到其他项目里面

<?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="lib/mysql-connector-java-5.1.25-bin.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost/0412user" userId="root"
            password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置   实体
        targetProject:生成的源代码放到位置  可以是相对位置 也可以绝对位置
        targetProject=‘src’指本项目的src
        -->
        <javaModelGenerator targetPackage="com.sxt.domain"
            targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置  生成xml的位置-->
        <sqlMapGenerator targetPackage="com.sxt.mapping"
            targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成Mapper的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.sxt.mapper" targetProject="D:\Workspaces\ADV0412_mybatis\13_mybtais_erp\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
        <table tableName="sys_user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
        </table>
        <table tableName="sys_dept" domainObjectName="Dept"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
        </table>
        <table tableName="sys_emp" domainObjectName="Emp"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>  

提示:把table里面的所有属性改成true之生生成的代码,不推荐,会生成一个UserExample的实体

public interface UserMapper {
    //查询 满足example条件的数据总条数
    int countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);
    //查询 满足example条件的数据
    List<User> selectByExample(UserExample example);

    User selectByPrimaryKey(Integer id);

    //批量选择更新 满足example条件的数据总条数
    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    //批量全部更新 满足example条件的数据总条数
    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

 

mybatis逆向工程的使用,工具。

原文:https://www.cnblogs.com/97guoxiang/p/12776147.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!