您好,欢迎访问一九零五行业门户网

SpringBoot2中如何整合Mybatis框架

一、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,接近jdbc5)简单的持久化框架,框架不臃肿简单易学
3、适用场景mybatis专注于sql本身,是一个足够灵活的dao层解决方案。
对性能的要求很高,或者需求变化较多的项目,mybatis将是不错的选择。
二、与springboot2整合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、编写接口实现@servicepublic 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、控制层测试类@restcontrollerpublic 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/inserthttp://localhost:8010/selectbyexamplehttp://localhost:8010/updatebyprimarykeyselectivehttp://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、分页实现代码@overridepublic 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
以上就是springboot2中如何整合mybatis框架的详细内容。
其它类似信息

推荐信息