当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

2019年06月19日  | 移动技术网IT编程  | 我要评论

一、mybatis框架

1、mybatis简介

mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可以使用简单的 xml 或注解来配置和映射原生类型、接口和 java 的 pojo(plain old java objects,普通老式 java 对象)为数据库中的记录。

2、mybatis特点

1)sql语句与代码分离,存放于xml配置文件中,方便管理
2)用逻辑标签控制动态sql的拼接,灵活方便
3)查询的结果集与java对象自动映射
4)编写原生态sql,接近jdbc
5)简单的持久化框架,框架不臃肿简单易学

3、适用场景

mybatis专注于sql本身,是一个足够灵活的dao层解决方案。
对性能的要求很高,或者需求变化较多的项目,mybatis将是不错的选择。

二、与springboot2.0整合

1、项目结构图


采用druid连接池,该连接池。

2、核心依赖

<!-- mybatis依赖 -->
<dependency>
    <groupid>org.mybatis.spring.boot</groupid>
    <artifactid>mybatis-spring-boot-starter</artifactid>
    <version>1.3.2</version>
</dependency>
<!-- mybatis的分页插件 -->
<dependency>
    <groupid>com.github.pagehelper</groupid>
    <artifactid>pagehelper</artifactid>
    <version>4.1.6</version>
</dependency>

3、核心配置

mybatis:
  # mybatis配置文件所在路径
  config-location: classpath:mybatis.cfg.xml
  type-aliases-package: com.boot.mybatis.entity
  # mapper映射文件
  mapper-locations: classpath:mapper/*.xml

4、逆向工程生成的文件


这里就不贴代码了。

5、编写基础测试接口

// 增加
int insert(imginfo record);
// 组合查询
list<imginfo> selectbyexample(imginfoexample example);
// 修改
int updatebyprimarykeyselective(imginfo record);
// 删除
int deletebyprimarykey(integer imgid);

6、编写接口实现

@service
public class imginfoserviceimpl implements imginfoservice {
    @resource
    private imginfomapper imginfomapper ;
    @override
    public int insert(imginfo record) {
        return imginfomapper.insert(record);
    }
    @override
    public list<imginfo> selectbyexample(imginfoexample example) {
        return imginfomapper.selectbyexample(example);
    }
    @override
    public int updatebyprimarykeyselective(imginfo record) {
        return imginfomapper.updatebyprimarykeyselective(record);
    }
    @override
    public int deletebyprimarykey(integer imgid) {
        return imginfomapper.deletebyprimarykey(imgid);
    }
}

7、控制层测试类

@restcontroller
public class imginfocontroller {
    @resource
    private imginfoservice imginfoservice ;
    // 增加
    @requestmapping("/insert")
    public int insert(){
        imginfo record = new imginfo() ;
        record.setuploaduserid("a123");
        record.setimgtitle("博文图片");
        record.setsystemtype(1) ;
        record.setimgtype(2);
        record.setimgurl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");
        record.setlinkurl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");
        record.setshowstate(1);
        record.setcreatedate(new date());
        record.setupdatedate(record.getcreatedate());
        record.setremark("知了");
        record.setbenable("1");
        return imginfoservice.insert(record) ;
    }
    // 组合查询
    @requestmapping("/selectbyexample")
    public list<imginfo> selectbyexample(){
        imginfoexample example = new imginfoexample() ;
        example.createcriteria().andremarkequalto("知了") ;
        return imginfoservice.selectbyexample(example);
    }
    // 修改
    @requestmapping("/updatebyprimarykeyselective")
    public int updatebyprimarykeyselective(){
        imginfo record = new imginfo() ;
        record.setimgid(11);
        record.setremark("知了一笑");
        return imginfoservice.updatebyprimarykeyselective(record);
    }
    // 删除
    @requestmapping("/deletebyprimarykey")
    public int deletebyprimarykey() {
        integer imgid = 11 ;
        return imginfoservice.deletebyprimarykey(imgid);
    }
}

8、测试顺序

http://localhost:8010/insert
http://localhost:8010/selectbyexample
http://localhost:8010/updatebyprimarykeyselective
http://localhost:8010/deletebyprimarykey

三、集成分页插件

1、mybatis配置文件

<?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>
    <plugins>
        <!--mybatis分页插件-->
        <plugin interceptor="com.github.pagehelper.pagehelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

2、分页实现代码

@override
public pageinfo<imginfo> querypage(int page,int pagesize) {
    pagehelper.startpage(page,pagesize) ;
    imginfoexample example = new imginfoexample() ;
    // 查询条件
    example.createcriteria().andbenableequalto("1").andshowstateequalto(1);
    // 排序条件
    example.setorderbyclause("create_date desc,img_id asc");
    list<imginfo> imginfolist = imginfomapper.selectbyexample(example) ;
    pageinfo<imginfo> pageinfo = new pageinfo<>(imginfolist) ;
    return pageinfo ;
}

3、测试接口

http://localhost:8010/querypage

四、源代码地址

github地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网