Home> Java> javaTutorial> body text

How to integrate the Mybatis framework in SpringBoot2

WBOY
Release: 2023-05-19 18:25:06
forward
1277 people have browsed it

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)简单的持久化框架,框架不臃肿简单易学
Copy after login

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

  org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2    com.github.pagehelper pagehelper 4.1.6 
Copy after login

3. Core configuration

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

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); // 组合查询 ListHow to integrate the Mybatis framework in SpringBoot2 selectByExample(ImgInfoExample example); // 修改 int updateByPrimaryKeySelective(ImgInfo record); // 删除 int deleteByPrimaryKey(Integer imgId);
Copy after login

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 ListHow 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); } }
Copy after login

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 ListHow 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); } }
Copy after login

8. Test sequence

http://localhost:8010/insert http://localhost:8010/selectByExample http://localhost:8010/updateByPrimaryKeySelective http://localhost:8010/deleteByPrimaryKey
Copy after login

3. Integrated paging plug-in

1, mybatis configuration file

 nbsp;configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">        
Copy after login

2, paging implementation code

@Override public PageInfoHow 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"); ListHow to integrate the Mybatis framework in SpringBoot2 imgInfoList = imgInfoMapper.selectByExample(example) ; PageInfoHow to integrate the Mybatis framework in SpringBoot2 pageInfo = new PageInfo(imgInfoList) ; return pageInfo ; }
Copy after login

3, test interface

http://localhost:8010/queryPage
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!