Spring Boot 2.X:集成 mybatis-plus 高效开发

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 2.X:集成 mybatis-plus 高效开发相关的知识,希望对你有一定的参考价值。

参考技术A 本文将介绍 mybats-plus 的常用实例,简化常规的 CRUD 操作。

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

继承 BaseMapper,T表示对应实体类

在启动内添加 @MapperScan 就不用再 UserDao 商用 @Mapper 注解。

本文介绍了 mybatis-plus 相关的 Mapper层 CRUD 接口实现,其还提供了 Service层 CRUD 的相关接口,有兴趣的小伙伴可以去使用下。 mybatis-plus 真正地提升了撸码效率。

其他学习要点:

spring boot 与 Mybatis整合(*)

  1. 在pom.xml文件中加入数据库、spring-mybatis整合
    <!-- spring boot 整合mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <!-- MYSQL -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
  2. 在springboot的启动类中添加注解
    package cn.java.controller;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /*
     * spring boot 启动程序
     */
    @SpringBootApplication(scanBasePackages= {"cn.java.controller","cn.java.service.impl"})
    @EnableAutoConfiguration
    //配置mapper层扫描
    @MapperScan(basePackages="cn.java.mapper")
    public class StartApplication {
        public static void main(String[] args) {
            SpringApplication.run(StartApplication.class, args);
        }
    
    }

     

 

控制层GoodController 

package cn.java.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.java.service.GoodService;

@Controller
@RequestMapping("/goods/")
public class GoodController {
    @Autowired
    private GoodService goodService;
    //获取goods表中所有数据
    @RequestMapping("/selectAllGoods.do")
    @ResponseBody
    public List<Map<String,Object>> selectAllGoods(){
        return goodService.getAll();
    }
}

 

 业务层

package cn.java.service;

import java.util.List;
import java.util.Map;

public interface GoodService {

    List<Map<String, Object>> getAll();

}
package cn.java.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.java.mapper.GoodMapper;
import cn.java.service.GoodService;
@Service
public class GoodServiceImpl implements GoodService {
    /*
     * 获取springboot库下goods表中所有的数据 
     */
    @Autowired
    private GoodMapper goodMapper;
    
    /*
     * 查询所有商品
     */
    
    /* (non-Javadoc)
     * @see cn.java.service.impl.GoodService#getAll()
     */
    @Override
    public List<Map<String,Object>> getAll(){
        return goodMapper.getAllGoods();
    }
}

DAO层

package cn.java.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Select;

public interface GoodMapper {
    @Select("SELECT * FROM goods")
    public List<Map<String,Object>> getAllGoods();
}

application.properties

#update tomcat port
server.port=8888

#config datasource(mysql)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=78978r

实体类

package cn.java.entity;

public class Good {
    private String goodName;
    private Float goodPrice;
    private String goodPhone;
    private String goodAddress;
    private Integer goodNum;
    
    public String getGoodName() {
        return goodName;
    }
    public void setGoodName(String goodName) {
        this.goodName = goodName;
    }
    public Float getGoodPrice() {
        return goodPrice;
    }
    public void setGoodPrice(Float goodPrice) {
        this.goodPrice = goodPrice;
    }
    public String getGoodPhone() {
        return goodPhone;
    }
    public void setGoodPhone(String goodPhone) {
        this.goodPhone = goodPhone;
    }
    public String getGoodAddress() {
        return goodAddress;
    }
    public void setGoodAddress(String goodAddress) {
        this.goodAddress = goodAddress;
    }
    public Integer getGoodNum() {
        return goodNum;
    }
    public void setGoodNum(Integer goodNum) {
        this.goodNum = goodNum;
    }
    
    

}

 

数据库springboot

