欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

程序员文章站 2024-02-29 23:44:22
一、添加所需依赖,当前完整的pom文件如下:

一、添加所需依赖,当前完整的pom文件如下:

<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.0</modelversion>
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>1.5.6.release</version>
 </parent>
 <groupid>org.rcddup</groupid>
 <artifactid>rcddup-custom</artifactid>
 <version>1.0.0</version>
 <properties>
  <java.version>1.8</java.version>
  <mybatis.version>1.3.1</mybatis.version>
  <mapper.version>3.4.3</mapper.version>
  <mybatis-generator.version>1.3.3</mybatis-generator.version>
  <mybatis-tk.version>1.1.4</mybatis-tk.version>
  <pagehelper.version>1.2.1</pagehelper.version>
 </properties>
 <dependencies>
  <!-- springboot - web -->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
  <!-- springboot - mybatis -->
  <dependency>
   <groupid>org.mybatis.spring.boot</groupid>
   <artifactid>mybatis-spring-boot-starter</artifactid>
   <version>${mybatis.version}</version>
  </dependency>
  <!-- springboot - mybatis 逆向工程 -->
  <dependency>
   <groupid>org.mybatis.generator</groupid>
   <artifactid>mybatis-generator-core</artifactid>
   <version>${mybatis-generator.version}</version>
  </dependency>
  <!-- mybatis 通用 mapper -->
  <dependency>
   <groupid>tk.mybatis</groupid>
   <artifactid>mapper-spring-boot-starter</artifactid>
   <version>${mybatis-tk.version}</version>
  </dependency>
  <!-- mysql -->
  <dependency>
   <groupid>mysql</groupid>
   <artifactid>mysql-connector-java</artifactid>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <!-- mybatis 逆向工程 插件 -->
   <plugin>
    <groupid>org.mybatis.generator</groupid>
    <artifactid>mybatis-generator-maven-plugin</artifactid>
    <version>${mybatis.generator.version}</version>
    <dependencies>
     <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>${mysql.version}</version>
     </dependency>
     <dependency>
      <groupid>tk.mybatis</groupid>
      <artifactid>mapper</artifactid>
      <version>${mapper.version}</version>
     </dependency>
    </dependencies>
    <configuration>
     <!-- 允许移动生成的文件 -->
     <verbose>true</verbose>
     <!-- 是否覆盖 -->
     <overwrite>true</overwrite>
     <!-- 配置文件 -->
     <configurationfile>
      ${basedir}/src/main/resources/generator/generatorconfig.xml
     </configurationfile>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

  mybtais 结合通用 mapper 非常好使,接下来看逆向工程的配置。

  有关通用mapper的信息,请查看:https://github.com/abel533/mapper

二、generatorconfig.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>
 <!-- 引入 application.properties -->
 <properties resource="application.properties" />
 <!-- mybatis3simple:不生成 example相关类及方法 -->
 <context id="mysql" targetruntime="mybatis3simple" defaultmodeltype="flat">
  <property name="beginningdelimiter" value="`" />
  <property name="endingdelimiter" value="`" />
  <!-- 指定生成 mapper 的继承模板 -->
  <plugin type="tk.mybatis.mapper.generator.mapperplugin">
   <property name="mappers" value="${generator.mappers}" />
  </plugin>
  <!-- 生成 javabean 对象重写 tostring方法 -->
  <plugin type="org.mybatis.generator.plugins.tostringplugin" />
  <!-- 生成 javabean 对象继承 serializable 类 -->
  <plugin type="org.mybatis.generator.plugins.serializableplugin" />
  <!-- 生成 javabean 对象重写 equals 和 hashcode 方法 -->
  <!-- <plugin type="org.mybatis.generator.plugins.equalshashcodeplugin" /> -->
  <!-- jdbc 连接配置 -->
  <jdbcconnection driverclass="${spring.datasource.driver-class-name}"
   connectionurl="${spring.datasource.url}"
   userid="${spring.datasource.username}"
   password="${spring.datasource.password}">
  </jdbcconnection>
  <javamodelgenerator targetpackage="${generator.javamodel-targetpackage}"
   targetproject="${generator.targetproject}" />
  <sqlmapgenerator targetpackage="${generator.sqlmap-targetpackage}" 
   targetproject="${generator.targetproject}" />
  <javaclientgenerator targetpackage="${generator.javaclient-targetpackage}"
   targetproject="${generator.targetproject}" type="xmlmapper" />
  <!-- mysql 配置 -->
  <!-- <generatedkey column="id" sqlstatement="mysql" identity="true" /> -->
  <!-- oracle 配置 -->
  <!-- <generatedkey column="id" sqlstatement="select seq_{1}.nextval from dual" identity="false" type="pre"/> -->
  <!-- tablename:数据库表名,domainobjectname:生成文件名 ,schema:数据源-->
  <table tablename="user">
   <generatedkey column="id" sqlstatement="mysql" identity="true" />
  </table>
 </context>
