目錄
1.MyBatis是什么?
2.為什么學習MyBatis?
3. 怎么學 MyBatis
4.第?個MyBatis查詢
4.1 添加MyBatis框架支持
4.1.1老項目添加MyBatis
4.1.2 新項目添加MyBatis
4.2 配置連接字符串和MyBatis
4.2.1 配置連接字符串
4.2.2 配置 MyBatis 中的 XML 路徑
5. 使用 MyBatis 的操作模式操作數(shù)據(jù)庫
5.1 添加實體類
5.2 添加 Mapper 接口
5.3 添加 User Mapper.xml
5.4 添加 Controller
1.MyBatis是什么?
MyBatis支持自定義SQL:MyBatis允許開發(fā)者編寫自定義的SQL查詢語句,從而更好地優(yōu)化和控制數(shù)據(jù)庫訪問。與其他ORM框架不同,MyBatis不會自動創(chuàng)建SQL語句,而是將這一過程交給開發(fā)者,使得開發(fā)者能夠更好地控制數(shù)據(jù)庫查詢邏輯。
MyBatis對存儲過程支持:MyBatis支持存儲過程的調(diào)用,可以通過MyBatis執(zhí)行存儲過程,將Java對象與存儲過程進行映射,并處理存儲過程的輸出參數(shù)。
MyBatis支持高級映射:MyBatis提供了高級映射功能,允許開發(fā)者將復雜的數(shù)據(jù)庫查詢結(jié)果映射到Java對象中,從而簡化數(shù)據(jù)的處理和轉(zhuǎn)換。
MyBatis去除JDBC代碼:MyBatis屏蔽了大部分JDBC(Java Database Connectivity)代碼,減少了繁瑣的數(shù)據(jù)庫操作,開發(fā)者只需關(guān)注SQL語句的編寫和Java對象的映射。
參數(shù)設(shè)置和結(jié)果集獲?。篗yBatis簡化了參數(shù)設(shè)置和結(jié)果集獲取的過程,開發(fā)者只需在配置文件或注解中定義相應(yīng)的映射關(guān)系,MyBatis會自動將Java對象和數(shù)據(jù)庫記錄進行轉(zhuǎn)換。
XML和注解配置:MyBatis支持兩種方式來配置和映射數(shù)據(jù)庫查詢:一種是通過XML文件進行配置,另一種是通過注解來實現(xiàn)。開發(fā)者可以根據(jù)個人偏好來選擇適合的方式。
映射原始類型、接口和Java POJO:MyBatis支持將原始數(shù)據(jù)類型、Java接口以及普通Java對象(POJO)與數(shù)據(jù)庫記錄進行映射。這種靈活性使得MyBatis適用于各種場景和項目規(guī)模。
總的來說,MyBatis是一個功能強大且靈活的持久層框架,通過簡化數(shù)據(jù)庫交互和提供豐富的映射功能,它大大提高了Java開發(fā)者的生產(chǎn)效率,并被廣泛應(yīng)用于各種Java項目中。
2.為什么學習MyBatis?
對于后端開發(fā)來說程序是由以下兩部分組成:
- 后端程序
- 數(shù)據(jù)庫
在之前的學習我們大多采用JDBC的方式來實現(xiàn)后端程序與數(shù)據(jù)庫的交互,但是JDBC的流程關(guān)于繁瑣
而MyBatis能夠幫助我們更加快熟、便捷的完成后端程序與數(shù)據(jù)庫的交互!
3. 怎么學 MyBatis
學習 MyBatis 兩步走
搭建 MyBatis 開發(fā)環(huán)境
使用 MyBatis 模式和語法操作數(shù)據(jù)庫
4.第?個MyBatis查詢
開始搭建 MyBatis 之前,我們先來看?下 MyBatis 在整個框架中的定位,框架交互流程圖:
MyBatis 也是?個 ORM 框架,ORM(Object Relational Mapping),即對象關(guān)系映射。在面向?qū)ο缶幊陶Z言中,將關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)與對象建立起映射關(guān)系,進而自動的完成數(shù)據(jù)與對象的互相轉(zhuǎn)換:
- ? 將輸入數(shù)據(jù)(即傳入對象)+SQL 映射成原生SQL
- ? 將結(jié)果集映射為返回對象,即輸出對象ORM 把數(shù)據(jù)庫映射為對象
ORM 把數(shù)據(jù)庫映射為對象: ?
- ??? 數(shù)據(jù)庫表(table)--> 類(class)
- ??? 記錄(record,行數(shù)據(jù))--> 對象(object)
- ??? 字段(field) --> 對象的屬性(attribute)
?般的 ORM 框架,會將數(shù)據(jù)庫模型的每張表都映射為?個 Java 類。 也就是說使用?MyBatis 可以像操作對象?樣來操作數(shù)據(jù)庫中的表,可以實現(xiàn)對象和數(shù)據(jù)庫表之間的轉(zhuǎn)換,接下來我們來看 MyBatis 的使用吧。
4.1 添加MyBatis框架支持
添加 MyBatis 框架支持分為兩種情況:?種情況是對自己之前的 Spring 項目進行升級,另?種情況是創(chuàng)建?個全新的 MyBatis 和 Spring Boot 的項目,下面我們分別來演示這兩種情況的具體實現(xiàn)
4.1.1老項目添加MyBatis
<!-- 添加 MyBatis 框架 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<!-- 添加 MySQL 驅(qū)動 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
為什么我在這里添加了MySQL驅(qū)動呢?
MyBatis 就像?個平臺(類似淘寶),而數(shù)據(jù)庫相當于商家有很多種,不止有 MySQL,還有 SQL Server、Oracle等等.....因此這兩個都是需要添加的,對比自身所使用的數(shù)據(jù)庫來添加就行了
4.1.2 新項目添加MyBatis
我們創(chuàng)建完新的ssm項目,直接去啟動時一定會報錯, 因為你添加了數(shù)據(jù)庫依賴而沒有連接數(shù)據(jù)庫(配置數(shù)據(jù)庫的連接信息)
4.2 配置連接字符串和MyBatis
4.2.1 配置連接字符串
如果是 application.yml 添加如下內(nèi)容:
# 數(shù)據(jù)庫連接位置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mycnblog?characterEncoding=utf8&useSSL=false
username: root
password: root
# 驅(qū)動
driver-class-name: com.mysql.cj.jdbc.Driver
注意:這里的password和你數(shù)據(jù)庫的嗎密碼是一樣的!
?application.properties:
spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/mycnblog?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
#數(shù)據(jù)庫密碼
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
注意事項:如果使用?mysql-connector-java 是 5.x 之前的使用的是“com.mysql.jdbc.Driver” ,如果是大于 5.x 使用的是“com.mysql.cj.jdbc.Driver” 。
4.2.2 配置 MyBatis 中的 XML 路徑
MyBatis 組成2部分:
- 接口(表的使用操作方法,給程序其他類調(diào)用的)
- xml? (實現(xiàn)接口,寫具體SQL語句)
#mybatis 中 xml 保存路徑
mybatis:
mapper-locations:
- classpath:mybatis/**Mapper.xml
5. 使用 MyBatis 的操作模式操作數(shù)據(jù)庫
先創(chuàng)建數(shù)據(jù)庫
-- 創(chuàng)建數(shù)據(jù)庫
drop database if exists mycnblog;
create database mycnblog DEFAULT CHARACTER SET utf8mb4;
-- 使用數(shù)據(jù)
use mycnblog;
-- 創(chuàng)建表[用戶表]
drop table if exists userinfo;
create table userinfo(
id int primary key auto_increment,
username varchar(100) not null,
password varchar(32) not null,
photo varchar(500) default '',
createtime datetime default now(),
updatetime datetime default now(),
`state` int default 1
) default charset 'utf8mb4';
-- 創(chuàng)建文章表
drop table if exists articleinfo;
create table articleinfo(
id int primary key auto_increment,
title varchar(100) not null,
content text not null,
createtime datetime default now(),
updatetime datetime default now(),
uid int not null,
rcount int not null default 1,
`state` int default 1
)default charset 'utf8mb4';
-- 創(chuàng)建視頻表
drop table if exists videoinfo;
create table videoinfo(
vid int primary key,
`title` varchar(250),
`url` varchar(1000),
createtime datetime default now(),
updatetime datetime default now(),
uid int
)default charset 'utf8mb4';
-- 添加一個用戶信息
INSERT INTO `mycnblog`.`userinfo` (`id`, `username`, `password`, `photo`,
`createtime`, `updatetime`, `state`) VALUES(1, 'admin', 'admin', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1);
-- 文章添加測試數(shù)據(jù)
insert into articleinfo(title,content,uid) values('Java','Java正文',1);
-- 添加視頻
insert into videoinfo(vid,title,url,uid) values(1,'java title','http://www.baidu.com',1);
5.1 添加實體類
先添加用戶的實體類:
package com.example.demo.eneity;
import lombok.Data;
import java.time.LocalDateTime;
/**
* Created with IntelliJ IEDA.
* Description:
* User:86186
* Date:2023-08-02
* Time:16:44
*/
@Data
public class Userinfo {
private int id;
private String username;
private String password;
private String photo;
private LocalDateTime createtime;
private LocalDateTime updatetime;
private int state;
}
5.2 添加 Mapper 接口
package com.example.demo.mapper;
import com.example.demo.eneity.Userinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* Created with IntelliJ IEDA.
* Description:
* User:86186
* Date:2023-08-02
* Time:16:48
*/
@Mapper// 和五大類注解是一樣的
public interface UserMapper {
Userinfo getUserById(@Param("id") Integer id);
}
5.3 添加 User 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.example.demo.mapper.UserMapper">
<select id="getUserById" resultType="com.example.demo.eneity.Userinfo">
select * from userinfo where id=${id}
</select>
</mapper>
5.4 添加 Service文章來源:http://www.zghlxwxcb.cn/news/detail-642040.html
package com.example.demo.service;
import com.example.demo.eneity.Userinfo;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created with IntelliJ IEDA.
* Description:
* User:86186
* Date:2023-08-02
* Time:17:16
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public Userinfo getUserById(int id){
return userMapper.getUserById(id);
}
}
5.4 添加 Controller
package com.example.demo.controller;
import com.example.demo.eneity.Userinfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IEDA.
* Description:
* User:86186
* Date:2023-08-02
* Time:17:22
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getuserbyid")
public Userinfo getUserById(Integer id){
if (id == null){
return null;
}
return userService.getUserById(id);
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-642040.html
到了這里,關(guān)于MyBatis查詢數(shù)據(jù)庫之一(概念+創(chuàng)建項目+基礎(chǔ)交互)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!