国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

SpringBoot整合mybatis簡單案例過程解析

2024-07-09 22:41:13
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了SpringBoot整合mybatis簡單案例過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

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中配置數據源信息

# DB Configationspring: 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

在連接數據庫的過程中可能會出現SQLException,可能是時區問題導致的,加上url路徑后面的“serverTimezone = GMT”即可

3.在主啟動類的同級創建po包和mapper包,在po包中創建實體類,編寫pojo;在mapper包中創建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  resultType="com.hxy.springbootdemo1.demo.pojo.MUser">    select * from user  </select></mapper>

4.手動配置mybatis的包掃描

  在主啟動類添加@MapperScan

  如果出現錯誤:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList

  有如下兩種方法解決:

  1 把映射文件 放到resources 目錄下 結構目錄一模一樣

  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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網之家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邮箱| 汶上县| 纳雍县| 博客| 松原市| 抚顺县| 清徐县| 哈密市| 盈江县| 宁安市| 上高县| 宣武区| 溧阳市| 弋阳县| 红桥区| 玉树县| 商洛市| 大渡口区| 安顺市| 永州市| 新野县| 太仓市| 桑日县| 肃北| 同仁县| 介休市| 莒南县| 浮山县| 嘉峪关市| 五峰| 舒兰市| 定陶县| 湘潭市| 甘洛县| 太保市| 沁源县| 策勒县| 郁南县| 息烽县| 绿春县| 海门市|