SpringBoot整合mybatis簡(jiǎn)單案例過(guò)程解析
這篇文章主要介紹了SpringBoot整合mybatis簡(jiǎn)單案例過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.在springboot項(xiàng)目中的pom.xml中添加mybatis的依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
2.在src/main/resources/application.yml中配置數(shù)據(jù)源信息
# DB Configation spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT username: root password: root # JAPConfigration jpa: database: mysql show-sql: true generate-ddl: true
在連接數(shù)據(jù)庫(kù)的過(guò)程中可能會(huì)出現(xiàn)SQLException,可能是時(shí)區(qū)問(wèn)題導(dǎo)致的,加上url路徑后面的“serverTimezone = GMT”即可
3.在主啟動(dòng)類的同級(jí)創(chuàng)建po包和mapper包,在po包中創(chuàng)建實(shí)體類,編寫pojo;在mapper包中創(chuàng)建mapper接口和mapper映射文件
mapper.java
package com.hxy.springbootdemo1.demo.mapper;
import com.hxy.springbootdemo1.demo.pojo.MUser;
import java.util.List;
public interface UserMapper {
List<MUser> getUserList();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hxy.springbootdemo1.demo.mapper.UserMapper">
<select id="getUserList" resultType="com.hxy.springbootdemo1.demo.pojo.MUser">
select * from user
</select>
</mapper>
4.手動(dòng)配置mybatis的包掃描
在主啟動(dòng)類添加@MapperScan
如果出現(xiàn)錯(cuò)誤:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList
有如下兩種方法解決:
1 把映射文件 放到resources 目錄下 結(jié)構(gòu)目錄一模一樣
2 修改配置文件pom.xml
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程解析
- SpringBoot整合MyBatis實(shí)現(xiàn)樂(lè)觀鎖和悲觀鎖的示例
- SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
- 通過(guò)Spring Boot整合Mybatis分析自動(dòng)配置詳解
- SpringBoot整合MyBatisPlus配置動(dòng)態(tài)數(shù)據(jù)源的方法
- Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫(kù)的步驟全紀(jì)錄
- springboot+springmvc+mybatis項(xiàng)目整合
相關(guān)文章
SpringMVC通過(guò)RESTful結(jié)構(gòu)實(shí)現(xiàn)頁(yè)面數(shù)據(jù)交互
RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開發(fā)方式,基于HTTP,可以使用XML格式定義或JSON格式定義。RESTFUL適用于移動(dòng)互聯(lián)網(wǎng)廠商作為業(yè)務(wù)接口的場(chǎng)景,實(shí)現(xiàn)第三方OTT調(diào)用移動(dòng)網(wǎng)絡(luò)資源的功能,動(dòng)作類型為新增、變更、刪除所調(diào)用資源2022-08-08
idea使用mybatis插件mapper中的方法爆紅的解決方案
這篇文章主要介紹了idea使用mybatis插件mapper中的方法爆紅的解決方案,文中給出了詳細(xì)的原因分析和解決方案,對(duì)大家解決問(wèn)題有一定的幫助,需要的朋友可以參考下2024-07-07
SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問(wèn)題解決
這篇文章給大家介紹了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依賴不兼容問(wèn)題,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題
這篇文章主要介紹了spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

