mybatis-plus类的模板

mybatis-plus类的模板

前言:

之前学mybatis-plus,只是知道dao(mapper)层集成BaseMapper,但是没有想过Iservice模板的方法,以前也只是用了BaseMapper里面封装好的方法,对于service层并没有研究其API的使用,现在整理一套模板,方便以后使用

说明:

以下我生成的模板是使用Mybatis-plus逆向工程生成

mybatis-plus逆向工程具体参考:

https://blog.csdn.net/shaoming314/article/details/108321977

https://blog.csdn.net/shaoming314/article/details/109356307

以上两篇文章分别介绍了通用的myabtis-plus整合逆向工程

还详细介绍了mybatis-plus整合逆向工程,并对整合oracle数据库相关的配置

1.entity

package com.fct.njjg.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.time.LocalDate;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableField;
import java.sql.Clob;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 日志记录表
 * </p>
 *
 * @author shaoming
 * @since 2020-11-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("SYS_INFO_LOG")
@ApiModel(value="SysInfoLog对象", description="日志记录表")
public class SysInfoLog extends Model<SysInfoLog> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键ID")
    @TableId(value = "OPER_ID", type = IdType.NONE)
    private String operId;

    @ApiModelProperty(value = "功能模块")
    @TableField("OPER_MODUL")
    private String operModul;

    @ApiModelProperty(value = "操作类型")
    @TableField("OPER_TYPE")
    private String operType;

    @ApiModelProperty(value = "操作描述")
    @TableField("OPER_DESC")
    private String operDesc;

    @ApiModelProperty(value = "请求参数")
    @TableField("OPER_REQU_PARAM")
    private Clob operRequParam;

    @ApiModelProperty(value = "返回参数")
    @TableField("OPER_RESP_PARAM")
    private Clob operRespParam;

    @ApiModelProperty(value = "操作员ID")
    @TableField("OPER_USER_ID")
    private String operUserId;

    @ApiModelProperty(value = "操作原名称")
    @TableField("OPER_USER_NAME")
    private String operUserName;

    @ApiModelProperty(value = "操作方法")
    @TableField("OPER_METHOD")
    private String operMethod;

    @ApiModelProperty(value = "请求URI")
    @TableField("OPER_URI")
    private String operUri;

    @ApiModelProperty(value = "请求IP")
    @TableField("OPER_IP")
    private String operIp;

    @ApiModelProperty(value = "操作时间")
    @TableField("OPER_CREATE_TIME")
    private LocalDate operCreateTime;

    @ApiModelProperty(value = "操作版本号")
    @TableField("OPER_VAR")
    private String operVar;


    @Override
    protected Serializable pkVal() {
        return this.operId;
    }

}

2.dao(mapper)层

package com.fct.njjg.mapper;

import com.fct.njjg.pojo.entity.SysInfoLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 * 日志记录表 Mapper 接口
 * </p>
 *
 * @author shaoming
 * @since 2020-11-05
 */
public interface SysInfoLogDao extends BaseMapper<SysInfoLog> {

}

3.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="com.fct.njjg.mapper.SysInfoLogDao">

    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fct.njjg.pojo.entity.SysInfoLog">
        <id column="OPER_ID" property="operId" />
        <result column="OPER_MODUL" property="operModul" />
        <result column="OPER_TYPE" property="operType" />
        <result column="OPER_DESC" property="operDesc" />
        <result column="OPER_REQU_PARAM" property="operRequParam" />
        <result column="OPER_RESP_PARAM" property="operRespParam" />
        <result column="OPER_USER_ID" property="operUserId" />
        <result column="OPER_USER_NAME" property="operUserName" />
        <result column="OPER_METHOD" property="operMethod" />
        <result column="OPER_URI" property="operUri" />
        <result column="OPER_IP" property="operIp" />
        <result column="OPER_CREATE_TIME" property="operCreateTime" />
        <result column="OPER_VAR" property="operVar" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        OPER_ID, OPER_MODUL, OPER_TYPE, OPER_DESC, OPER_REQU_PARAM, OPER_RESP_PARAM, OPER_USER_ID, OPER_USER_NAME, OPER_METHOD, OPER_URI, OPER_IP, OPER_CREATE_TIME, OPER_VAR
    </sql>

</mapper>

4.service层

package com.fct.njjg.service;

