Spring Boot Mybatis 搞反向工程,太方便咯。。

作者:山丘i

blog.csdn.net/m0_37922192/article/details/109248865

1. 拷贝 Mybatis 反向工程配置文件到项目的根目录下

2. 根据项目及表的情况,修改 GeneratorMapper.xml 配置

  • 如果使用 高版本 , 驱动类变为:com.mysql.cj.jdbc.Driver

  • url 后面应该加属性 nullCatalogMeansCurrent=true ,否则生成有问题

当前版本 MySQL 数据库为 5.7

主要根据注释来修改自己的内容

<?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>  
      
    <classPathEntry location="E:\Java\tool\maven_repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar"/>  
  
    <context id="tables" targetRuntime="MyBatis3">  
          
        <commentGenerator>  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
          
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
                        connectionURL="jdbc:mysql://localhost:3306/springboot"  
                        userId="root"  
                        password="123456">  
        </jdbcConnection>  

        <javaModelGenerator targetPackage="com.md.springboot.model"  
                            targetProject="src/main/java">  
            <property name="enableSubPackages" value="false"/>  
            <property name="trimStrings" value="false"/>  
        </javaModelGenerator>  
 
        <sqlMapGenerator targetPackage="com.md.springboot.mapper"  
                         targetProject="src/main/java">  
            <property name="enableSubPackages" value="false"/>  
        </sqlMapGenerator>  

        <javaClientGenerator type="XMLMAPPER"  
                             targetPackage="com.md.springboot.mapper" targetProject="src/main/java">  
            <property name="enableSubPackages" value="false"/>  
        </javaClientGenerator>  

        <table tableName="t_student" domainObjectName="Student"  
               enableCountByExample="false"  
               enableUpdateByExample="false"  
               enableDeleteByExample="false"  
               enableSelectByExample="false"  
               selectByExampleQueryId="false"/>  
    </context>  
</generatorConfiguration>  

此时会报错,如下

这个时候可以不用理会,项目也是会正常运行的,当然也可以这样:

添加之后就不会报红了

3. 在pom.xml 文件中添加 mysql 反向工程依赖

<build>  
    <plugins>  
          
        <plugin>  
            <groupId>org.mybatis.generator</groupId>  
            <artifactId>mybatis-generator-maven-plugin</artifactId>  
            <version>1.3.6</version>  
            <configuration>  
                  
                <configurationFile>GeneratorMapper.xml</configurationFile>  
                <verbose>true</verbose>  
                <overwrite>true</overwrite>  
            </configuration>  
        </plugin>  
    </plugins>  

</build>  

4. 双击生成相关文件

5. 生成的文件

自动生成model/Student、实体类

以及StudentMapper,接口

StudentMapper.xml 具体对数据库的操作

这样方便我们使用,具体的下面详细介绍,注意看注释

Student

package com.md.springboot.model;  
  
public class Student {  
    private Integer id;  
  
    private String name;  
  
    private Integer age;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public Integer getAge() {  
        return age;  
    }  
  
    public void setAge(Integer age) {  
        this.age = age;  
    }  
}  

StudentMapper

package com.md.springboot.mapper;  
  
import com.md.springboot.model.Student;  
  
public interface StudentMapper {  
    int deleteByPrimaryKey(Integer id);  
  
    int insert(Student record);  
  
    int insertSelective(Student record);  
  
    Student selectByPrimaryKey(Integer id);  
  
    int updateByPrimaryKeySelective(Student record);  
  
    int updateByPrimaryKey(Student record);  
}  

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="com.md.springboot.mapper.StudentMapper">  
 
  <resultMap id="BaseResultMap" type="com.md.springboot.model.Student">  
      
    <id column="id" jdbcType="INTEGER" property="id" />  
    <result column="name" jdbcType="VARCHAR" property="name" />  
    <result column="age" jdbcType="INTEGER" property="age" />  
  </resultMap>  

  <sql id="Base_Column_List">  
    id, name, age  
  </sql>  
  
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">  
    select   
    <include refid="Base_Column_List" />  
    from t_student  
    where id = #{id,jdbcType=INTEGER}  
  </select>  
  
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">  
    delete from t_student  
    where id = #{id,jdbcType=INTEGER}  
  </delete>  
  
  <insert id="insert" parameterType="com.md.springboot.model.Student">  
    insert into t_student (id, name, age  
      )  
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}  
      )  
  </insert>  
  
  <insert id="insertSelective" parameterType="com.md.springboot.model.Student">  
    insert into t_student  
    <trim prefix="(" suffix=")" suffixOverrides=",">  
      <if test="id != null">  
        id,  
      </if>  
      <if test="name != null">  
        name,  
      </if>  
      <if test="age != null">  
        age,  
      </if>  
    </trim>  
    <trim prefix="values (" suffix=")" suffixOverrides=",">  
      <if test="id != null">  
        #{id,jdbcType=INTEGER},  
      </if>  
      <if test="name != null">  
        #{name,jdbcType=VARCHAR},  
      </if>  
      <if test="age != null">  
        #{age,jdbcType=INTEGER},  
      </if>  
    </trim>  
  </insert>  
  
  
  <update id="updateByPrimaryKeySelective" parameterType="com.md.springboot.model.Student">  
    update t_student  
    <set>  
      <if test="name != null">  
        name = #{name,jdbcType=VARCHAR},  
      </if>  
      <if test="age != null">  
        age = #{age,jdbcType=INTEGER},  
      </if>  
    </set>  
    where id = #{id,jdbcType=INTEGER}  
  </update>  
  
  <update id="updateByPrimaryKey" parameterType="com.md.springboot.model.Student">  
    update t_student  
    set name = #{name,jdbcType=VARCHAR},  
      age = #{age,jdbcType=INTEGER}  
    where id = #{id,jdbcType=INTEGER}  
  </update>  
</mapper>  



好文章,我在看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值