码迷,mamicode.com
首页 > 编程语言 > 详细

springboot集成mybatisplus

时间:2018-04-04 20:57:21      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:password   exe   sch   seq   poj   图片   out   entity   void   

每天学习一点点 编程PDF电子书、视频教程免费下载: http://www.shitanlife.com/code

 

介绍:

         Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。(摘自mybatis-plus官网)Mybatis虽然已经给我们提供了很大的方便,但它还是有不足之处,MP的存在就是为了稍稍弥补Mybatis的不足。在我们使用Mybatis时会发现,每当要写一个业务逻辑的时候都要在DAO层写一个方法,再对应一个SQL,即使是简单的条件查询、即使仅仅改变了一个条件都要在DAO层新增一个方法,针对这个问题,MP这样一个框架,一种集Mybatis与Hibernate的优点一起的框架。它提供了Hibernate的单表CURD操作的方便同时,又保留了Mybatis的特性。

本章只教大家怎么使用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

 创建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>
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 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>
        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.chaoqi.springboot_mybatisplus.dao.domain" targetProject="src/main/java"/>
        <!-- 生成mapper xml文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <!-- 生成mapper xml对应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运行generator

技术分享图片

生成完后的项目结构如下

技术分享图片

 

五、整合mybatisplus

 

创建service接口以及service实现类

技术分享图片
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;
    }
}
技术分享图片

创建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,一些简单的sqlmybatiplus都给封装好了,节省了许多开发时间,如果是一些复杂的sql,也可以通过写原生sql来实现,在我的第二篇博客中有讲到http://www.cnblogs.com/caichaoqi/p/8580521.html

运行SpringbootMybatisplusApplication主函数

技术分享图片
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);
    }
}
技术分享图片

技术分享图片

查看数据库

技术分享图片

 下面给出mybatisplus封装的一些方法,这些方法具体怎么使用,感兴趣的小伙伴可以查看下源代码,mybatisplus还有一个强大的分页功能,如果有兴趣也可以去学习http://mp.baomidou.com

技术分享图片

(源码下载地址:https://github.com/caicahoqi/ChaoqiIsPrivateLibrary

springboot集成mybatisplus

标签:password   exe   sch   seq   poj   图片   out   entity   void   

原文地址:https://www.cnblogs.com/scode2/p/8718553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!