本篇內(nèi)容主要講解“SpringBoot2中如何整合Mybatis框架”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“SpringBoot2中如何整合Mybatis框架”吧!
成都創(chuàng)新互聯(lián)是一家專(zhuān)注于做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),蛟河網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:蛟河等地區(qū)。蛟河做網(wǎng)站價(jià)格咨詢(xún):18980820575
MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲(chǔ)過(guò)程以及高級(jí)映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡(jiǎn)單的 XML 或注解來(lái)配置和映射原生類(lèi)型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 對(duì)象)為數(shù)據(jù)庫(kù)中的記錄。
1)sql語(yǔ)句與代碼分離,存放于xml配置文件中,方便管理 2)用邏輯標(biāo)簽控制動(dòng)態(tài)SQL的拼接,靈活方便 3)查詢(xún)的結(jié)果集與java對(duì)象自動(dòng)映射 4)編寫(xiě)原生態(tài)SQL,接近JDBC 5)簡(jiǎn)單的持久化框架,框架不臃腫簡(jiǎn)單易學(xué)
MyBatis專(zhuān)注于SQL本身,是一個(gè)足夠靈活的DAO層解決方案。
對(duì)性能的要求很高,或者需求變化較多的項(xiàng)目,MyBatis將是不錯(cuò)的選擇。
采用druid連接池,該連接池。
<!-- mybatis依賴(lài) --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mybatis的分頁(yè)插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
mybatis: # mybatis配置文件所在路徑 config-location: classpath:mybatis.cfg.xml type-aliases-package: com.boot.mybatis.entity # mapper映射文件 mapper-locations: classpath:mapper/*.xml
這里就不貼代碼了。
// 增加 int insert(ImgInfo record); // 組合查詢(xún) List<ImgInfo> selectByExample(ImgInfoExample example); // 修改 int updateByPrimaryKeySelective(ImgInfo record); // 刪除 int deleteByPrimaryKey(Integer imgId);
@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); } }
@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) ; } // 組合查詢(xún) @RequestMapping("/selectByExample") public List<ImgInfo> 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); } }
http://localhost:8010/insert http://localhost:8010/selectByExample http://localhost:8010/updateByPrimaryKeySelective http://localhost:8010/deleteByPrimaryKey
<?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分頁(yè)插件--> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="MySQL"/> </plugin> </plugins> </configuration>
@Override public PageInfo<ImgInfo> queryPage(int page,int pageSize) { PageHelper.startPage(page,pageSize) ; ImgInfoExample example = new ImgInfoExample() ; // 查詢(xún)條件 example.createCriteria().andBEnableEqualTo("1").andShowStateEqualTo(1); // 排序條件 example.setOrderByClause("create_date DESC,img_id ASC"); List<ImgInfo> imgInfoList = imgInfoMapper.selectByExample(example) ; PageInfo<ImgInfo> pageInfo = new PageInfo<>(imgInfoList) ; return pageInfo ; }
http://localhost:8010/queryPage
到此,相信大家對(duì)“SpringBoot2中如何整合Mybatis框架”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱(chēng):SpringBoot2中如何整合Mybatis框架
URL標(biāo)題:http://chinadenli.net/article30/gioopo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站導(dǎo)航、服務(wù)器托管、網(wǎng)站改版、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)