Mybatis框架----->(6)在SpringBoot中使用MyBatis逆向工程

注意
Mybatis逆向工程只能运用于数据库单表操作

1、在SpringBoot根目录下创建GeneratorMapper.xml

在这里插入图片描述

2、修改 GeneratorMapper.xml 配置
<?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>
    <!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="D:\javaWed\SpringBoot\mysql-connector-java-5.1.38.jar"/>
    <!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
    <context id="tables" targetRuntime="MyBatis3">
        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3366/springboot"
                        userId="root"
                        password="123456">
        </jdbcConnection>
        <!-- 生成 【model 类】,targetPackage 指定 model 类的包名, targetProject 指定
       生成的 model 放在 idea 的哪个工程下面-->
        <javaModelGenerator targetPackage="com.hcz.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>
        <!-- 生成 MyBatis 的 【Mapper.xml】 文件,targetPackage 指定 mapper.xml 文件的
       包名, targetProject 指定生成的 mapper.xml 放在 idea 的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.hcz.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- 生成 MyBatis 的 【Mapper 接口类】文件,targetPackage 指定 Mapper 接口类的包
       名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.hcz.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 数据库表名及对应的 Java 模型类名 -->
        <table tableName="t_student" domainObjectName="Student"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>


3、在pom.xml 文件中添加 mysql 反向工程依赖和Mybatis整合SpringBoot框架的起步依赖
<!--mybatis 代码自动生成插件-->
      <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>
<!--Mybatis整合SpringBoot框架的起步依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.0</version>
    </dependency>
4、双击红色选中命令,生成相关文件

在这里插入图片描述

5、出现BUILD SUCCESS表示创建成功

在这里插入图片描述

6、在src/main/java/com/hcz目录下将自动生成以下文件夹和文件

在这里插入图片描述

7、生成的Dao接口中将生成6个方法
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);
}
8、生成的对应映射文件为
<mapper namespace="com.hcz.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="com.hcz.model.Student">
    <!--
        id:标签只能修改主键字段
        result:除了主键以外的字段
        column:数据库中的字段名
        property:映射对象的属性名
        jdbcType:列中数据库字段的类型(可以省略不写)
    -->
    <!--
        resultMap的作用:
              1、当数据库中字段与实体类对象的属性名不一致时,可以进行转换
              2、当前查询的结果没有对象一个表的时候,可以自定义一个结果集
    -->
    <!--
        如果数据库中字段名由多个单词通过‘下划线’构成,
        通过Mybatis逆向工程生成对象属性会按照驼峰命名法来生成属性名称

        数据库表字段名      实体类对象属性名称
        user_name           userName
        product_type        productType
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
  </resultMap>
  <!--sql语句片段,将公共部分抽取出来-->
  <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.hcz.model.Student">
    insert into t_student (id, name, age
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
      )
  </insert>

  <!--suffixOverrides:去除多余的逗号-->
  <insert id="insertSelective" parameterType="com.hcz.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.hcz.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.hcz.model.Student">
    update t_student
    set name = #{name,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

猜你喜欢

转载自blog.csdn.net/hcz666/article/details/115314182