Mybaits逆向工程模板

Mybatis模板

  • Mapper中的关键字
  • mybatis的逆向工程
    • 项目结构
    • pom.xml
    • generatorConfig.xml
    • log4j.properties

Mapper中的关键字

column: 数据库中定义的字段名

property:实体类中定义的属性

id:命名空间唯一标识,可以被用来引用这条语句

parameterType:将会传入这条语句的参数类的完全限定名或者别名

resultType:从这条语句要返回的期望类型的类的完全限定名或别名(这里注意下集合类型,应该是集合可以包含的类型,不能是集合本身),重要:使用resultType或resultMap,但不能同时使用。

resultMap:命名引用外部的resultMap,其名称要和外部的resultMap元素的ID名称一致,用于映射其结果到实体类指定对象中。

 <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs"
            type="com.wu.UserPO">
     <result column="address" jdbcType="LONGVARCHAR" property="address"/>
resultMap>

mysql 和 mapper对应数据类型

int         			 Long Integer       INTEGER
tinyint				Integer          		TINYINT
varchar			String					VARCHAR
datetime			Date					TIMESTAMP
timestamp									TIMESTAMP
float					

mybatis的逆向工程

Idea + Maven + mybatis

项目结构

Mybaits逆向工程模板_第1张图片

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.toolgroupId>
    <artifactId>MyBatisReverseartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.3version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.22version>
        dependency>
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.6version>
     dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.7version>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.22version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

project>

generatorConfig.xml

注意
targetProject:生成实体类的位置

Mac系统下 为./src ,windows系统下为.\src





<generatorConfiguration>
    <context id="mybatisGenerator" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test_merge?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true"
                        userId="root"
                        password="root">
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="PO"
                            targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="mapper"
                             targetProject="./src/main/java">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        
        <table tableName="user_total">table>
        

        
    context>
generatorConfiguration>

log4j.properties

log4j.appender.FILE = org.apache.log4j.FileAppender 
log4j.appender.FILE.File = file.log 
log4j.appender.FILE.Append = false 
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.ConversionPattern = [framework] % d - % c -%- 4r [ % t] %- 5p % c % x - % m % n 

src/main/java/test/Generator

package 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;

/**
 * MyBatis官方提供的逆向工程Java类
 * @author Tapan
 * 2021年月1日 16:15:47
 */
public class Generator {
     
    public void generator() throws Exception{
     
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        /**指向逆向工程的配置文件*/
        File configFile = new File("src/main/resources/generatorConfig.xml");
        ConfigurationParser parser = new ConfigurationParser(warnings);
        Configuration config = parser.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
    }
    public static void main(String[] args) throws Exception {
     
        try {
     
            Generator generatorSqlmap = new Generator();
            generatorSqlmap.generator();
        } catch (Exception e) {
     
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(环境配置搭建,mybatis,java,maven,generator)