日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls

發布時間:2023/12/1 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文末視頻講解


SpringBoot的版本是2.2.0

一、整合Mybatis

1-1、引入pom文件

<dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.19version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druid-spring-boot-starterartifactId> <version>1.1.23version> dependency> <dependency> <groupId>org.mybatis.spring.bootgroupId> <artifactId>mybatis-spring-boot-starterartifactId> <version>2.1.3version> dependency>

1-2、添加application.yml配置文件

添加MySql配置文件
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://ip:prot/database?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC username: root password: 123456 dbcp2: min-idle: 5 # 數據庫連接池最小維持連接數 initial-size: 5 # 初始連接數 max-total: 5 # 最大連接數 max-wait-millis: 200 # 等待鏈接獲取的最大超時時間
添加Mybatis配置文件
mybatis: type-aliases-package: com.xdx97.frame # 所有Entity別名類所在包 mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml

1-3、測試

Frame
public class Frame { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
TestMapper
import com.xdx97.frame.bean.Frame;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;@Mapperpublic interface TestMapper{ Frame selectById(@Param("idq") int id);}
TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <mapper namespace="com.xdx97.frame.mapper.TestMapper"><select id="selectById" parameterType="java.lang.Integer" resultType="com.xdx97.frame.bean.Frame"> SELECT id,name FROM frame WHERE id = #{idq} select>mapper>
TestController
import com.xdx97.frame.bean.Frame;import com.xdx97.frame.mapper.TestMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController { @Autowired private TestMapper testMapper; @GetMapping("/test") public Frame fun(){ return testMapper.selectById(1); }}

二、整合Mybatis-Plus

2-1、引入pom

<dependency> <groupId>com.baomidougroupId> <artifactId>mybatis-plus-boot-starterartifactId> <version>3.2.0version> dependency>

2-2、修改yml文件

mybatis: type-aliases-package: com.xdx97.frame # 所有Entity別名類所在包mybatis-plus: mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml
2-3、修改TestMapper

繼承BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.xdx97.frame.bean.Frame;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;@Mapperpublic interface TestMapper extends BaseMapper { Frame selectById(@Param("idq") int id);}

這樣,baseMapper里面的方法我們就可以直接使用了

三、其它

3-1、如果你需要用到枚舉,那么你需要多配置一個handle

配置位置如下,具體handle如果寫,百度一下

mybatis-plus: mapper-locations: classpath:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xml type-handlers-package:
3-2、如果引入Mbatis-Plus后出現了如下異常
Invalid bound statement (not found):

如果你的xml等一些配置關聯沒有寫錯的話,那么請考慮下面這種情況

  • 如果引用mybatis-plus-boot-starter 依賴,需要配置 mybatis-plus.mapper-locations

  • 如果引用mybatis-plus 依賴,需要配置 mybatis.mapper-locations


總結

以上是生活随笔為你收集整理的springboot 整合mybatis_SpringBoot整合Mybatis、MybatisPuls的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。