使用Spring Boot Mybatis 搞反向工程的步骤

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>
  <!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
  <classPathEntry location="E:\Java\tool\maven_repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.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:3306/springboot"
            userId="root"
            password="123456">
    </jdbcConnection> 

    <!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
    生成的 model 放在 IDEA 的哪个工程下面-->
    <javaModelGenerator targetPackage="com.md.springboot.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.md.springboot.mapper"
             targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator> 

    <!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
    名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪个工程下面 -->
    <javaClientGenerator type="XMLMAPPER"
               targetPackage="com.md.springboot.mapper" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
    </javaClientGenerator> 

    <!-- 数据库表名及对应的 Java 模型类名,有几个表写几个table -->
    <table tableName="t_student" domainObjectName="Student"
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false"/> 

  </context>
</generatorConfiguration> 

此时会报错,如下

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

Spring Boot 理论+实战系列教程大家看这个:

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

<build>
  <plugins>
    <!--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>
  </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"> 

 <!--
  数据库字段名称   实体对象属性名称
  user_name      userName
  user_age      userAge 

 -->
 <!--
  如果数据表中的字段是多个单词构成的,通过Mybatis逆向工程生成的对象属性名称
  会按照驼峰命名法的规则生成属性名称
  自己设计数据表的时候,多个单词之前使用下划线分隔 

 --> 

 <!--
  resultMap的作用
  1. 当数据库中的字段名称和实体类对象的属性名不一致,可以进行转换
  2. 当前查询的结果对象没有对应一个表时,可以自定义一个结果集
 -->
 <resultMap id="BaseResultMap" type="com.md.springboot.model.Student">
  <!--
   id标签只能修饰主键字段,result标签修饰其他字段
   column 数据库中的字段名称
   property 映射对象的属性名称
   jdbcType 对应的类型
  -->
  <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.md.springboot.model.Student">
  insert into t_student (id, name, age
   )
  values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
   )
 </insert> 

 <!--动态sql-->
 <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> 

以上就是使用Spring Boot Mybatis 搞反向工程的步骤的详细内容,更多关于Spring Boot Mybatis 搞反向工程的资料请关注我们其它相关文章!

(0)

