?MyBatis 是一個輕量級的持久化框架,用于簡化數據庫訪問和操作。它通過將 SQL 語句與 Java 代碼分離,允許開發者使用 XML 或注解來配置 SQL 語句,并將結果映射為 Java 對象。MyBatis 提供了靈活的 SQL 控制,適合需要精細控制 SQL 的場景,同時支持動態 SQL、存儲過程和緩存等功能,以滿足不同的需求?。
?整合MyBatis
數據庫安裝:
【PostgreSQL】安裝及使用(Navicat/Arcgis),連接(C#)_postgresql navicat-CSDN博客
數據庫連接
下載驅動
由于我電腦上安裝的兩個版本的Postgresql12和14所以端口得修改
添加表數據
打開編寫窗口
以下是mysql數據庫的語句
create database if not exists mybatis;use mybatis;create table user(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年齡',gender tinyint unsigned comment '性別, 1:男, 2:女',phone varchar(11) comment '手機號'
) comment '用戶表';insert into user(id, name, age, gender, phone) VALUES (null,'白眉鷹王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛獅王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龍王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');
以下是postgresql的語句(注意不要建立user表)
-- 1. 創建數據庫(兼容IF NOT EXISTS語法需調整)
CREATE DATABASE mybatis;-- 2. 連接數據庫(PostgreSQL無USE命令)
\c mybatis;-- 3. 創建表(處理自增主鍵、無符號類型、注釋語法)
CREATE TABLE "usertest" (id SERIAL PRIMARY KEY, -- 自增主鍵改造name VARCHAR(100), -- 字符串類型兼容age SMALLINT, -- TINYINT UNSIGNED → SMALLINTgender SMALLINT, -- 同age處理邏輯phone VARCHAR(11) -- 保留原定義
);
COMMENT ON TABLE "usertest" IS '用戶表'; -- 表注釋單獨設置
COMMENT ON COLUMN "usertest".id IS 'ID';
COMMENT ON COLUMN "usertest".name IS '姓名';
COMMENT ON COLUMN "usertest".age IS '年齡';
COMMENT ON COLUMN "usertest".gender IS '性別, 1:男, 2:女';
COMMENT ON COLUMN "usertest".phone IS '手機號';-- 4. 插入數據(處理自增列、單引號規范)
INSERT INTO "usertest" (name, age, gender, phone) VALUES('白眉鷹王', 55, 1, '18800000000'), -- 省略自增id [[20]][[10]]('金毛獅王', 45, 1, '18800000001'),('青翼蝠王', 38, 1, '18800000002'),('紫衫龍王', 42, 2, '18800000003'),('光明左使', 37, 1, '18800000004'),('光明右使', 48, 1, '18800000005');
添加鏡像
<!-- 配置阿里云倉庫 --><repositories><repository><id>aliyun-repos</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>aliyun-repos</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories>
添加mybatis起步依賴
在新工程上操作
<!--mybatis的起步依賴--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency>
添加數據庫的驅動依賴
<!--postgresql驅動依賴--><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency>
添加數據庫配置
spring:application:name: springboot-mybatisdatasource:driver-class-name: org.postgresql.Driverurl: jdbc:postgresql://localhost:5432/mybatisusername: postgrespassword: postgres
創建User實體類
package com.zwh.springbootmybatis.pojo;public class User {private Integer id;private String name;private Short age;private Short gender;private String phone;public User() {}public User(Integer id, String name, Short age, Short gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Short getAge() {return age;}public void setAge(Short age) {this.age = age;}public Short getGender() {return gender;}public void setGender(Short gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", phone='" + phone + '\'' +'}';}
}
創建數據庫交互接口
package com.zwh.springbootmybatis.mapper;import com.zwh.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface UserMapper {@Select("select * from user where id = #{id}")public User findById(Integer id);
}
創建業務邏輯接口?
package com.zwh.springbootmybatis.service;import com.zwh.springbootmybatis.pojo.User;public interface UserService {public User findById(Integer id);
}
創建接口的實現類?
package com.zwh.springbootmybatis.service.impl;import com.zwh.springbootmybatis.mapper.UserMapper;
import com.zwh.springbootmybatis.pojo.User;
import com.zwh.springbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User findById(Integer id) {return userMapper.findById(id);}
}
處理前端請求?
package com.zwh.springbootmybatis.Controller;import com.zwh.springbootmybatis.pojo.User;
import com.zwh.springbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/findById")public User findById(Integer id){return userService.findById(id);}
}
運行?
http://127.0.0.1:8080/findById?id=1
?
mapper
、service
、impl
、pojo
、controller講解
在Spring Boot中,mapper
、service
、impl
、pojo
、controller
是分層架構中的關鍵組件,它們分別負責不同的職責:
-
Mapper:通常指MyBatis的Mapper接口,用于定義與數據庫交互的方法。這些方法的具體實現(如SQL語句)通常放在
mapper.xml
文件中。Mapper接口通過@Mapper
注解標識,并且可以繼承BaseMapper
等通用接口。 -
Service:表示業務邏輯層的接口,定義了業務邏輯的方法。Service接口通常由
ServiceImpl
實現,實現類中會調用Mapper接口來執行數據庫操作。 -
ServiceImpl:是Service接口的實現類,負責具體的業務邏輯處理。它通常通過
@Service
注解標識,并且會注入Mapper接口以獲取數據庫操作能力。 -
Pojo(Plain Old Java Object)?:表示實體類,通常用于映射數據庫表。Pojo類包含字段和對應的getter/setter方法,用于在Java對象與數據庫之間進行數據轉換。
-
Controller:負責處理前端請求,作為應用的入口點。Controller通過
@Controller
或@RestController
注解標識,并調用Service層的方法來處理業務邏輯。
這些組件共同構成了Spring Boot的分層架構,使得代碼結構清晰、易于維護和擴展。