springboot整合mybatis的超詳細(xì)過(guò)程(配置模式+注解模式)
一.簡(jiǎn)單介紹
1.配置相關(guān)的依賴
2.配置模式
3寫.mapper、controller、service
4.配置yaml文件 配置mybatis全局配置文件
(這里我使用的是配置模式+注解模式所以需要配置全局文件)
二具體配置
2.1.配置相關(guān)的依賴.
當(dāng)然也可以在創(chuàng)建springboot的時(shí)候勾選對(duì)應(yīng)的功能
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!--mybatis整合springboot起步依賴--> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
2.2 寫.mapper、controller、service
在寫這個(gè)之前話要寫基本的pojo
pojo相關(guān) 這里使用了 lombok
package com.xbfinal.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class ssmdb { private Integer id; private String name; private String type; private String description; }
2.2.1mapper文件
話不多說(shuō)注意代碼的注釋
package com.xbfinal.mapper; import com.xbfinal.pojo.ssmdb; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface ssmdbMapper { //更具id查對(duì)應(yīng)的書(shū)用注解模式 @Select("select * from tbl_book where id=#{id}") public ssmdb getById(int id); //查詢所有的書(shū) ,這里我們用配置模式 //配置模式我個(gè)人喜歡用來(lái)寫復(fù)制的sql語(yǔ)句(狗頭) public List<ssmdb> getAll(); }
2.2.2service文件
一般用來(lái)實(shí)現(xiàn)mapper的 直接看代碼
package com.xbfinal.service; import com.xbfinal.mapper.ssmdbMapper; import com.xbfinal.pojo.ssmdb; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ser { @Autowired ssmdbMapper ssmdbMapper; public ssmdb getById(int id){ return ssmdbMapper.getById(id); } public List<ssmdb> getAll(){ return ssmdbMapper.getAll(); } }
2.2.2controller文件
package com.xbfinal.controller; import com.xbfinal.pojo.ssmdb; import com.xbfinal.service.ser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class controller01 { @Autowired ser ser; @RequestMapping("/id") public String to01(){ final ssmdb byId = ser.getById(1); return byId.toString(); } @RequestMapping("/all") public String to02(){ // final ssmdb byId = ser.getById(1); final List<ssmdb> all = ser.getAll(); return all.toString(); } }
2.3配置相關(guān)文件
1.寫mysql的文件。寫在application.yaml文件中
spring: datasource: password: 0615 username: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
2.由于用配置模式+注解模式所以需要配置mybatis全局文件
在static文件下創(chuàng)建mybatis文件夾然后創(chuàng)建配置文件如下
<?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> <!--基本的東西都不用配置,因?yàn)閟pringboot配好了--> </configuration>
接著在mapper文件下寫mybatis對(duì)應(yīng)的mapper配置
<?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.xbfinal.mapper.ssmdbMapper"> <!--public List<ssmdb> getAll();--> <select id="getAll" resultType="com.xbfinal.pojo.ssmdb"> select * from tbl_book </select> </mapper>
最后在yaml文件中配好mybatis
mybatis: config-location: classpath:static/mybatis/mybatis-config.xml mapper-locations: classpath:static/mybatis/mapper/*.xml
三、結(jié)果截圖
數(shù)據(jù)庫(kù)
四、可能遇到的報(bào)錯(cuò)
SpringBoot連接數(shù)據(jù)庫(kù)報(bào)錯(cuò):Access denied for user ‘root‘@‘localhost‘ (using password: YES)
解決方案
檢查自己的mysql配置是否正確如果正確嘗試一下把密碼加上""如圖
到此這篇關(guān)于springboot整合mybatis(配置模式+注解模式)的文章就介紹到這了,更多相關(guān)springboot整合mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot下mybatis-plus開(kāi)啟打印sql日志的配置指南
- springboot mybatis druid配置多數(shù)據(jù)源教程
- springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
- SpringBoot整合Mybatis,解決TypeAliases配置失敗的問(wèn)題
- springboot項(xiàng)目整合mybatis并配置mybatis中間件的實(shí)現(xiàn)
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot-MyBatis配置-配置端口號(hào)與服務(wù)路徑(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)
相關(guān)文章
深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問(wèn)題
這篇文章主要介紹了解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07java設(shè)計(jì)模式之適配器模式(Adapter)
這篇文章主要介紹了java設(shè)計(jì)模式之適配器模式Adapter的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

迅速掌握J(rèn)ava容器中常用的ArrayList類與Vector類用法

Mybatis配置之typeAlias標(biāo)簽的用法