import com.fct.njjg.pojo.entity.SysInfoLog;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * <p>
 * 日志记录表 服务类
 * </p>
 *
 * @author shaoming
 * @since 2020-11-05
 */
public interface SysInfoLogService extends IService<SysInfoLog> {

}

5.service层实现类(xxxServiceImpl)

package com.fct.njjg.service.impl;

import com.fct.njjg.pojo.entity.SysInfoLog;
import com.fct.njjg.mapper.SysInfoLogDao;
import com.fct.njjg.service.SysInfoLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 日志记录表 服务实现类
 * </p>
 *
 * @author shaoming
 * @since 2020-11-05
 */
@Service
public class SysInfoLogServiceImpl extends ServiceImpl<SysInfoLogDao, SysInfoLog> implements SysInfoLogService {

}

6.controller层

package com.fct.njjg.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 日志记录表 前端控制器
 * </p>
 *
 * @author shaoming
 * @since 2020-11-05
 */
@RestController
@RequestMapping("/sysInfoLog")
public class SysInfoLogController {

}

方法使用实例

数据库

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(80) DEFAULT NULL COMMENT '用户名',
  `password` varchar(40) DEFAULT NULL COMMENT '用户密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
package com.shaoming;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shaoming.mapper.UserMapper;
import com.shaoming.model.entity.User;
import com.shaoming.service.IUserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Calendar;
import java.util.List;

@SpringBootTest
class SpringbootTestDemoApplicationTests {
@Autowired
private IUserService iUserService;
@Autowired
private UserMapper userMapper;
    @Test
    void contextLoads() {
    }

    /**
     * 查询所有
     */
    @Test
    public void testMapper1(){
        List<User> users = userMapper.selectList(null);
        System.out.println(users);
    }
    @Test
    public void testService1(){
        List<User> list = iUserService.list(null);
        System.out.println(list);
    }

    /**
     * 按id查询
     */
    @Test
    public void testMapperSelectById(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    @Test
    public void testServiceSelectById(){
        User user = iUserService.getById(1);
        System.out.println(user);
    }

    /**
     * 新增
     */
    @Test
    public void testMapperInsert(){
        for(int i=0 ; i<10 ; i++){
        User user = new User();
        user.setId(null).setName("root"+i).setPassword("root"+i);
        userMapper.insert(user);
        }
    }
    @Test
    public void testServiceInsert(){
        for(int i=0 ; i<10 ; i++){
        User user = new User();
        user.setId(null).setName("root"+i).setPassword("root"+i);
            iUserService.save(user);
        }
    }

    /**
     * 更新
     */
    @Test
    public void testMapperUpdataById(){
        User user = new User();
        user.setId("10").setName("root1010").setPassword("root101010");
        userMapper.updateById(user);
    }

    @Test
    public void testServiceUpdateById(){
        User user = new User();
        user.setId("10").setName("root1010").setPassword("root101010");
        iUserService.updateById(user);
    }

    /**
     * 根据条件查询集合
     * MariaDB [jwt]> select * from user where name = 'root1' and password='root1';
     * +----+-------+----------+
     * | id | name  | password |
     * +----+-------+----------+
     * |  2 | root1 | root1    |
     * | 11 | root1 | root1    |
     * +----+-------+----------+
     * 2 rows in set (0.000 sec)
     */
    @Test
    public void testMapperSelectByParam(){
        /**
         * 报错
         * TooManyResultsException
         * 因为查询结果不止一条记录
         */
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//        userQueryWrapper.eq("name","root1");//报错,因为root1的记录是两个
        userQueryWrapper.eq("name","root2");
        System.out.println(userMapper.selectOne(userQueryWrapper));
    }
    @Test
    public void testMapperSelectListByParam(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name","root1");
        userQueryWrapper.eq("password","root1");
        System.out.println(userMapper.selectList(userQueryWrapper));
    }

    @Test
    public void testServiceSelectByParam(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name","root1");//报错,因为root1的记录是两个
//        userQueryWrapper.eq("name","root2");
        System.out.println(iUserService.getOne(userQueryWrapper));
    }
    @Test
    public void testServiceSelectListByParam(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name","root1");
        userQueryWrapper.eq("password","root1");
        System.out.println(iUserService.list(userQueryWrapper));
    }

    
}

总结

以上模板只是参考,具体因人而异

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值