mybatis代码生成器模板配置

1.mybatis代码生成器的介绍

代码生成器的目标就是简化单表的增删改查操作,这些标准化的流程工作,交给机器来实现,不需要程序员自己去完成。一般对一张表的操作有,根据主键查询,根据map集合查询,单条数据插入,批量插入,根据主键删除等等一系列操作,下面就让我们来配置一下代码生成器的环境,如何让mybatis自动实现代码的生成。

2.mybatis代码生成器的配置

mybatis代码生成器模板配置_第1张图片

代码生成器配置并不复杂,主要就配置有generator.xml ,生成代码的测试类 GeneratorTest,还要配置相关的依赖文件

3.数据库文件

数据库文件在配置generator.xml的时候会用到,根据数据库表生成java文件
mybatis代码生成器模板配置_第2张图片

4.pom.xml配置文件


    
      junit
      junit
      4.11
      test
    

    
    
      org.mybatis.generator
      mybatis-generator-core
      1.3.7
    

    
    
      org.mybatis
      mybatis
      3.5.6
    

    
      mysql
      mysql-connector-java
      8.0.31
    


  

5.generator.xml配置文件

注意:

1.generator.xml注意这个位置是放在和pom.xml文件的同一路径下

2.需要自己准备一个mysql驱动包,版本和数据库的版本对应


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="D:\developtools\mysqlconnection\mysql-connector-java-8.0.15.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">




        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/travel?serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="12345678">
        jdbcConnection>



        
        <javaModelGenerator targetPackage="com.tz.entity" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>




        
        <sqlMapGenerator targetPackage="mappers"  targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>



        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.tz.mapper"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>



        
        <table schema="mybatis" tableName="tab_category" domainObjectName="Category"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>

        <table schema="mybatis" tableName="tab_favorite" domainObjectName="Favorite"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>

        <table schema="mybatis" tableName="tab_route" domainObjectName="Route"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>

        <table schema="mybatis" tableName="tab_route_img" domainObjectName="RouteImg"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>

        <table schema="mybatis" tableName="tab_seller" domainObjectName="Seller"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>

        <table schema="mybatis" tableName="tab_user" domainObjectName="User"
               enableSelectByExample="false"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        table>


    context>
generatorConfiguration>

6.GeneratorTest生成代码

import org.junit.Test;
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;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GeneratorTest {
    @Test
    public void test01() throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("generator.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);
    }

}

7.完成之后的截图

mybatis代码生成器模板配置_第3张图片

你可能感兴趣的:(编程固定模板配置,mybatis,java,数据库)