springboot集成mybatisplus的方法
介绍:
mybatis-plus(简称mp)是一个 mybatis 的增强工具,在 mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。()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来实现,在我的第二篇博客中有讲到
运行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还有一个强大的分页功能,如果有兴趣也可以去学习
(源码下载地址)
最后谢谢大家的观看,写博客经验不足,写得不好请见谅
上一篇: jdk在centos中安装配置图文教程
下一篇: 优化MyBatis配置文件中的配置详解
推荐阅读