Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Mybatis Framework in SpringBoot2

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to integrate the Mybatis framework in SpringBoot2". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to integrate the Mybatis framework in SpringBoot2.

1. Brief introduction of Mybatis Framework 1 and mybatis

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

2. Mybatis characteristics 1) sql statements are separated from code and stored in xml configuration file, which is easy to manage. 2) dynamic SQL splicing is controlled by logical tags, flexible and convenient. 3) query result set and java object are automatically mapped. 4) write original SQL, close to JDBC5) simple persistence framework, the framework is not bloated, easy to learn 3, suitable for scenarios.

MyBatis focuses on SQL itself and is a sufficiently flexible DAO-tier solution.

MyBatis will be a good choice for projects with high performance requirements or changing requirements.

2. Integration with SpringBoot2 1. Project structure diagram

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

2. Core depends on org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.github.pagehelper pagehelper 4.1.63, core configuration mybatis: # mybatis configuration file path config-location: classpath:mybatis.cfg.xml type-aliases-package: com.boot.mybatis.entity # mapper mapping file mapper-locations: classpath:mapper/*.xml4, reverse engineering generated file

No code will be posted here.

5. Write basic test interface / / add int insert (ImgInfo record); / / combine query List selectByExample (ImgInfoExample example); / modify int updateByPrimaryKeySelective (ImgInfo record); / / delete int deleteByPrimaryKey (Integer imgId); 6. Write interface to implement @ Servicepublic class ImgInfoServiceImpl implements ImgInfoService {@ Resource private ImgInfoMapper imgInfoMapper; @ Override public int insert (ImgInfo record) {return imgInfoMapper.insert (record) } @ Override public List 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 @ RestControllerpublic class ImgInfoController {@ Resource private ImgInfoService imgInfoService / / add @ RequestMapping ("/ insert") public int insert () {ImgInfo record = new ImgInfo (); record.setUploadUserId ("A123"); record.setImgTitle ("blog 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) } / / combined query @ RequestMapping ("/ selectByExample") public List 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 ("smile"); return imgInfoService.updateByPrimaryKeySelective (record);} / / Delete @ RequestMapping ("/ deleteByPrimaryKey") public int deleteByPrimaryKey () {Integer imgId = 11; return imgInfoService.deleteByPrimaryKey (imgId) }} 8, test sequence http://localhost:8010/inserthttp://localhost:8010/selectByExamplehttp://localhost:8010/updateByPrimaryKeySelectivehttp://localhost:8010/deleteByPrimaryKey 3, integrated paging plug-in 1, mybatis configuration file 2, paging implementation code @ Overridepublic PageInfo queryPage (int page,int pageSize) {PageHelper.startPage (page,pageSize); ImgInfoExample example = new ImgInfoExample () / / query condition example.createCriteria (). AndBEnableEqualTo ("1"). AndShowStateEqualTo (1); / / sort condition example.setOrderByClause ("create_date DESC,img_id ASC"); List imgInfoList = imgInfoMapper.selectByExample (example); PageInfo pageInfo = new PageInfo (imgInfoList); return pageInfo 3. The test interface http://localhost:8010/queryPage is here. I believe you have a deeper understanding of "how to integrate the Mybatis framework in SpringBoot2". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to use CSS to realize Cylindrical data report

    This article will explain in detail how to use CSS to achieve cylindrical data reports. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article. The code is as follows:

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report