</generatorconfiguration>

  我们将对应的信息都在 application.properties 中定义好,在这直接使用,application.properties如下:

# generator
generator.targetproject=src/main/java
generator.mappers=org.rcddup.app.common.base.ibasemapper
generator.javamodel-targetpackage=org.rcddup.app.domain
generator.sqlmap-targetpackage=org.rcddup.app.dao
generator.javaclient-targetpackage=org.rcddup.app.dao

三、编写生成mapper的集成接口ibasemapper

package org.rcddup.app.common.base;
import tk.mybatis.mapper.common.mapper;
import tk.mybatis.mapper.common.mysqlmapper;
public interface ibasemapper<t> extends mapper<t>, mysqlmapper<t> {
}

  集成的 mapper 以及 mysqlmapper 几乎包含了你所有的 crud方法。

四、使用逆向工程生成我们的第一个文件

  右键项目 --> maven --> maven build --> goals: mybtais-generator:generate,点击 run.....

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

  我们的usermapper以及usermapper.xml就生成了。

  (1)usermapper.java

package org.rcddup.app.dao;
import org.rcddup.app.common.base.ibasemapper;
import org.rcddup.app.domain.user;
public interface usermapper extends ibasemapper<user> {
}

  (2)usermapper.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="org.rcddup.app.dao.usermapper">
 <resultmap id="baseresultmap" type="org.rcddup.app.domain.user">
 <!--
  warning - @mbg.generated
 -->
 <id column="id" jdbctype="bigint" property="id" />
 <result column="name" jdbctype="varchar" property="name" />
 <result column="age" jdbctype="tinyint" property="age" />
 <result column="status" jdbctype="tinyint" property="status" />
 </resultmap>
</mapper>

  是不是发现xml中都没有配置任何crud方法。

五、重新编写 userservice.java

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

  我们原先编写的 userservice 现在我们用新生成的 usermapper 来重新编写。

package org.rcddup.app.service.impl;
import java.util.list;
import org.rcddup.app.dao.usermapper;
import org.rcddup.app.domain.user;
import org.rcddup.app.service.iuserservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import tk.mybatis.mapper.entity.example;
@service
public class userservice implements iuserservice{
 @autowired
 private usermapper usermapper;
 @override
 public list<user> listuser() {
  return usermapper.selectall();
 }
 @override
 public user getuserbyid(long id) {
  // 方法一:适用于任何字段的查询
//  example example = new example(user.class);
//  example.createcriteria().andequalto("id", id);
//  list<user> list = usermapper.selectbyexample(example);
//  if (list != null && list.size() > 0) {
//   return list.get(0);
//  } else {
//   return null;
//  }
  // 方法二:只适用于主键字段
  return usermapper.selectbyprimarykey(id);
 }
}

六、启动 app.java

  输入:localhost:8080/user/get?id=1

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

  输入:localhost:8080/user/list

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

  好了,到这我们整合了mybatis逆向工程以及通用mapper。

总结

以上所述是小编给大家介绍的springboot整合mybatis逆向工程及 mybatis通用mapper实例详解 ,希望对大家有所帮助