Mybatis-Generator 使用教程:一键自动完成增删改查

Mybatis-Generator 使用教程

项目环境:

JDK8+postgresql11+IDEA+MAVEN+SPRINGBOOT

偷懒,直接创建springboot项目mybatis-generator-demo

1、配置依赖


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.daodaogroupId>
    <artifactId>mybatis-generator-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>mybatis-generator-demoname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <mbg.version>1.3.7mbg.version>
        
        <targetJavaProject>${basedir}/src/main/javatargetJavaProject>
        <targetMapperPackage>com.daodao.mybatisgeneratordemo.daotargetMapperPackage>
        <targetModelPackage>com.daodao.mybatisgeneratordemo.pojotargetModelPackage>
        
        <targetResourcesProject>${basedir}/src/main/resourcetargetResourcesProject>
        <targetXMLPackage>mappertargetXMLPackage>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.3version>
        dependency>

        <dependency>
            <groupId>org.postgresqlgroupId>
            <artifactId>postgresqlartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>${mbg.version}version>
                <configuration>
                    
                    <verbose>trueverbose>
                    
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
                <dependencies>
                    <dependency>
                        
                        <groupId>org.postgresqlgroupId>
                        <artifactId>postgresqlartifactId>
                        <version>42.2.14version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>
project>

2、配置数据库

server.port=8080
#配置数据库信息
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://112.124.16.77:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=密码

#配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml

3、配置代码生成器文件





<generatorConfiguration>
    
    <properties resource="application.properties"/>
    
    <classPathEntry location="D:\maven\Repository\org\postgresql\postgresql\42.2.14\postgresql-42.2.14.jar"/>
    <context id="Postgresql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        
        <property name="autoDelimitKeywords" value="true"/>
        
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <commentGenerator>
            
            <property name="suppressAllComments" value="true"/>
            
            <property name="suppressDate" value="true"/>
        commentGenerator>

        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        jdbcConnection>

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

        
        <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
        
        <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
        
        <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
                             type="XMLMAPPER"/>
        
        <table tableName="teacher">
            
            <property name="useActualColumnNames" value="false"/>
        table>
    context>
generatorConfiguration>

Mybatis-Generator 使用教程:一键自动完成增删改查_第1张图片

点击运行插件即可。

看一下效果:

//实体类
package com.daodao.mybatisgeneratordemo.pojo;

public class Teacher {
    private String guid;

    private String name;

    private Long age;

    private Long salary;

    private Integer classId;

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getAge() {
        return age;
    }

    public void setAge(Long age) {
        this.age = age;
    }

    public Long getSalary() {
        return salary;
    }

    public void setSalary(Long salary) {
        this.salary = salary;
    }

    public Integer getClassId() {
        return classId;
    }

    public void setClassId(Integer classId) {
        this.classId = classId;
    }
}
//dao层
package com.daodao.mybatisgeneratordemo.dao;

import com.daodao.mybatisgeneratordemo.pojo.Teacher;
import java.util.List;

public interface TeacherMapper {
    int deleteByPrimaryKey(String guid);

    int insert(Teacher record);

    Teacher selectByPrimaryKey(String guid);

    List<Teacher> selectAll();

    int updateByPrimaryKey(Teacher record);
}


<mapper namespace="com.daodao.mybatisgeneratordemo.dao.TeacherMapper">
  <resultMap id="BaseResultMap" type="com.daodao.mybatisgeneratordemo.pojo.Teacher">
    <id column="guid" jdbcType="VARCHAR" property="guid" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="NUMERIC" property="age" />
    <result column="salary" jdbcType="NUMERIC" property="salary" />
    <result column="class_id" jdbcType="INTEGER" property="classId" />
  resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from teacher
    where guid = #{guid,jdbcType=VARCHAR}
  delete>
  <insert id="insert" parameterType="com.daodao.mybatisgeneratordemo.pojo.Teacher">
    insert into teacher (guid, `name`, age, 
      salary, class_id)
    values (#{guid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=NUMERIC}, 
      #{salary,jdbcType=NUMERIC}, #{class_id,jdbcType=INTEGER})
  insert>
  <update id="updateByPrimaryKey" parameterType="com.daodao.mybatisgeneratordemo.pojo.Teacher">
    update teacher
    set `name` = #{name,jdbcType=VARCHAR},
      age = #{age,jdbcType=NUMERIC},
      salary = #{salary,jdbcType=NUMERIC},
      class_id = #{class_id,jdbcType=INTEGER}
    where guid = #{guid,jdbcType=VARCHAR}
  update>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select guid, `name`, age, salary, class_id
    from teacher
    where guid = #{guid,jdbcType=VARCHAR}
  select>
  <select id="selectAll" resultMap="BaseResultMap">
    select guid, `name`, age, salary, class_id
    from teacher
  select>
mapper>

以上。

你可能感兴趣的:(Java)