Mybatis逆向工程,自动生成javabean和Dao层增删改查mapper&xml

Mybatis逆向工程

Mybatis逆向工程可以自动生成javabean和Dao层

实现步骤:

  1. 引入pom依赖

    https://mvnrepository.com/ 在此网址一次搜索MySQL、MyBatis、MyBatis Generator Core,导入依赖

    	
    
    	<dependency>
    	    <groupId>org.mybatisgroupId>
    	    <artifactId>mybatisartifactId>
    	    <version>3.5.6version>
    	dependency>
    	
    	
    	
    	<dependency>
    	    <groupId>mysqlgroupId>
    	    <artifactId>mysql-connector-javaartifactId>
    	    <version>8.0.28version>
    	dependency>
    	
    	
    	
    	<dependency>
    	    <groupId>org.mybatis.generatorgroupId>
    	    <artifactId>mybatis-generator-coreartifactId>
    	    <version>1.3.7version>
    	dependency>
    
  2. 创建逆向工程的配置文件 generatorConfig.xml

    
    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="context" targetRuntime="MyBatis3">
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
            <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
            <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
                <property name="searchString" value="Example$" />
                <property name="replaceString" value="Criteria" />
            plugin>
            
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
                <property name="suppressDate" value="true" />
            commentGenerator>
            
            <jdbcConnection driverClass= "com.mysql.cj.jdbc.Driver"
                            connectionURL= "jdbc:mysql://localhost:3306/myschool?serverTimezone=UTC"
                            userId="root" password="123456">jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            javaTypeResolver>
            
            <javaModelGenerator targetPackage="com.demo.bean"
                                targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            javaModelGenerator>
            
            <sqlMapGenerator targetPackage="com.demo.mapper"
                             targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            sqlMapGenerator>
            
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.demo.mapper" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            javaClientGenerator>
            
            
            
            <table tableName="student"
                   domainObjectName="Student"
                   enableCountByExample="false"
                   enableUpdateByExample="false"
                   enableDeleteByExample="false"
                   enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="teacher"
                   domainObjectName="Teacher"
                   enableCountByExample="false"
                   enableUpdateByExample="false"
                   enableDeleteByExample="false"
                   enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
           	<table tableName="sc"
                   domainObjectName="Score"
                   enableCountByExample="false"
                   enableUpdateByExample="false"
                   enableDeleteByExample="false"
                   enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
        context>
    generatorConfiguration>
    

    注释:

    1. 数据库连接信息需要更改:数据库的账号密码,连接哪个库
    2. 生成模型的包名和位置:生成bean类的位置
    3. 生成映射文件的包名和位置:mapper.java接口位置
    4. 生成DAO的包名和位置:mapper.xml文件位置
    5. table标签:需要生成的表,只需要将数据库中的表名填写到tableName属性上,将bean中的类名填写到domainObjectName属性上即可
    6. 完成以上步骤配置完成
  3. main方法

    test中唯一修改的地方是:generatorConfig.xml配置文件的路径

    package com.dong.test;
    
    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 test {
    	public static void main(String[] args) {
    		try {
    			List<String> warnings = new ArrayList<String>();
    	        boolean overwrite = true;
    	        //实际的配置文件目录
    	        File configFile = new File("src/main/resources/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);
    	        System.out.println("逆向工程生成成功");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
  4. 运行,自动生成

    运行test,看到输出:逆向工程生成成功即完成。刷新项目,就可以看到生成的bean和Dao层了

你可能感兴趣的:(Mybatis,mybatis,xml)