文章目录
- 一、什么是MyBatis
- 二、MyBatis开发环境配置
- 1.创建数据库表
- 2.添加MyBatis框架支持
- 3.配置连接字符串和MyBatis
- 4.添加业务代码流程
 
 
一、什么是MyBatis
MyBatis是一种持久层框架,也是一种ORM框架(Object Relational Mapping即对象关系映射)。
其中是持久层框架代表着,使用它可以更简单的操作和读取数据库工具。
是ORM框架,说明MyBatis可以将OOP中的对象和关系型数据库中的数据建立起映射关系,自动完成数据和对象的互相转换:①将输入数据和sql映射成原生sql②将结果集映射成返回对象。
ORM将数据库映射成对象:
数据库->类
记录->对象
字段->对象属性
这意味着我们可以像操作对象操作数据库中的表,实现对象和数据库表之间的转换。
MyBatis支持自定义SQL(所有的sql需要自己去写,半自动),存储过程以及高级映射(多张表映射到一个对象中的多个属性)。使用它可以通过简单的XML配置或者注解辅助操作,但是现在用的更多的还是XML配置的方式。
二、MyBatis开发环境配置
1.创建数据库表
-- 创建数据库
drop database if exists mycnblog;
create database mycnblog DEFAULT CHARACTER SET utf8mb4;
-- 使用数据数据
use mycnblog;
-- 创建表[用户表]
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 timestamp default current_timestamp,
    updatetime timestamp default current_timestamp,
    `state` int default 1
) default charset 'utf8mb4';
-- 创建文章表
drop table if exists  articleinfo;
create table articleinfo(
    id int primary key auto_increment,
    title varchar(100) not null,
    content text not null,
    createtime timestamp default current_timestamp,
    updatetime timestamp default current_timestamp,
    uid int not null,
    rcount int not null default 1,
    `state` int default 1
)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),
(2, 'zhangsan', 'zhangsan', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1);
-- 文章添加测试数据
insert into articleinfo(title,content,uid)
    values('Java','Java正文',1);
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-APqECCaj-1691465946131)(F:\typora插图\image-20230515161925545.png)]](https://img-blog.csdnimg.cn/7aef877bb1ea46c78e04dfbd70f247e3.png)
2.添加MyBatis框架支持
老项目中加框架支持:
<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.3.0</version>
</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
</dependency>
说明:这里除了手动加框架外,还可以使用插件——editstarters,在pom.xml中右键->生成->查找想要的框架勾选->OK
新项目中添加框架支持:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yEqtCVpq-1691465946132)(F:\typora插图\image-20230515162420494.png)]](https://img-blog.csdnimg.cn/86b549435c6c4b67bde6b94a82a46407.png)
切记:这里是两个,一个是mysql的驱动和mybatis的框架。
why:数据库可不是只有Mysql一家,Mybatis为很多关系型数据库提供框架支持,对应到每个具体的数据库就需要单独的驱动。
何为驱动:直接驱动硬件
3.配置连接字符串和MyBatis
在整个项目中添加配置——application.properties文件中:
#协议,ip,端口号,是否用加密的方式(最后一个视本机电脑而定)
spring.datasource.url=jdbc:mysql://localhost:3306/mycnblog?characterEncoding=utf8&useSSL=false
#用户名
spring.datasource.username=root
#注意!!password是本机的mysql的密码,不是固定的值
spring.datasource.password=111111
#设置Mysql的驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#设置MyBatis的驱动
mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml
说明:这里的配置有两方面
-  数据库连接字符串设置——url、username、password、driver-class-name 
-  MyBatis的XML文件设置——保存查询数据库的具体操作sql的文件 匹配一级 可以匹配多个级别 
除了上边两大部分是必须的外,我们还可以配置下边两个配置项,可以在测试的时候打印执行的sql,方便分析执行和问题的查找。
#日志打印实现+日志打印级别(我们自己的)==>打印mybatis执行的sql【默认输出info级别,但是这是属于debug级别的,所以我们需要修改日志打印级别】
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
logging.level.com.example.demo=debug
4.添加业务代码流程
- 添加表对应的实体类
- 添加mapper接口(数据持久层),在下边建对应的接口
- 添加相应表的xml设置(表相关具体方法实现)
- 添加service
- 添加controller
- 使用postman/ajax测试
根据我们写的sql,建立对应的项目结构:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PzZ9uapF-1691465946133)(F:\typora插图\image-20230515164049713.png)]](https://img-blog.csdnimg.cn/c5465dfde563481b8266d63779d91d60.png)
定义相应的实体类:
UserEntity:
package com.example.demo.entity;
import lombok.Data;
import lombok.Setter;
import java.time.LocalDateTime;
@Data
//不需要五大注解
public class UserEntity {
    //建议属性名和字段名设置相同,mybatis会自动实现关联
    private Integer id;
    private String username;
    private String password;
    private String photo;
    //原则:代码接收大于数据库字段类型即可
    //string 格式化,后期还是需要统一的对时间字段进行格式化处理
    private LocalDateTime createtime;
    private LocalDateTime updatatime;
    private Integer state;
}
ArticleInfo:
package com.example.demo.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class ArticleInfo {
    private int id;
    private String title;
    private String content;
    private LocalDateTime createtime;
    private LocalDateTime updatatime;
    private int uid;
    private int rcount;
    private int state;
}
ArticleInfoVO:
package com.example.demo.entity.vo;
import com.example.demo.entity.ArticleInfo;
import lombok.Data;
@Data
public class ArticleInfoVO extends ArticleInfo {
    private String username;
    @Override
    public String toString() {
        return "ArticleInfoVO{" +
                "username='" + username + '\'' +
                "} " + super.toString();
    }
}
说明:
1.这里mapper下边显示的两个鸟是因为安装了个插件,没加的显示的时候是I
2.因为ArticleInfo由lombok自动生成的toString方法,信息不完整,所以这里又重写了。当我们自己写了和lombok提供的一样的,以我们自己写的为主。
3.文章具体信息对应的实体类和ArticleInfo不属于同一级,所以这里又建了一级:vo,并在此中建立ArticleInfoVO,并重写toString方法
当我们想要完成一条sql操作的执行,就是先在mapper层声明方法,然后在对应的xml文件中实现即可。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yLpyw3Qg-1691465946133)(F:\typora插图\image-20230515170440555.png)]](https://img-blog.csdnimg.cn/71b4368bb29141d78135053ddd3bccaa.png)



















