天天看点

Mybatis-plus使用介绍-基于SpringBoot

源码地址:码云地址 ,推荐下载源码后,导入到idea中,在按照本篇博文进行解读

Mybatis-Plus可以简单的理解为Mybatis的加强,其在Mybatis的基础上完成了一些封装,这样开发人员在使用起来就很容易和方便

 一、创建SpringBoot工程,并引入相关依赖

<!--Spring Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot dev tools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- lombok 自动生成实体类的get/set方法,其他用法请自行查看 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
           

 二、引入mybatis-plus依赖

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

三、引入druid数据库连接池以及其他依赖

        <!--德鲁伊数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>
        <!--引入防止包没有类的异常-->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
           

更多依赖请参考代码

四、建库建表,请参考代码项目目录下的两个sql文件,建表语句如下

drop table joke;

create table joke(
id BIGINT UNSIGNED primary key auto_increment,
joke_name varchar(20),
joke_content varchar(300),
joke_type smallint unsigned,
create_time datetime  DEFAULT now(),
update_time datetime
);

drop table joke_type;

create table joke_type(
id BIGINT UNSIGNED primary key auto_increment,
type_name varchar(10),
create_time datetime DEFAULT now(),
update_time datetime
);
           

五、添加配置文件

- application.properties
- mybatis-config.xml
- logback-spring.xml
           

 六、POJO类写法

@Getter
@Setter
// 对应表名
@TableName(value="joke")
public class Joke {
    // 主键 IdType.AUTO->自增
    @TableId(value="id",type = IdType.AUTO)
    private Long id;
    // mybatis-config.xml中开启驼峰映射,会自动完成列明到属性的映射
    private String jokeName;
    private String jokeContent;
    private Integer jokeType;
    private Date createTime;
    private Date updateTime;
}
           

另外一个JokeType同理创建

七、创建Mapper接口

Mybatis-Plus中我们可能用到一个比较多的类是BaseMapper接口,其最终也是利用的Mybatis接口编程的实现机制,其**默认提供了一系列的增删改查**的基础方法,并且开发人员对于这些基础操作不需要写SQL进行处理操作

@Repository
public interface JokeMapper extends BaseMapper<Joke> {
    // 注解方式实现多表查询
    @Select("select j.id,j.joke_name,j.joke_content,j.joke_type,j.create_time,jt.type_name" +
            " from joke j,joke_type jt where j.joke_type=#{jokeType} and j.joke_type=jt.id")
    List<JokeInfoVO> selectJokeInfo(Integer jokeType);

    // mybatis方式,配置xml文件实现多表查询,后续添加JokeMapper.xml文件
    List<JokeInfoVO> selectJokeInfoXml(Integer jokeType);
}
           
@Repository
public interface JokeTypeMapper extends BaseMapper<JokeType> {
}
           

八、常见Service接口以及实现类

public interface JokeService {
    // 增
    int insertJoke(Joke joke);
    // 改
    int updateJokeById(Joke joke);
    // 删
    int deleteById(Integer id);
    // 根据id查
    Joke selectById(Integer id);
    // 根据map查,map中封装查询条件
    List<Joke> selectByMap(Map map);
    // 查询多个id的值
    List<Joke> selectByIdList(List<Integer> ids);
    // 条件封装到QueryMapper对象查询
    List<Joke> selectWrapper(Joke joke);
    // 分页查询
    IPage<Joke> selectByPage(Joke joke, int currPage, int pageSize);
    // 多表连接查询,注解方式实现,如果需要分页,请参考上一个分页查询带入进入
    // 或自己在SQL语句后面追加limit语句
    List<JokeInfoVO> selectJokeInfo(Integer jokeType);
    // 多表连接查询,XML方式实现,如果需要分页,请参考上一个分页查询带入进入
    // 或自己在SQL语句后面追加limit语句
    List<JokeInfoVO> selectJokeInfoXml(Integer jokeType);
}
           

具体实现请参考实现类,JokeServiceImpl

九、配置分页拦截器

如果不配置分页拦截器,分页是不能实现的,配置如下,详细参考代码

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            return paginationInterceptor;
        }
}
           

十、定义controller

这里没有使用单元测试去实现功能的测试,而是写了Contrller,页面中访问时,让其将结果数据作为json字符串返回显示在页面上,具体实现代码请参考代码,其中controller中参数都是写死的,自己在进行实际测试查看时,可以对controller方法进行修改,参数由前端传入即可。

十一、补充JokeMapper.xml文件

该文件在源代码resources/mapper目录下,里面只有一个多表连接查询的方法,写法就是mybatis中的实现方式

<?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.elife.mp.mapper.JokeMapper" >

    <select id="selectJokeInfoXml" resultType="jokeInfoVO">
        select j.id,j.joke_name,j.joke_content,j.joke_type,j.create_time,jt.type_name
        from joke j,joke_type jt where j.joke_type=#{jokeType} and j.joke_type=jt.id
    </select>
</mapper>