CREATE TABLE `springboot`.`goods`(  
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT \'主键\',
  `good_name` VARCHAR(40) COMMENT \'商品名称\',
  `good_price` FLOAT(10,2) COMMENT \'商品单价\',
  `good_phone` VARCHAR(20) COMMENT \'商品联系电话\',
  `good_address` VARCHAR(40) COMMENT \'商品地址\',
  `good_num` INT COMMENT \'商品库存\',
  PRIMARY KEY (`id`)
);

 

 GoodMapper.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="cn.java.mapper.GoodMapper">
    <resultMap id="BaseResultMap" type="cn.java.entity.Good">
    <id column="id" property="id" jdbcType="BIGINT"/>
    <id column="good_name" property="goodName" jdbcType="VARCHAR"/>
    <id column="good_price" property="goodPrice" jdbcType="REAL"/>
    <id column="good_phone" property="goodPhone" jdbcType="VARCHAR"/>
    <id column="good_address" property="goodAddress" jdbcType="VARCHAR"/>
    <id column="good_num" property="goodNum" jdbcType="INTEGER"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,good_name,good_price_good_phone,good_address,good_num
    </sql>
    
    <!-- 查询所有 -->
    <select id="selectAllGoods" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"></include> FROM goods;
    </select>
    
    
    <!-- <select id="selectByPrimarKey" resultMap="BaseResultMap" parmeterType="java.lang.Long">
        select
        <include refid="Base_Column_List">
            from goods
            where id = #{id,jdbcType=BIGINT}
        </include>
    </select>
    <delete id="deleteByPrimaryKey" parmeterType="java.lang.Long">
        delete from goods
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" parmeterType="cn.java.entity.Good">
        insert into goods(id,good_name,good_price,good_phone,good_address,good_num
        )
        values(#{id,jdbcType=BIGINT},#{goodName,jdbcType=VARCHAR},#{goodPrice,jdbcType=REAL},#{goodPhone,jdbcType=VARCHAR},
        #{goodAddress,jdbcType=VARCHAR},#{goodNum,jdbcType=INTEGER}
        )
    </insert>
    <insert id="insertSelective" parmeterType="cn.java.entity.Good">
    insert into goods
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
    id,
    </if>
    <if test="goodName != null">
    good_name,
    </if>
    <if test="goodPrice != null">
    good_price,
    </if>
    <if test="goodPhone != null">
    good_phone,
    </if>
    <if test="goodAddress != null">
    good_address,
    </if>
    <if test="goodNum != null">
    good_num,
    </if>
    </trim>    
    <trim prefix="values(" suffix=")" suffixOverrides=",">
    <if test="id != null">
        #{id,jdbcType=BIHINT},
    </if>
    <if test="goodName != null">
        #{goodName,jdbcType=VARCHAR},
    </if>
    <if test="goodPrice != null">
        #{goodPrice,jdbcType=REAL},
    </if>
    <if test="goodPhone != null">
        #{goodPhone,jdbcType=VARCHAR},
    </if>
    <if test="goodAddress != null">
        #{goodAddress,jdbcType=VARCHAR},
    </if>
    <if test="goodNum != null">
        #{goodNum,jdbcType=INTEGER},
    </if>
    </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" prameterType="cn.java.entity.Good">
    update goods
    set    good_name = #{goodName,jdbcType=VARCHAR},
        good_price = #{goodName,jdbcType=VARCHAR},
        good_phone = #{goodPhone,jdbcType=VARCHAR},
        good_address = #{goodAddress,jdbcType=VARCHAR},
        good_num = #{goodNum,jdbcType=INTEGER}
        where id = #{id,jdbcType=BIHINT}
    </update> -->
    
</mapper>

 (3)当MyBatis  语句较为复杂时  使用xml更加方便

#config mybatis xml
mybatis.mapper-location=classpath:mapping/*.xml
mybatis.type-aliases-package=cn.java.entity

 

 控制层与业务层  业务层与DAO层 通过接口连接

 

以上是关于Spring Boot 2.X:集成 mybatis-plus 高效开发的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 集成 mybatis

Spring Boot集成Mybatis完整实例

spring boot集成mybatis

spring boot 和 mybatis集成

Spring Boot 集成MyBatis

Spring Boot2:使用Spring Boot2集成Mybatis缓存机制