Home >Java >javaTutorial >How to integrate the Mybatis framework in SpringBoot2

How to integrate the Mybatis framework in SpringBoot2

WBOY
WBOYforward
2023-05-19 18:25:061360browse

1. Mybatis framework

1. Introduction to mybatis

MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and retrieval of result sets. MyBatis can use simple XML or annotations to configure and map native types, interfaces, and Java POJOs (Plain Old Java Objects, plain old Java objects) into records in the database.

2. Mybatis features

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

3. Applicable scenarios

MyBatis focuses on SQL itself and is a flexible enough DAO layer solution.
MyBatis will be a good choice for projects that have high performance requirements or have changing needs.

2. Integration with SpringBoot2

1. Project structure diagram

How to integrate the Mybatis framework in SpringBoot2

Using druid connection pool, this connection pool.

2. Core dependencies

<!-- 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. Core configuration

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

4. Files generated by reverse engineering

How to integrate the Mybatis framework in SpringBoot2

I won’t post the code here.

5. Write basic test interface

// 增加
int insert(ImgInfo record);
// 组合查询
List<img info alt="How to integrate the Mybatis framework in SpringBoot2" > selectByExample(ImgInfoExample example);
// 修改
int updateByPrimaryKeySelective(ImgInfo record);
// 删除
int deleteByPrimaryKey(Integer imgId);</imginfo>

6. Write interface implementation

@Service
public class ImgInfoServiceImpl implements ImgInfoService {
    @Resource
    private ImgInfoMapper imgInfoMapper ;
    @Override
    public int insert(ImgInfo record) {
        return imgInfoMapper.insert(record);
    }
    @Override
    public List<img info alt="How to integrate the Mybatis framework in SpringBoot2" > 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);
    }
}</imginfo>

7. Control layer test class

@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<img info alt="How to integrate the Mybatis framework in SpringBoot2" > 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);
    }
}</imginfo>

8. Test sequence

http://localhost:8010/insert
http://localhost:8010/selectByExample
http://localhost:8010/updateByPrimaryKeySelective
http://localhost:8010/deleteByPrimaryKey

3. Integrated paging plug-in

1, mybatis configuration file

<?xml  version="1.0" encoding="UTF-8" ?>
nbsp;configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <!--mybatis分页插件-->
        <plugin>
            <property></property>
        </plugin>
    </plugins>
</configuration>

2, paging implementation code

@Override
public PageInfo<img info alt="How to integrate the Mybatis framework in SpringBoot2" > 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<img info alt="How to integrate the Mybatis framework in SpringBoot2" > imgInfoList = imgInfoMapper.selectByExample(example) ;
    PageInfo<img info alt="How to integrate the Mybatis framework in SpringBoot2" > pageInfo = new PageInfo(imgInfoList) ;
    return pageInfo ;
}</imginfo></imginfo></imginfo>

3, test interface

http://localhost:8010/queryPage

The above is the detailed content of How to integrate the Mybatis framework in SpringBoot2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete