SpringBoot整合mybatis簡單案例過程解析
這篇文章主要介紹了SpringBoot整合mybatis簡單案例過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1.在springboot項目中的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ù)庫的過程中可能會出現(xiàn)SQLException,可能是時區(qū)問題導(dǎo)致的,加上url路徑后面的“serverTimezone = GMT”即可
3.在主啟動類的同級創(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.手動配置mybatis的包掃描
在主啟動類添加@MapperScan
如果出現(xiàn)錯誤: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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC通過RESTful結(jié)構(gòu)實(shí)現(xiàn)頁面數(shù)據(jù)交互
RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計風(fēng)格和開發(fā)方式,基于HTTP,可以使用XML格式定義或JSON格式定義。RESTFUL適用于移動互聯(lián)網(wǎng)廠商作為業(yè)務(wù)接口的場景,實(shí)現(xiàn)第三方OTT調(diào)用移動網(wǎng)絡(luò)資源的功能,動作類型為新增、變更、刪除所調(diào)用資源2022-08-08idea使用mybatis插件mapper中的方法爆紅的解決方案
這篇文章主要介紹了idea使用mybatis插件mapper中的方法爆紅的解決方案,文中給出了詳細(xì)的原因分析和解決方案,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-07-07SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問題解決
這篇文章給大家介紹了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依賴不兼容問題,文中通過代碼示例和圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01spring的同一定時任務(wù)上一次的任務(wù)未結(jié)束前不會啟動這次任務(wù)問題
這篇文章主要介紹了spring的同一定時任務(wù)上一次的任務(wù)未結(jié)束前不會啟動這次任務(wù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12