13.mybatis_generator(传智播客)

亢琦
2023-12-01

逆向工程:mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo类…)。
企业实际开发中,常用的逆向工程方式:由数据库表生成java代码。

需求:根据数据库的表信息自动生成po类、mapeper接口以及sql映射文件

1.添加依赖

<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.5</version>
</dependency>

2.配置文件

<?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>
	    <context id="testTables" targetRuntime="MyBatis3">
	        <!-- 去除所有生成文件的注释 -->
	        <commentGenerator>
	            <property name="suppressAllComments" value="true"/>
	            <property name="suppressDate" value="true"/>
	        </commentGenerator>
	        <!-- 1.配置数据源 -->
	        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
	                        connectionURL="jdbc:mysql://localhost:3306/mmall?characterEncoding=utf-8"
	                        userId="root"
	                        password="admin123">
	        </jdbcConnection>
	
	        <javaTypeResolver >
	            <property name="forceBigDecimals" value="false" />
	        </javaTypeResolver>
	
	        <!-- 2.生成po类的位置 -->
	        <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
	            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
	            <property name="enableSubPackages" value="false"/>
	            <!-- 是否对model添加构造函数 -->
	            <property name="constructorBased" value="true"/>
	            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
	            <property name="trimStrings" value="true"/>
	            <!-- 建立的Model对象是否不可改变 ,即生成的Model对象不会有setter方法,只有构造方法 -->
	            <property name="immutable" value="false"/>
	        </javaModelGenerator>
	
	        <!-- 3.生成sql映射文件和mapper接口的的位置 -->
	        <sqlMapGenerator targetPackage="mappers"  targetProject="./src/main/resources">
	            <property name="enableSubPackages" value="false" />
	        </sqlMapGenerator>
	
	        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao"  targetProject="./src/main/java">
	            <property name="enableSubPackages" value="false" />
	        </javaClientGenerator>
	
	        <!-- 4.配置要生成的数据库表信息 -->
	        <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	        <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
	            <columnOverride column="detail" jdbcType="VARCHAR" />
	            <columnOverride column="sub_images" jdbcType="VARCHAR" />
	        </table>
	        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	
	    </context>
	</generatorConfiguration>

在generatorConfig.xml中配置mapper生成的详细信息,注意修改以下几点配置:
1.添加要生成的数据表名称
2.生成po类的位置
3.生成sql映射文件和mapper接口的的位置

3.使用java类生成mapper接口、sql映射文件以及po类文件

public class Generator {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //需要根据实际情况修改文件路径
        File configFile = new File("/Users/wenlei/代码/mmall/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);
    }
}
 类似资料: