使用mybatis plus操作数据库

本章将基于SOFAST框架中mybatis plus介绍如何规范操作数据库

在使用mybatis plus操作数据库之前请先了解 https://baomidou.com/(如果已经掌握可以忽略)。

需要注意: 1.因为biz-common-web中引入了so-fast-web-starter依赖,so-fast-web-starter内置了MySQL、Mybatis依赖,因此不需要再导入依赖。 2.如果项目需要使用其他数据库比如sql server则需要在项目的pom中加入Sql Server驱动包,剔除MySQL驱动。

    #加入Sql Server驱动包依赖 
          <dependency>
              <groupId>com.microsoft.sqlserver</groupId>
              <artifactId>mssql-jdbc</artifactId>
              <version>6.2.0.jre8</version>
              <scope>runtime</scope>
          </dependency>
    #剔除Mysql驱动包依赖
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.22</version>
              <exclusions>
                  <exclusion>
                      <groupId>mysql</groupId>
                      <artifactId>mysql</artifactId>
                  </exclusion>
              </exclusions>
          </dependency>

2.在nacos中将Mysql连接配置改为Sql Server连接。

简单的单表中

基础的分页查寻、根据ID查询、新增、修改、删除可以由代码生成器生成后直接使用,以上如不能满足需求可以根据需求自定义接口,例如根据title查询hello表中的所有数据:

1.在IHelloService接口中加入getContentByTitle方法(根据title查询centent)

package com.sofast.cloud.biz.hello.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sofast.cloud.biz.hello.entity.Hello;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * <p>
 *  hello服务类
 * </p>
 *
 * @author NCIT
 * @date 2022-02-08
 */
public interface IHelloService extends IService<Hello> {
    /**
     * 根据标题分页查询
     *
     * @param title 标题
     * @return Hello分页查询
     */
    String getContentByTitle(String title);
}

2.HelloServiceImpl实现类继承ServiceImpl类,并且实现IHelloService 接口重写getContentByTitle方法

需要注意:

1.构造查询条件推荐使用mybatis-plus 的LambdaQueryWrapper。(代码简洁明了,方便后期维护)

2.必须继承Servicelmpl,继承后才能使用mybatis-plus封装的方法

/*
 * Copyright (c) 2019-2029. 恩梯梯数据(中国)信息技术有限公司. All Rights Reserved.
 */

package com.sofast.cloud.biz.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sofast.cloud.biz.hello.entity.Hello;
import com.sofast.cloud.biz.hello.mapper.HelloMapper;
import com.sofast.cloud.biz.hello.service.IHelloService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sofast.cloud.core.redis.service.RedisService;
import com.sofast.cloud.sdk.upms.feign.IUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  Hello服务实现类
 * </p>
 *
 * @author NCIT
 * @date 2022-02-08
 */
@Service
public class HelloServiceImpl extends ServiceImpl<HelloMapper, Hello> implements IHelloService {
    /**
     * 根据标题分页查询
     *
     * @param title 标题
     * @return Hello分页查询
     */
    @Override
    public String getContentByTitle(String title) {

        //使用LambdaQueryWrapper构造查询条件
        LambdaQueryWrapper<Hello> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Hello::getTitle, title);
        Hello hello = getOne(queryWrapper);
        String content=hello.getContent();

        return content;

    }
}

3.HelloController继承BaseController类,定义前端访问接口调用iHelloService得到返回结果。

需要注意

1.需要满足SOFAST开发规范,返回类型为R

2.必须继承BaseController,SOFAST在该类对servlet做了封装

/*
 * Copyright (c) 2019-2029. 恩梯梯数据(中国)信息技术有限公司. All Rights Reserved.
 */

package com.sofast.cloud.biz.hello.controller;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import com.sofast.cloud.core.web.controller.BaseController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import  com.sofast.cloud.common.domain.vo.PageRequestBaseVo;
import com.sofast.cloud.common.domain.vo.R;
import com.sofast.cloud.common.constants.Constants;
import com.sofast.cloud.common.constants.MsgConstants;
import com.sofast.cloud.core.log.annotation.LogOperator;
import com.sofast.cloud.biz.hello.service.IHelloService;
import com.sofast.cloud.biz.hello.entity.Hello;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;

/**
 * <p>
 *  控制器
 * </p>
 * @description: 
 * @author: NCIT
 * @date: 2022-02-08
 */
@Api(value = "Hello操作API", tags = "Hello")
@RestController
@RequestMapping("/hello")
public class HelloController extends BaseController {

    @Autowired
    private IHelloService iHelloService;
    /**
     * 根据标题查询内容
     * @param title 标题
     * @return content 内容
     */
    @ApiOperation(value = "根据标题查询内容")
    @LogOperator(title = "根据标题查询内容", type = Constants.SELECT_OPERATOR)
    @GetMapping(value = "/getContentBytitle")
    public R<String> getContentBytitle(String title) {
       String content = iHelloService.getContentByTitle( title);
        return R.data(content);
    }
}
复杂的业务场景或多表中

同mybatis操作流程,在mapper接口中定义方法 ,在xml中写对应sql 。请参考(http://mybatis.org/spring/zh/index.html)

Copyright © 2022. 恩梯梯数据(中国)信息技术有限公司. all right reserved,powered by Gitbook该文件修订时间: 2022-02-28 10:33:58

results matching ""

    No results matching ""