Some usage summary about mybatis-plus

        The dao framework of mybatis-plus is used in the project. Some built-in methods are very useful, and they will be used all the time, but they will not be built and some of the principles are not well understood. Imitate it first, for future reference. (based on spring boot)

1. The pom file introduces dependencies

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

Two, application startup class file

Here is mainly the @MapperScan annotation

package com.lsl.mylsl;

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

@EnableTransactionManagement
@SpringBootApplication
@MapperScan({"com.lsl.mylsl.mapper","com.lsl.*.mylsl.mapper"})
public class MylslApplication {

    public static void main(String[] args) {
//        ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
//        System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
//        context.getBean(MyConfig.class).show();
//        System.out.println(context.getBean("propertiesConfig"));
//        System.out.println(context.getBean(MyEnvironmentPostProcessor.class));
//        System.out.println("springboot.name=" + context.getEnvironment().getProperty("springboot.name"));


//        context.close();
        SpringApplication.run(MylslApplication.class, args);
    }

}

3. Related controller, service, BO, mapper and mapper.xml

 1、controller

@CrossOrigin This is an annotation to solve cross-domain
package com.lsl.mylsl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lsl.mylsl.BO.CatBO;
import com.lsl.mylsl.service.ICatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin(allowCredentials = "true")
@RestController
@RequestMapping("/api/lsl")
public class CatController {

    @Autowired
    ICatService  catService;

    @PostMapping(value = "qryCat",produces = "application/json;charset=UTF-8")
    public String qryCatByAge(@RequestBody Map params){
        String catId = params.get("catId").toString();
        String catAge = params.get("catAge").toString();
        String catName = params.get("catName").toString();

        //mybatis-plus自带方法--查询list
        List<CatBO> catList = catService.list(new QueryWrapper<CatBO>()
                .eq("cat_id", catId).isNotNull("cat_status"));
        //mybatis-plus自带方法--查一条记录
        CatBO catBO1 = catService.getById(catId);
        //mybatis-plus自带方法--查一条记录
        CatBO catBO2 = catService.getOne(new QueryWrapper<CatBO>().eq("cat_id", catId));

        //mybatis-plus自带方法--插入一条记录
        CatBO bo = new CatBO();
        String catId1 = UUID.randomUUID().toString();
        bo.setCatId(catId1);
        bo.setCatAge("6");
        bo.setCatName("haha");
        catService.save(bo);

        //mybatis-plus自带方法--批量插入记录
        List<CatBO> listBo = new ArrayList<>();
        listBo.add(bo);
        catService.saveBatch(listBo);


        //mybatis-plus自带方法--更新
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("cat_id",catId);
        updateWrapper.set("cat_age",catAge);
        updateWrapper.set("cat_name",catName);
        catService.update(updateWrapper);

        //自己写的sql
        List<Map> mapList = catService.qryCatByAge(params);

        return "";

    }
}

2、service

package com.lsl.mylsl.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.lsl.mylsl.BO.CatBO;

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

public interface ICatService extends IService<CatBO> {

    List<Map> qryCatByAge(Map params);
}

package com.lsl.mylsl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsl.mylsl.BO.CatBO;
import com.lsl.mylsl.mapper.CatMapper;
import com.lsl.mylsl.service.ICatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CatServiceImpl extends ServiceImpl<CatMapper, CatBO> implements ICatService {

    @Autowired
    CatMapper catMapper;

    @Override
    public List<Map> qryCatByAge(Map params) {
        //返回字段为数据库字段一直,如果数据库字段是大写,则也是大写
        return catMapper.qryCatByAge(params);
    }
}

3、mapper

package com.lsl.mylsl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lsl.mylsl.BO.CatBO;
import org.springframework.stereotype.Repository;

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

@Repository
public interface CatMapper extends BaseMapper<CatBO> {

    List<Map> qryCatByAge(Map params);

    int qryCatCount(Map params);

    List<Map> qryCatByid(Map params);

    void updCatAge(Map params);
}

4、mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lsl.mylsl.mapper.CatMapper">


    <!--分页查询 -->
    <select id="qryCatByAge" resultType="java.util.Map" parameterType="java.util.Map">
        select
        *
        from
        (
        select
        QS.*,
        ROWNUM RN
        from
        (
        select * from tab_cat<where>
            <if test="catAge !=null and catAge !='' ">cat_age>#{catAge}
            </if>

            <if test="sortBy !=null and sortBy !='' ">
                order by NLSSORT($(sortBy),'NLS_SORT=SCHINESE_PINYIN_M')
            </if>

            <if test="order !=null and order !='' ">
                ${order}
            </if>


        </where>
        ) QS
        )
        where RN between (${page}-1) * ${limit} +1 and ${page}*${limit}
    </select>

    <!-- 查询记录总数-->
    <select id="qryCatCount" resultType="int" parameterType="map">
        select count(1) from tab_cat where cat_age>#{catAge}
    </select>

    <!-- 不分页查询记录-->
    <select id="qryCatByid" resultType="int" parameterType="map">
        <![CDATA[
            select * from tab_cat where cat_id>#{catId}
        ]]>
    </select>

    <!-- 更新记录-->
    <update id="updCatAge" parameterType="map">
        <![CDATA[
            update tab_cat set cat_age=#{catAge} where cat_id>#{catId}
        ]]>
    </update>

</mapper>

5、BO

package com.lsl.mylsl.BO;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;

@TableName("tab_cat")
public class CatBO implements Serializable {
    private static final long serialVersionUID = 1765207774189824729L;

    @TableId(type = IdType.UUID)
    private String catId;
    @TableField("CAT_NAME")
    private String catName;
    @TableField("CAT_AGE")
    private String catAge;

    //该字段在数据库中不存在,业务需要,不会映射到表
    @TableField(exist = false)
    private String catNum;

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String getCatAge() {
        return catAge;
    }

    public void setCatAge(String catAge) {
        this.catAge = catAge;
    }

    public String getCatNum() {
        return catNum;
    }

    public void setCatNum(String catNum) {
        this.catNum = catNum;
    }
}

4. Project structure

Guess you like

Origin blog.csdn.net/dhklsl/article/details/121831898