SpringBoot2中怎么整合Mybatis框架,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生類型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 對象)為數據庫中的記錄。
1)sql語句與代碼分離,存放于xml配置文件中,方便管理 2)用邏輯標簽控制動態SQL的拼接,靈活方便 3)查詢的結果集與java對象自動映射 4)編寫原生態SQL,接近JDBC 5)簡單的持久化框架,框架不臃腫簡單易學
MyBatis專注于SQL本身,是一個足夠靈活的DAO層解決方案。
對性能的要求很高,或者需求變化較多的項目,MyBatis將是不錯的選擇。

采用druid連接池,該連接池。
<!-- 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>
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); // 組合查詢 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) ;
}
// 組合查詢
@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分頁插件--> <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() ;
// 查詢條件
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
看完上述內容,你們掌握SpringBoot2中怎么整合Mybatis框架的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯-成都網站建設公司行業資訊頻道,感謝各位的閱讀!
網頁標題:SpringBoot2中怎么整合Mybatis框架-創新互聯
文章起源:http://www.yijiale78.com/article48/djgoep.html
成都網站建設公司_創新互聯,為您提供動態網站、品牌網站建設、微信公眾號、手機網站建設、企業網站制作、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