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
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
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.