相关推荐

  • SpringBoot配置使Mybatis打印SQL执行时的实际参数值操作

    问题描述 在开发过程中,默认配置下SpringBoot和Mybatis正常结合运行,但在打印的日志中动态sql中的参数位置显示的是?,当sql执行违背预期时不免是因为传到Mapper中sql参数值不正确所导致,这时候如果调试能查看sql执行时占位符处的值,这无疑能让问题展示得更加直观,如何在SpringBoot.Mybatis的框架下使日志中打印出sql执行时获得的实际参数呢? 问题表现 解决办法 在SpringBoot项目中的配置文件application.properties或者bootst

  • SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解

    第一步配置yml文件 server: port: 8080 spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver thymel

  • SpringBoot整合MybatisPlus的教程详解

    Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 它已经封装好了一些crud方法,对于非常常见的一些sql我们不用写xml了,直接调用这些方法就行,但它也是支持我们自己手动写xml. 帮我们摆脱了用mybatis需要写大量的xml文件的麻烦,非常安逸哦 用过就不想用其他了,太舒服了 好了,我们开始整合整合 新建一个SpringBoot的工程 这里是我整合完一个最终的结构,可以参考一下 <?xml ve

  • SpringBoot整合Mybatis注解开发的实现代码

    官方文档: https://mybatis.org/mybatis-3/zh/getting-started.html SpringBoot整合Mybatis 引入maven依赖 (IDEA建项目的时候直接选就可以了) <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <ve

  • 解决Spring boot整合mybatis,xml资源文件放置及路径配置问题

    网上各种解决方案,我试了好久,整合了几篇文章才凑出来,在这里分享一下,实在不想网友们在这里面绕圈子,毕竟,写代码的时间是愉快的,解决bug也是愉快的,但也是一直在bug里面绕圈子就不爽了. 亲自试验: 1) 我的mapper和xml是这样子放置的 2) 在.xml中namespace是这样的: 3) application.properties中mybatis.mapper-locations得这么配置到xml 4) 最后呢,你只要在pom.xml中build下这样配置 5) 按照我这种位置防止

  • SpringBoot + Mybatis-plus实战之Mybatis-plus的一级缓存、二级缓存

    前言 现在的JAVA行业,貌似已经是SpringBoot + SpringCloud 的天下了,早期的SSH,SSM框架已经老去,与SpringBoot相结合的JPA框架虽然省去了很多的增删改查sql,但是比较笨拙,在面对一些复杂多变的逻辑时常常力不从心,而相对应的Mybatis由于其高度的灵活性受到广大JAVA攻城狮的欢迎.之前整合过了springboot+mybatis,前几天看到一个面试的问一个问题,Mybatis的一级缓存,二级缓存.我想这个应该也是一个重点吧,所以今天决定来详细解读一下

  • springboot集成mybatisPlus+多数据源的实现示例

    该项目主要实现mybatisplus.多数据源.lombok.druid的集成 主要参考 https://mp.baomidou.com/guide/quick-start.html 项目地址:https://github.com/Blankwhiter/mybatisplus-springboot release1.0 项目结构: 一.创建表以及测试数据 CREATE TABLE user ( id VARCHAR(32) NOT NULL COMMENT '主键ID', name VARCH

  • Springboot中MyBatisplus使用IPage和Page分页的实例代码

    一.需求:实现Springboot中MyBatisplus使用IPage和Page分页 二.技术:MyBatisplus的IPage和Page 三.实现 1.代码结构 2.代码详情 (1)Controller package com.xkcoding.rbac.security.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; i

  • SpringBoot整合Mybatis无法扫描xml文件的解决

    网上说是使用idea在SpringBoot整合Mybatis时候会扫描不到xml文件 1.将xml文件放在resources下 2.在application.properties中配置xml文件的扫面 补充知识:Springboot整合mybatis /*.xml路径URl does not exist问题 解决一: 在配置文件下 扫描不到 xml文件: 原来的文件: <bean id="sqlSessionFactory" class="org.mybatis.spr

  • 使用Spring Boot Mybatis 搞反向工程的步骤

    1. 拷贝 Mybatis 反向工程配置文件到项目的根目录下 2. 根据项目及表的情况,修改 GeneratorMapper.xml 配置 如果使用 高版本 , 驱动类变为:com.mysql.cj.jdbc.Driver url 后面应该加属性 nullCatalogMeansCurrent=true ,否则生成有问题 当前版本 MySQL 数据库为 5.7 主要根据注释来修改自己的内容 <?xml version="1.0" encoding="UTF-8"

  • Spring boot + mybatis + orcale实现步骤实例代码讲解

    接着上次的实现, 添加 mybatis 查询 orcale 数据库 第一步: 新建几个必须的包, 结果如下 第二步: 在service包下新建personService.java 根据名字查person方法接口 package com.example.first.service; import com.example.first.entity.Person; public interface personService { Person queryPersonByName(String name

  • Spring boot Mybatis 整合(完整版)

    本项目使用的环境: 开发工具: Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页插件 mybatis generator 自动生成代码插件 步骤: 1.创建一个springboot项目: 2.创建项目的文件结构以及jdk的版本 3.选择项目所需要的依赖 然后点击finish 5.看一下文件的结构: 6.查看一下pom.xml: <?xml version="1.0&qu

  • Spring Boot之搞定mongoTemplate的知识小结

    最近开发一些MongoDB+Spring Boot的项目,发现相较于MyBatis来说,Spring Boot对于MongoDB进行操作变化更多,所以总结一下使用mongoTemplate库的知识点,以备以后查阅方便. 首先在项目中的pom.xml配置文件中添加如下依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-

  • 详解spring boot mybatis全注解化

    本文重点给大家介绍spring boot mybatis 注解化的实例代码,具体内容大家参考下本文: pom.xml <!-- 引入mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version

  • Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)

    在上篇文章给大家介绍了Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一),接下来我们添加分页相关的依赖,时间紧张,直接上代码了,贴上我的pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=

  • Spring Boot+Mybatis的整合过程

    依赖配置 结合前面的内容,这里我们要嵌入数据库的操作,这里以操作MySQL为例整合Mybatis,首先需要在原来的基础上添加以下依赖 <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1<

  • Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)

    环境搭建 spring boot的简介 以往我们开发时用到spring总是避免不了繁琐的配置,例如我们要配置一个数据库连接,可能需要以下几步: 1.编写jdbc.properties配置文件: 2.创建spring的配置文件,加入spring配置文件前缀.配置数据库连接信息以及sqlsessionFactory等等: 3.还要在web.xml文件中加入spring的监听. springboot的出现大大简化了项目的搭建过程(spring配置以及maven配置),让我们专注于应用功能的开发,而不是

  • spring boot+mybatis 多数据源切换(实例讲解)

    由于公司业务划分了多个数据库,开发一个项目会同事调用多个库,经过学习我们采用了注解+aop的方式实现的 1.首先定义一个注解类 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface TargetDataSource { String value();//此处接收的是数据源的名称 } 2.然后建一个配置类,这个在项目启动时会加载数据源,一开始采用了HikariCP,查资料说是最快性能最好的

  • Spring + Spring Boot + MyBatis + MongoDB的整合教程

    前言 我之前是学Spring MVC的,后面听同学说Spring Boot挺好用,极力推荐我学这个鬼.一开始,在网上找Spring Boot的学习资料,他们博文写得不是说不好,而是不太详细. 我就在想我要自己写一篇尽可能详细的文章出来,下面话不多说了,来一看看详细的介绍吧. 技术栈 Spring Spring Boot MyBatis MongoDB MySQL 设计模式 MVC 功能 注册(用户完成注册后是默认未激活的,程序有个定时器在检测没有激活的用户,然后发一次邮件提醒用户激活) 登录 发

随机推荐