springboot整合mybatis-plus

1.pom.xml添加依赖

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.3.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>jsqlparser</artifactId>
                    <groupId>com.github.jsqlparser</groupId>
                </exclusion>
            </exclusions>
</dependency>

 

2.application.yml配置,部分配置根据自己项目而定

# MyBatis配置
mybatis:
  # 搜索指定包别名。mapper.xml中用到一些自定义POJO,你可以用完全限定名来指定这些POJO的引用
  typeAliasesPackage: com.klx.**.pojo.po
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 加载全局的配置文件
  configLocation: classpath:mybatis/mybatis-config.xml

 

3.mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 全局的配置 -->
	<settings>
		<setting name="cacheEnabled"             value="true" />  <!-- 全局映射器启用缓存 -->
		<setting name="useGeneratedKeys"         value="true" />  <!-- 允许 JDBC 支持自动生成主键 -->
		<setting name="defaultExecutorType"      value="REUSE" /> <!-- 配置默认的执行器 -->
		<!--
		指定 MyBatis 所用日志的具体实现
		MyBatis的默认输出顺序是SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING
		-->
		<setting name="logImpl"                  value="SLF4J" />
<!--		<setting name="logImpl" 				 value="org.apache.ibatis.logging.stdout.StdOutImpl" />-->
		<setting name="jdbcTypeForNull"          value="NULL"/>
		<setting name="callSettersOnNulls"       value="true"/>
		<setting name="lazyLoadingEnabled"       value="true"/>   <!-- 启用延迟加载功能 -->
		<setting name="aggressiveLazyLoading"    value="false"/>  <!-- 按需要延迟加载 -->
		<setting name="defaultFetchSize"         value="3000"/>
		<setting name="mapUnderscoreToCamelCase" value="true"/>   <!--驼峰式命名-->
<!--		<setting name="show_sql"                 value="true"/>-->
<!--		<setting name="format_sql"               value="true"/>-->
	</settings>

	<typeAliases>
		<typeAlias alias="Integer" type="java.lang.Integer" />
		<typeAlias alias="Long" type="java.lang.Long" />
		<typeAlias alias="Map" type="java.util.Map" />
		<typeAlias alias="HashMap" type="java.util.HashMap" />
		<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
		<typeAlias alias="ArrayList" type="java.util.ArrayList" />
		<typeAlias alias="LinkedList" type="java.util.LinkedList" />
	</typeAliases>

</configuration>

 

4.测试

(1)controller

package com.klx.demo.controller.lesson;

import com.klx.demo.pojo.dto.lesson.AppLessonDto;
import com.klx.demo.pojo.vo.lesson.AppLessonVo;
import com.klx.demo.service.AppLessonService;
import com.klx.demo.utils.AjaxResult;
import com.klx.demo.utils.PageBean;
import com.klx.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 课程表(AppLesson)表控制层
 *
 * @author klx
 * @since 2021-05-08 10:33:09
 */

@RestController
@RequestMapping("/api/lesson/appLesson")
@Slf4j
@Api(description = "课程表(AppLesson)表控制层", value = "AppLessonController")
public class AppLessonController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private AppLessonService appLessonService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", paramType = "header", dataType = "String", value = "token令牌", required = true)
    })
    public AjaxResult list(AppLessonDto appLessonDto) {

        Object keyObject = redisUtil.getCacheObject("key");
        if (Objects.isNull(keyObject)) {
            redisUtil.setCacheObject("key", "nba", 2, TimeUnit.MINUTES);
        } else {
            log.warn(keyObject.toString());
        }

        // 开始查询
        PageBean<List<AppLessonVo>> listPageBean = appLessonService.selectAppLessonList(appLessonDto);
        return AjaxResult.success(listPageBean);
    }

}

 

(2)service

package com.klx.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.klx.demo.pojo.dto.lesson.AppLessonDto;
import com.klx.demo.pojo.po.lesson.AppLessonPo;
import com.klx.demo.pojo.vo.lesson.AppLessonVo;
import com.klx.demo.utils.PageBean;

