溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot集成mybatisplus的方法

發(fā)布時(shí)間:2020-08-31 10:14:14 來(lái)源:腳本之家 閱讀:312 作者:豬點(diǎn)點(diǎn) 欄目:編程語(yǔ)言

介紹:

         Mybatis-Plus(簡(jiǎn)稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。(摘自mybatis-plus官網(wǎng))Mybatis雖然已經(jīng)給我們提供了很大的方便,但它還是有不足之處,MP的存在就是為了稍稍彌補(bǔ)Mybatis的不足。在我們使用Mybatis時(shí)會(huì)發(fā)現(xiàn),每當(dāng)要寫一個(gè)業(yè)務(wù)邏輯的時(shí)候都要在DAO層寫一個(gè)方法,再對(duì)應(yīng)一個(gè)SQL,即使是簡(jiǎn)單的條件查詢、即使僅僅改變了一個(gè)條件都要在DAO層新增一個(gè)方法,針對(duì)這個(gè)問(wèn)題,MP這樣一個(gè)框架,一種集Mybatis與Hibernate的優(yōu)點(diǎn)一起的框架。它提供了Hibernate的單表CURD操作的方便同時(shí),又保留了Mybatis的特性。

本章只教大家怎么使用MybatisPlus,如果想深入了解底層是怎么實(shí)現(xiàn)的可以去官網(wǎng)下載源代碼進(jìn)行解讀。

一、創(chuàng)建項(xiàng)目

  這里就不一步一步來(lái)了,我直接給出創(chuàng)建后的項(xiàng)目結(jié)構(gòu),在本章的最后我會(huì)給出源碼地址需要看效果的可以進(jìn)行下載。

springboot集成mybatisplus的方法

二、引入依賴

<?xml version="1.0" encoding="UTF-8"?>
<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>

 <groupId>com.chaoqi</groupId>
 <artifactId>springboot_mybatisplus</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springboot_mybatisplus</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.0.RELEASE</version>
  <relativePath/>
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
  </dependency>

  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <!--添加jsp依賴 -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
  </dependency>
  <!-- SpringBoot - MyBatis 逆向工程 -->
  <dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.2</version>
  </dependency>

  <!-- MyBatis 通用 Mapper -->
  <dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper-spring-boot-starter</artifactId>
   <version>1.1.4</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
     <verbose>true</verbose>
     <overwrite>true</overwrite>
    </configuration>
    <executions>
     <execution>
      <id>Generate MyBatis Artifacts</id>
      <goals>
       <goal>generate</goal>
      </goals>
     </execution>
    </executions>
    <dependencies>
     <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.2</version>
     </dependency>
     <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper</artifactId>
      <version>3.5.0</version>
     </dependency>
    </dependencies>
   </plugin>
  </plugins>
 </build>


</project>

三、編輯application.yml

server:
 port: 8080

spring:
 mvc:
 view:
  prefix: /WEB-INF/jsp/
  suffix: .jsp
 datasource:
 url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false
 username: root
 password: 123456
 driver-class-name: com.mysql.jdbc.Driver

mybatis:
 mapper-locations: classpath:mapper/*.xml
 type-aliases-package: com.chaoqi.springboot_mybatisplus.domain

四、逆向生成pojo,mapper

 創(chuàng)建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>

 <properties resource="application.yml"/>
 <classPathEntry location="D:/mysql/mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar"/>
 <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  <property name="beginningDelimiter" value="`"/>
  <property name="endingDelimiter" value="`"/>
  <property name="javaFileEncoding" value="UTF-8"/>
  <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
   <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
  </plugin>
  <!-- 注釋 -->
  <commentGenerator>
   <!-- 是否生成注釋代時(shí)間戳 -->
   <property name="suppressDate" value="true"/>
   <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
   <property name="suppressAllComments" value="false"/>
  </commentGenerator>
  <!-- JDBC連接 -->
  <jdbcConnection
    driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"
    userId="root"
    password="123456">
  </jdbcConnection>
  <!-- 生成實(shí)體類地址 -->
  <javaModelGenerator targetPackage="com.chaoqi.springboot_mybatisplus.dao.domain" targetProject="src/main/java"/>
  <!-- 生成mapper xml文件 -->
  <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
  <!-- 生成mapper xml對(duì)應(yīng)Client-->
  <javaClientGenerator targetPackage="com.chaoqi.springboot_mybatisplus.dao.mapper" targetProject="src/main/java"
        type="XMLMAPPER"/>
  <!-- 配置表信息 -->
  <table tableName="%">
   <!--mysql 配置-->
   <generatedKey column="id" sqlStatement="Mysql"/>
   <!--oracle 配置-->
   <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
  </table>
 </context>
</generatorConfiguration>

maven運(yùn)行g(shù)enerator

springboot集成mybatisplus的方法

生成完后的項(xiàng)目結(jié)構(gòu)如下

springboot集成mybatisplus的方法

五、整合mybatisplus

 創(chuàng)建service接口以及service實(shí)現(xiàn)類

package com.chaoqi.springboot_mybatisplus.service;


import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;
import java.util.List;
public interface MusicInfoService {
 public List<MusicInfo> getMusicInfo();
}

package com.chaoqi.springboot_mybatisplus.service.impl;

import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;
import com.chaoqi.springboot_mybatisplus.dao.mapper.MusicInfoMapper;
import com.chaoqi.springboot_mybatisplus.service.MusicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MusicInfoServiceImpl implements MusicInfoService {

 @Autowired
 private MusicInfoMapper musicInfoMapper;

 @Override
 public List<MusicInfo> getMusicInfo() {
  List<MusicInfo> musicInfos = musicInfoMapper.selectAll();
  return musicInfos;
 }
}

創(chuàng)建Controller

package com.chaoqi.springboot_mybatisplus.web;

import com.chaoqi.springboot_mybatisplus.dao.domain.MusicInfo;
import com.chaoqi.springboot_mybatisplus.service.MusicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/music")
public class MusicInfoController {

 @Autowired
 private MusicInfoService musicInfoService;

 @RequestMapping("/showMusic")
 public List<MusicInfo> getMusicInfo() {
  List<MusicInfo> musicInfo1 = musicInfoService.getMusicInfo();
  return musicInfo1;
 }

}

這里我的mapper并不要寫sql,一些簡(jiǎn)單的sqlmybatiplus都給封裝好了,節(jié)省了許多開(kāi)發(fā)時(shí)間,如果是一些復(fù)雜的sql,也可以通過(guò)寫原生sql來(lái)實(shí)現(xiàn),在我的第二篇博客中有講到springboot+springmvc+mybatis項(xiàng)目整合

運(yùn)行SpringbootMybatisplusApplication主函數(shù)

package com.chaoqi.springboot_mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.chaoqi.springboot_mybatisplus.dao.mapper")
public class SpringbootMybatisplusApplication {

 public static void main(String[] args) {
  SpringApplication.run(SpringbootMybatisplusApplication.class, args);
 }
}


查看數(shù)據(jù)庫(kù)

springboot集成mybatisplus的方法

下面給出mybatisplus封裝的一些方法,這些方法具體怎么使用,感興趣的小伙伴可以查看下源代碼,mybatisplus還有一個(gè)強(qiáng)大的分頁(yè)功能,如果有興趣也可以去這里學(xué)習(xí)

springboot集成mybatisplus的方法

(源碼下載地址)

最后謝謝大家的觀看,寫博客經(jīng)驗(yàn)不足,寫得不好請(qǐng)見(jiàn)諒

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI