SpringBoot 2.0 Base Case (10): Integrate Mybatis framework, integrate Paging Assistant plug-in

Keywords: Java Mybatis SQL xml

I. Mybatis Framework

1. Introduction to mybatis

MyBatis is an excellent persistence layer framework that supports customization of SQL, stored procedures, and advanced mappings.MyBatis avoids almost all JDBC code and setting parameters manually and getting result sets.MyBatis can use simple XML or annotations to configure and map native types, interfaces, and Java POJO s (Plain Old Java Objects, plain old Java objects) as records in the database.

2. Characteristics of mybatis

1) sql statements are separated from code and stored in xml configuration file for easy management
 2) Use logical tags to control splicing of dynamic SQL for flexibility and convenience
 3) Query result set automatically maps to java objects
 4) Write native SQL, close to JDBC
 5) Simple persistent framework, simple and easy to learn

3. Scenarios applicable

MyBatis focuses on SQL itself and is a flexible enough DAO-tier solution.
MyBatis is a good choice for projects with high performance requirements or more variable requirements.

Integration with SpringBoot 2.0

1. Project Structure Diagram

Use a druid connection pool, which is the connection pool.

2. Core Dependency

<!-- mybatis rely on -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- mybatis Paging Plugin -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

3. Core Configuration

mybatis:
  # The path to the mybatis configuration file
  config-location: classpath:mybatis.cfg.xml
  type-aliases-package: com.boot.mybatis.entity
  # mapper map file
  mapper-locations: classpath:mapper/*.xml

4. Files generated by reverse engineering

No more code here.

5. Write the basic test interface

// increase
int insert(ImgInfo record);
// Combinatorial Query
List<ImgInfo> selectByExample(ImgInfoExample example);
// modify
int updateByPrimaryKeySelective(ImgInfo record);
// delete
int deleteByPrimaryKey(Integer imgId);

6. Writing 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<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. Control Layer Test Class

@RestController
public class ImgInfoController {
    @Resource
    private ImgInfoService imgInfoService ;
    // increase
    @RequestMapping("/insert")
    public int insert(){
        ImgInfo record = new ImgInfo() ;
        record.setUploadUserId("A123");
        record.setImgTitle("Bowen Picture");
        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("Cicada");
        record.setbEnable("1");
        return imgInfoService.insert(record) ;
    }
    // Combinatorial Query
    @RequestMapping("/selectByExample")
    public List<ImgInfo> selectByExample(){
        ImgInfoExample example = new ImgInfoExample() ;
        example.createCriteria().andRemarkEqualTo("Cicada") ;
        return imgInfoService.selectByExample(example);
    }
    // modify
    @RequestMapping("/updateByPrimaryKeySelective")
    public int updateByPrimaryKeySelective(){
        ImgInfo record = new ImgInfo() ;
        record.setImgId(11);
        record.setRemark("Know a smile");
        return imgInfoService.updateByPrimaryKeySelective(record);
    }
    // delete
    @RequestMapping("/deleteByPrimaryKey")
    public int deleteByPrimaryKey() {
        Integer imgId = 11 ;
        return imgInfoService.deleteByPrimaryKey(imgId);
    }
}

8. Test Order

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

3. Integrated Paging Plugin

1. mybatis configuration file

<?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 jPaginate-->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

2. Paging implementation code

@Override
public PageInfo<ImgInfo> queryPage(int page,int pageSize) {
    PageHelper.startPage(page,pageSize) ;
    ImgInfoExample example = new ImgInfoExample() ;
    // query criteria
    example.createCriteria().andBEnableEqualTo("1").andShowStateEqualTo(1);
    // sort criteria
    example.setOrderByClause("create_date DESC,img_id ASC");
    List<ImgInfo> imgInfoList = imgInfoMapper.selectByExample(example) ;
    PageInfo<ImgInfo> pageInfo = new PageInfo<>(imgInfoList) ;
    return pageInfo ;
}

3. Test Interface

http://localhost:8010/queryPage

4. Source code address

GitHub Address: Know a smile
https://github.com/cicadasmile
 Code Cloud Address: Know a smile
https://gitee.com/cicadasmile

Posted by BillyBoB on Fri, 16 Aug 2019 09:38:12 -0700