import java.util.List;

/**
 * 课程表(AppLesson)表服务接口
 *
 * @author klx
 * @since 2021-05-08 10:33:08
 */
public interface AppLessonService extends IService<AppLessonPo> {

    PageBean<List<AppLessonVo>> selectAppLessonList(AppLessonDto appLessonDto);

}

 

(3)serviceImpl

package com.klx.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.klx.demo.mapper.lesson.AppLessonMapper;
import com.klx.demo.pojo.dto.lesson.AppLessonDto;
import com.klx.demo.pojo.po.lesson.AppLessonPo;
import com.klx.demo.pojo.vo.lesson.AppLessonVo;
import com.klx.demo.service.AppLessonService;
import com.klx.demo.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 课程表(AppLesson)表服务实现类
 *
 * @author klx
 * @since 2021-05-08 10:33:09
 */
@Service("appLessonService")
public class AppLessonServiceImpl extends ServiceImpl<AppLessonMapper, AppLessonPo> implements AppLessonService {

    @Autowired
    private AppLessonMapper appLessonMapper;


    @Override
    public PageBean<List<AppLessonVo>> selectAppLessonList(AppLessonDto appLessonDto) {
        List<AppLessonVo> appLessonVos = appLessonMapper.selectAppLessonList(appLessonDto);
        PageBean<List<AppLessonVo>> listPageBean = new PageBean<>(appLessonVos);
        listPageBean.setTotal(appLessonVos.size());
        return listPageBean;
    }

}

 

(4)Mapper.java

package com.klx.demo.mapper.lesson;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.klx.demo.pojo.dto.lesson.AppLessonDto;
import com.klx.demo.pojo.po.lesson.AppLessonPo;
import com.klx.demo.pojo.vo.lesson.AppLessonVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 课程表(AppLesson)表数据库访问层
 *
 * @author klx
 * @since 2021-05-08 10:33:08
 */

@Mapper
@Repository
public interface AppLessonMapper extends BaseMapper<AppLessonPo> {

    List<AppLessonVo> selectAppLessonList(@Param("appLessonDto") AppLessonDto appLessonDto);

}

 

(5)Mapper.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.klx.demo.mapper.lesson.AppLessonMapper">
    <resultMap type="com.klx.demo.pojo.po.lesson.AppLessonPo" id="AppLessonMap">
        <id property="lessonId" column="lesson_id" jdbcType="INTEGER"/>
        <result property="lessonTitle" column="lesson_title" jdbcType="VARCHAR"/>
        <result property="lessonSummary" column="lesson_summary" jdbcType="VARCHAR"/>
        <result property="imgUrl" column="img_url" jdbcType="VARCHAR"/>
        <result property="dictType" column="dict_type" jdbcType="VARCHAR"/>
        <result property="dictCode" column="dict_code" jdbcType="INTEGER"/>
        <result property="lessonScore" column="lesson_score" jdbcType="NUMERIC"/>
        <result property="viewTotal" column="view_total" jdbcType="INTEGER"/>
        <result property="favoriteTotal" column="favorite_total" jdbcType="INTEGER"/>
        <result property="orderNum" column="order_num" jdbcType="INTEGER"/>
        <result property="stateNum" column="state_num" jdbcType="INTEGER"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="OTHER"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="OTHER"/>
        <result property="deleteBy" column="delete_by" jdbcType="VARCHAR"/>
        <result property="deleteTime" column="delete_time" jdbcType="OTHER"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="unlockUserIds" column="unlock_user_ids" jdbcType="OTHER"/>
    </resultMap>

    <select id="selectAppLessonList" resultType="com.klx.demo.pojo.vo.lesson.AppLessonVo">

                select * from app_lesson where state_num=1

    </select>
</mapper>

 

posted @ 2021-06-08 17:08  詹姆溪  阅读(58)  评论(0编辑  收藏  举报